2013-02-07 17:50:02 +00:00
|
|
|
#ifndef YAP_PACKAGES_CLPBN_HORUS_FACTORGRAPH_H_
|
|
|
|
#define YAP_PACKAGES_CLPBN_HORUS_FACTORGRAPH_H_
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
#include <vector>
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
#include "Factor.h"
|
2012-06-19 14:32:12 +01:00
|
|
|
#include "BayesBallGraph.h"
|
2012-05-23 14:56:01 +01:00
|
|
|
#include "Horus.h"
|
|
|
|
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
class FacNode;
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class VarNode : public Var {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
2012-12-17 18:39:42 +00:00
|
|
|
VarNode (VarId varId, unsigned nrStates,
|
2013-02-13 18:54:15 +00:00
|
|
|
int evidence = Constants::unobserved)
|
2012-05-23 14:56:01 +01:00
|
|
|
: Var (varId, nrStates, evidence) { }
|
|
|
|
|
|
|
|
VarNode (const Var* v) : Var (v) { }
|
|
|
|
|
|
|
|
void addNeighbor (FacNode* fn) { neighs_.push_back (fn); }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const FacNodes& neighbors() const { return neighs_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
FacNodes neighs_;
|
2012-12-27 22:25:45 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN (VarNode);
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-28 17:00:46 +01:00
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class FacNode {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
|
|
|
FacNode (const Factor& f) : factor_(f), index_(-1) { }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const Factor& factor() const { return factor_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
Factor& factor() { return factor_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
void addNeighbor (VarNode* vn) { neighs_.push_back (vn); }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const VarNodes& neighbors() const { return neighs_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t getIndex() const { return index_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
void setIndex (size_t index) { index_ = index; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
std::string getLabel() { return factor_.getLabel(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
VarNodes neighs_;
|
|
|
|
Factor factor_;
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t index_;
|
2012-12-27 22:25:45 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN (FacNode);
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class FactorGraph {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
2013-02-28 19:45:37 +00:00
|
|
|
FactorGraph() : bayesFactors_(false) { }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
FactorGraph (const FactorGraph&);
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
~FactorGraph();
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const VarNodes& varNodes() const { return varNodes_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const FacNodes& facNodes() const { return facNodes_; }
|
2012-05-31 22:42:38 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void setFactorsAsBayesian() { bayesFactors_ = true; }
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool bayesianFactors() const { return bayesFactors_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t nrVarNodes() const { return varNodes_.size(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t nrFacNodes() const { return facNodes_.size(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
VarNode* getVarNode (VarId vid) const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
void addFactor (const Factor& factor);
|
|
|
|
|
|
|
|
void addVarNode (VarNode*);
|
|
|
|
|
|
|
|
void addFacNode (FacNode*);
|
|
|
|
|
|
|
|
void addEdge (VarNode*, FacNode*);
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool isTree() const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
BayesBallGraph& getStructure();
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void print() const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-01-08 17:01:03 +00:00
|
|
|
void exportToLibDai (const char*) const;
|
|
|
|
|
|
|
|
void exportToUai (const char*) const;
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void exportToGraphViz (const char*) const;
|
|
|
|
|
2013-03-14 16:57:34 +00:00
|
|
|
FactorGraph& operator= (const FactorGraph&);
|
|
|
|
|
|
|
|
static FactorGraph readFromUaiFormat (const char*);
|
|
|
|
|
|
|
|
static FactorGraph readFromLibDaiFormat (const char*);
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static bool exportToLibDai() { return exportLd_; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static bool exportToUai() { return exportUai_; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static bool exportGraphViz() { return exportGv_; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static bool printFactorGraph() { return printFg_; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void enableExportToLibDai() { exportLd_ = true; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void disableExportToLibDai() { exportLd_ = false; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void enableExportToUai() { exportUai_ = true; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void disableExportToUai() { exportUai_ = false; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void enableExportToGraphViz() { exportGv_ = true; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void disableExportToGraphViz() { exportGv_ = false; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void enablePrintFactorGraph() { printFg_ = true; }
|
2013-01-08 17:01:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static void disablePrintFactorGraph() { printFg_ = false; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
typedef std::unordered_map<unsigned, VarNode*> VarMap;
|
|
|
|
|
2013-03-14 16:57:34 +00:00
|
|
|
void clone (const FactorGraph& fg);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool containsCycle() const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
bool containsCycle (const VarNode*, const FacNode*,
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<bool>&, std::vector<bool>&) const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
bool containsCycle (const FacNode*, const VarNode*,
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<bool>&, std::vector<bool>&) const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-03-14 16:57:34 +00:00
|
|
|
static void ignoreLines (std::ifstream&);
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
VarNodes varNodes_;
|
|
|
|
FacNodes facNodes_;
|
|
|
|
VarMap varMap_;
|
2012-12-27 22:25:45 +00:00
|
|
|
BayesBallGraph structure_;
|
|
|
|
bool bayesFactors_;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
static bool exportLd_;
|
|
|
|
static bool exportUai_;
|
|
|
|
static bool exportGv_;
|
|
|
|
static bool printFg_;
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
2012-05-28 17:00:46 +01:00
|
|
|
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
inline VarNode*
|
|
|
|
FactorGraph::getVarNode (VarId vid) const
|
|
|
|
{
|
|
|
|
VarMap::const_iterator it = varMap_.find (vid);
|
|
|
|
return it != varMap_.end() ? it->second : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-08 01:11:18 +00:00
|
|
|
struct sortByVarId {
|
2012-12-17 18:39:42 +00:00
|
|
|
bool operator()(VarNode* vn1, VarNode* vn2) {
|
2012-05-28 17:00:46 +01:00
|
|
|
return vn1->varId() < vn2->varId();
|
2013-02-08 01:11:18 +00:00
|
|
|
}};
|
2012-05-28 17:00:46 +01:00
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2012-05-28 17:00:46 +01:00
|
|
|
|
2013-02-08 00:20:01 +00:00
|
|
|
#endif // YAP_PACKAGES_CLPBN_HORUS_FACTORGRAPH_H_
|
2012-05-23 14:56:01 +01:00
|
|
|
|