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>
|
|
|
|
|
|
|
|
#include "GraphicalModel.h"
|
|
|
|
#include "Shared.h"
|
2011-12-12 15:29:51 +00:00
|
|
|
#include "Distribution.h"
|
|
|
|
#include "Factor.h"
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
class FgFacNode;
|
|
|
|
|
|
|
|
class FgVarNode : public VarNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FgVarNode (VarId varId, unsigned nrStates) : VarNode (varId, nrStates) { }
|
|
|
|
FgVarNode (const VarNode* v) : VarNode (v) { }
|
|
|
|
void addNeighbor (FgFacNode* fn)
|
|
|
|
{
|
|
|
|
neighs_.push_back (fn);
|
|
|
|
}
|
|
|
|
const vector<FgFacNode*>& neighbors (void) const
|
|
|
|
{
|
|
|
|
return neighs_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN (FgVarNode);
|
|
|
|
// members
|
|
|
|
vector<FgFacNode*> neighs_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FgFacNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FgFacNode (Factor* factor)
|
|
|
|
{
|
|
|
|
factor_ = factor;
|
|
|
|
index_ = -1;
|
|
|
|
}
|
|
|
|
Factor* factor() const
|
|
|
|
{
|
|
|
|
return factor_;
|
|
|
|
}
|
|
|
|
void addNeighbor (FgVarNode* vn)
|
|
|
|
{
|
|
|
|
neighs_.push_back (vn);
|
|
|
|
}
|
|
|
|
const vector<FgVarNode*>& neighbors (void) const
|
|
|
|
{
|
|
|
|
return neighs_;
|
|
|
|
}
|
|
|
|
int getIndex (void) const
|
|
|
|
{
|
|
|
|
assert (index_ != -1);
|
|
|
|
return index_;
|
|
|
|
}
|
|
|
|
void setIndex (int index)
|
|
|
|
{
|
|
|
|
index_ = index;
|
|
|
|
}
|
|
|
|
Distribution* getDistribution (void)
|
|
|
|
{
|
|
|
|
return factor_->getDistribution();
|
|
|
|
}
|
|
|
|
const ParamSet& getParameters (void) const
|
|
|
|
{
|
|
|
|
return factor_->getParameters();
|
|
|
|
}
|
|
|
|
string getLabel (void)
|
|
|
|
{
|
|
|
|
return factor_->getLabel();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN (FgFacNode);
|
|
|
|
|
|
|
|
Factor* factor_;
|
|
|
|
int index_;
|
|
|
|
vector<FgVarNode*> neighs_;
|
|
|
|
};
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
class FactorGraph : public GraphicalModel
|
|
|
|
{
|
|
|
|
public:
|
2011-07-22 21:33:30 +01:00
|
|
|
FactorGraph (void) {};
|
|
|
|
FactorGraph (const BayesNet&);
|
2011-05-17 12:00:33 +01:00
|
|
|
~FactorGraph (void);
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
void readFromUaiFormat (const char*);
|
|
|
|
void readFromLibDaiFormat (const char*);
|
2011-07-22 21:33:30 +01:00
|
|
|
void addVariable (FgVarNode*);
|
2011-12-12 15:29:51 +00:00
|
|
|
void addFactor (FgFacNode*);
|
|
|
|
void addEdge (FgVarNode*, FgFacNode*);
|
|
|
|
void addEdge (FgFacNode*, FgVarNode*);
|
|
|
|
VarNode* getVariableNode (unsigned) const;
|
|
|
|
VarNodes getVariableNodes (void) const;
|
|
|
|
bool isTree (void) const;
|
2011-07-22 21:33:30 +01:00
|
|
|
void setIndexes (void);
|
|
|
|
void freeDistributions (void);
|
|
|
|
void printGraphicalModel (void) const;
|
2011-12-12 15:29:51 +00:00
|
|
|
void exportToGraphViz (const char*) const;
|
2011-07-22 21:33:30 +01:00
|
|
|
void exportToUaiFormat (const char*) const;
|
2011-12-12 15:29:51 +00:00
|
|
|
void exportToLibDaiFormat (const char*) const;
|
|
|
|
|
|
|
|
const FgVarSet& getVarNodes (void) const { return varNodes_; }
|
|
|
|
const FgFacSet& getFactorNodes (void) const { return facNodes_; }
|
2011-07-22 21:33:30 +01:00
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
FgVarNode* getFgVarNode (VarId vid) const
|
2011-07-22 21:33:30 +01:00
|
|
|
{
|
|
|
|
IndexMap::const_iterator it = indexMap_.find (vid);
|
|
|
|
if (it == indexMap_.end()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return varNodes_[it->second];
|
|
|
|
}
|
|
|
|
}
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
private:
|
2011-12-12 15:29:51 +00:00
|
|
|
bool containsCycle (void) const;
|
|
|
|
bool containsCycle (const FgVarNode*, const FgFacNode*,
|
|
|
|
vector<bool>&, vector<bool>&) const;
|
|
|
|
bool containsCycle (const FgFacNode*, const FgVarNode*,
|
|
|
|
vector<bool>&, vector<bool>&) const;
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN (FactorGraph);
|
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
FgVarSet varNodes_;
|
|
|
|
FgFacSet facNodes_;
|
|
|
|
|
|
|
|
typedef unordered_map<unsigned, unsigned> IndexMap;
|
|
|
|
IndexMap indexMap_;
|
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
|
|
|
|