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

168 lines
3.2 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 "BayesNet.h"
2012-03-22 11:33:24 +00:00
#include "Horus.h"
using namespace std;
2012-04-05 18:38:56 +01:00
2012-04-05 23:00:48 +01:00
class FactorNode;
2011-12-12 15:29:51 +00:00
2012-03-31 23:27:37 +01:00
2012-04-05 23:00:48 +01:00
class VarNode : public Var
2011-12-12 15:29:51 +00:00
{
public:
2012-04-05 23:00:48 +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-05 23:00:48 +01:00
void addNeighbor (FactorNode* fn) { neighs_.push_back (fn); }
2012-03-31 23:27:37 +01:00
2012-04-05 23:00:48 +01:00
const FactorNodes& 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-05 23:00:48 +01:00
FactorNodes neighs_;
2011-12-12 15:29:51 +00:00
};
2012-04-05 23:00:48 +01:00
class FactorNode
2011-12-12 15:29:51 +00:00
{
public:
2012-04-05 23:00:48 +01:00
FactorNode (const FactorNode* fn)
2012-03-31 23:27:37 +01:00
{
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-04-05 23:00:48 +01:00
FactorNode (Factor* f) : factor_(new Factor(*f)), index_(-1) { }
2012-03-31 23:27:37 +01:00
2012-04-05 23:00:48 +01:00
FactorNode (const Factor& f) : factor_(new Factor (f)), index_(-1) { }
2012-04-05 18:38:56 +01:00
2012-03-31 23:27:37 +01:00
Factor* factor() const { return factor_; }
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-05 23:00:48 +01:00
const VarNodes& neighbors (void) const { return neighs_; }
2012-03-22 11:33:24 +00:00
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:
2012-04-05 23:00:48 +01:00
DISALLOW_COPY_AND_ASSIGN (FactorNode);
2011-12-12 15:29:51 +00:00
2012-03-31 23:27:37 +01:00
Factor* factor_;
2012-04-05 23:00:48 +01:00
VarNodes neighs_;
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
};
2012-04-05 23:00:48 +01:00
class FactorGraph
{
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-04-05 23:00:48 +01:00
const VarNodes& varNodes (void) const { return varNodes_; }
2012-03-31 23:27:37 +01:00
2012-04-05 23:00:48 +01:00
const FactorNodes& factorNodes (void) const { return facNodes_; }
2012-04-05 18:38:56 +01:00
void setFromBayesNetwork (void) { fromBayesNet_ = true; }
bool isFromBayesNetwork (void) const { return fromBayesNet_ ; }
2012-04-05 23:00:48 +01:00
VarNode* getVarNode (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*);
2012-04-05 23:00:48 +01:00
void addVariable (VarNode*);
2012-03-31 23:27:37 +01:00
2012-04-05 23:00:48 +01:00
void addFactor (FactorNode*);
2012-03-31 23:27:37 +01:00
2012-04-05 18:38:56 +01:00
void addFactor (const Factor& factor);
2012-04-05 23:00:48 +01:00
void addEdge (VarNode*, FactorNode*);
2012-03-31 23:27:37 +01:00
2012-04-05 23:00:48 +01:00
void addEdge (FactorNode*, VarNode*);
2012-03-31 23:27:37 +01:00
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);
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;
private:
2012-03-31 23:27:37 +01:00
// DISALLOW_COPY_AND_ASSIGN (FactorGraph);
bool containsCycle (void) const;
2012-04-05 23:00:48 +01:00
bool containsCycle (const VarNode*, const FactorNode*,
2012-03-31 23:27:37 +01:00
vector<bool>&, vector<bool>&) const;
2012-04-05 23:00:48 +01:00
bool containsCycle (const FactorNode*, 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-05 23:00:48 +01:00
VarNodes varNodes_;
FactorNodes 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