2011-12-12 15:29:51 +00:00
|
|
|
#ifndef HORUS_FACTORGRAPH_H
|
|
|
|
#define HORUS_FACTORGRAPH_H
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
#include "Factor.h"
|
2012-04-05 18:38:56 +01:00
|
|
|
#include "BayesNet.h"
|
2012-03-22 11:33:24 +00:00
|
|
|
#include "Horus.h"
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-04-05 18:38:56 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
class FacNode;
|
2011-12-12 15:29:51 +00:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
class VarNode : public Var
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-04-10 12:53:52 +01:00
|
|
|
VarNode (VarId varId, unsigned nrStates)
|
|
|
|
: Var (varId, nrStates) { }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNode (const Var* v) : Var (v) { }
|
2012-03-22 11:33:24 +00:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void addNeighbor (FacNode* fn) { neighs_.push_back (fn); }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
const FacNodes& neighbors (void) const { return neighs_; }
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
private:
|
2012-04-05 23:00:48 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN (VarNode);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNodes neighs_;
|
2011-12-12 15:29:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
class FacNode
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNode (const Factor& f) : factor_(f), index_(-1) { }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
const Factor& factor (void) const { return factor_; }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
Factor& factor (void) { return factor_; }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
void addNeighbor (VarNode* vn) { neighs_.push_back (vn); }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
const VarNodes& neighbors (void) const { return neighs_; }
|
2012-03-22 11:33:24 +00:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
int getIndex (void) const { return index_; }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
void setIndex (int index) { index_ = index; }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
string getLabel (void) { return factor_.getLabel(); }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
private:
|
2012-04-10 11:51:56 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN (FacNode);
|
2011-12-12 15:29:51 +00:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNodes neighs_;
|
2012-04-10 12:53:52 +01:00
|
|
|
Factor factor_;
|
2012-03-31 23:27:37 +01:00
|
|
|
int index_;
|
2012-03-22 11:33:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CompVarId
|
|
|
|
{
|
2012-04-05 23:00:48 +01:00
|
|
|
bool operator() (const Var* v1, const Var* v2) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-04-05 23:00:48 +01:00
|
|
|
return v1->varId() < v2->varId();
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
2011-12-12 15:29:51 +00:00
|
|
|
};
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
class FactorGraph
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2012-04-10 12:53:52 +01:00
|
|
|
FactorGraph (bool fbn = false) : fromBayesNet_(fbn) { }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-03-22 11:33:24 +00:00
|
|
|
FactorGraph (const FactorGraph&);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
~FactorGraph (void);
|
2011-12-12 15:29:51 +00:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
const VarNodes& varNodes (void) const { return varNodes_; }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
const FacNodes& facNodes (void) const { return facNodes_; }
|
2012-04-05 18:38:56 +01:00
|
|
|
|
|
|
|
bool isFromBayesNetwork (void) const { return fromBayesNet_ ; }
|
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNode* getVarNode (VarId vid) const
|
2011-07-22 21:33:30 +01:00
|
|
|
{
|
2012-04-10 12:53:52 +01:00
|
|
|
VarMap::const_iterator it = varMap_.find (vid);
|
|
|
|
return it != varMap_.end() ? it->second : 0;
|
2011-07-22 21:33:30 +01:00
|
|
|
}
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
void readFromUaiFormat (const char*);
|
|
|
|
|
|
|
|
void readFromLibDaiFormat (const char*);
|
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
void addFactor (const Factor& factor);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
void addVarNode (VarNode*);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void addFacNode (FacNode*);
|
2012-04-05 18:38:56 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void addEdge (VarNode*, FacNode*);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
|
|
|
bool isTree (void) const;
|
|
|
|
|
2012-04-05 18:38:56 +01:00
|
|
|
DAGraph& getStructure (void);
|
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
void print (void) const;
|
2012-03-31 23:27:37 +01:00
|
|
|
|
|
|
|
void exportToGraphViz (const char*) const;
|
|
|
|
|
|
|
|
void exportToUaiFormat (const char*) const;
|
|
|
|
|
|
|
|
void exportToLibDaiFormat (const char*) const;
|
|
|
|
|
2012-03-22 11:33:24 +00:00
|
|
|
static bool orderFactorVariables;
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
private:
|
2012-03-31 23:27:37 +01:00
|
|
|
// DISALLOW_COPY_AND_ASSIGN (FactorGraph);
|
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
void ignoreLines (std::ifstream&) const;
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
bool containsCycle (void) const;
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
bool containsCycle (const VarNode*, const FacNode*,
|
2012-03-31 23:27:37 +01:00
|
|
|
vector<bool>&, vector<bool>&) const;
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
bool containsCycle (const FacNode*, const VarNode*,
|
2012-03-31 23:27:37 +01:00
|
|
|
vector<bool>&, vector<bool>&) const;
|
2011-12-12 15:29:51 +00:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
VarNodes varNodes_;
|
|
|
|
FacNodes facNodes_;
|
2011-12-12 15:29:51 +00:00
|
|
|
|
2012-04-05 18:38:56 +01:00
|
|
|
DAGraph structure_;
|
2012-04-10 12:53:52 +01:00
|
|
|
bool fromBayesNet_;
|
2012-04-05 18:38:56 +01:00
|
|
|
|
2012-04-10 12:53:52 +01:00
|
|
|
typedef unordered_map<unsigned, VarNode*> VarMap;
|
|
|
|
VarMap varMap_;
|
2011-05-17 12:00:33 +01:00
|
|
|
};
|
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
#endif // HORUS_FACTORGRAPH_H
|
2011-07-22 21:33:30 +01:00
|
|
|
|