This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/clpbn/bp/FactorGraph.h

173 lines
3.4 KiB
C
Raw Normal View History

2011-12-12 15:29:51 +00:00
#ifndef HORUS_FACTORGRAPH_H
#define HORUS_FACTORGRAPH_H
#include <vector>
2011-12-12 15:29:51 +00:00
#include "Factor.h"
2012-04-05 18:38:56 +01:00
#include "GraphicalModel.h"
#include "BayesNet.h"
2012-03-22 11:33:24 +00:00
#include "Horus.h"
using namespace std;
2012-04-05 18:38:56 +01:00
2011-12-12 15:29:51 +00:00
class FgFacNode;
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
class FgVarNode : public VarNode
{
public:
FgVarNode (VarId varId, unsigned nrStates) : VarNode (varId, nrStates) { }
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
FgVarNode (const VarNode* v) : VarNode (v) { }
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
void addNeighbor (FgFacNode* fn) { neighs_.push_back (fn); }
const FgFacSet& neighbors (void) const { return neighs_; }
2011-12-12 15:29:51 +00:00
private:
DISALLOW_COPY_AND_ASSIGN (FgVarNode);
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
FgFacSet neighs_;
2011-12-12 15:29:51 +00:00
};
class FgFacNode
{
public:
2012-03-31 23:27:37 +01:00
FgFacNode (const FgFacNode* fn)
{
2012-03-22 11:33:24 +00:00
factor_ = new Factor (*fn->factor());
2011-12-12 15:29:51 +00:00
index_ = -1;
}
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
FgFacNode (Factor* f) : factor_(new Factor(*f)), index_(-1) { }
2012-03-31 23:27:37 +01:00
2012-04-05 18:38:56 +01:00
FgFacNode (const Factor& f) : factor_(new Factor (f)), index_(-1) { }
2012-03-31 23:27:37 +01:00
Factor* factor() const { return factor_; }
void addNeighbor (FgVarNode* vn) { neighs_.push_back (vn); }
2012-03-22 11:33:24 +00:00
const FgVarSet& neighbors (void) const { return neighs_; }
2011-12-12 15:29:51 +00:00
int getIndex (void) const
{
assert (index_ != -1);
return index_;
}
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
void setIndex (int index)
{
index_ = index;
}
2012-03-31 23:27:37 +01:00
const Params& params (void) const
2011-12-12 15:29:51 +00:00
{
2012-03-31 23:27:37 +01:00
return factor_->params();
2011-12-12 15:29:51 +00:00
}
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
string getLabel (void)
{
return factor_->getLabel();
}
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
private:
DISALLOW_COPY_AND_ASSIGN (FgFacNode);
2012-03-31 23:27:37 +01:00
Factor* factor_;
FgVarSet neighs_;
int index_;
2012-03-22 11:33:24 +00:00
};
struct CompVarId
{
bool operator() (const VarNode* vn1, const VarNode* vn2) const
{
return vn1->varId() < vn2->varId();
}
2011-12-12 15:29:51 +00:00
};
class FactorGraph : public GraphicalModel
{
public:
2012-04-05 18:38:56 +01:00
FactorGraph (void) { }
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
~FactorGraph (void);
2011-12-12 15:29:51 +00:00
2012-03-31 23:27:37 +01:00
const FgVarSet& getVarNodes (void) const { return varNodes_; }
const FgFacSet& getFactorNodes (void) const { return facNodes_; }
2012-04-05 18:38:56 +01:00
void setFromBayesNetwork (void) { fromBayesNet_ = true; }
bool isFromBayesNetwork (void) const { return fromBayesNet_ ; }
2011-12-12 15:29:51 +00:00
FgVarNode* getFgVarNode (VarId vid) const
{
2012-03-22 11:33:24 +00:00
IndexMap::const_iterator it = varMap_.find (vid);
2012-04-05 18:38:56 +01:00
return (it != varMap_.end()) ? varNodes_[it->second] : 0;
}
2012-03-31 23:27:37 +01:00
void readFromUaiFormat (const char*);
void readFromLibDaiFormat (const char*);
void addVariable (FgVarNode*);
void addFactor (FgFacNode*);
2012-04-05 18:38:56 +01:00
void addFactor (const Factor& factor);
2012-03-31 23:27:37 +01:00
void addEdge (FgVarNode*, FgFacNode*);
void addEdge (FgFacNode*, FgVarNode*);
VarNode* getVariableNode (unsigned) const;
VarNodes getVariableNodes (void) const;
bool isTree (void) const;
2012-04-05 18:38:56 +01:00
DAGraph& getStructure (void);
2012-03-31 23:27:37 +01:00
void setIndexes (void);
void printGraphicalModel (void) const;
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;
private:
2012-03-31 23:27:37 +01:00
// DISALLOW_COPY_AND_ASSIGN (FactorGraph);
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-12-12 15:29:51 +00:00
2012-03-31 23:27:37 +01:00
FgVarSet varNodes_;
FgFacSet facNodes_;
2011-12-12 15:29:51 +00:00
2012-04-05 18:38:56 +01:00
bool fromBayesNet_;
DAGraph structure_;
2011-12-12 15:29:51 +00:00
typedef unordered_map<unsigned, unsigned> IndexMap;
2012-03-31 23:27:37 +01:00
IndexMap varMap_;
};
2011-12-12 15:29:51 +00:00
#endif // HORUS_FACTORGRAPH_H