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

147 lines
3.0 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-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
const Factor& factor (void) const { return factor_; }
2012-03-31 23:27:37 +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
const VarNodes& neighbors (void) const { return neighs_; }
2012-03-22 11:33:24 +00:00
int getIndex (void) const { return index_; }
2012-03-31 23:27:37 +01:00
void setIndex (int index) { index_ = index; }
2012-03-31 23:27:37 +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
};
2012-04-05 23:00:48 +01:00
class FactorGraph
{
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
~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-16 19:29:55 +01:00
unsigned nrVarNodes (void) const { return varNodes_.size(); }
unsigned nrFacNodes (void) const { return facNodes_.size(); }
2012-04-05 23:00:48 +01:00
VarNode* getVarNode (VarId vid) const
{
2012-04-10 12:53:52 +01:00
VarMap::const_iterator it = varMap_.find (vid);
return it != varMap_.end() ? it->second : 0;
}
2012-03-31 23:27:37 +01:00
void readFromUaiFormat (const char*);
void readFromLibDaiFormat (const char*);
void addFactor (const Factor& factor);
2012-03-31 23:27:37 +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;
static bool orderVariables;
2012-03-22 11:33:24 +00:00
private:
2012-03-31 23:27:37 +01:00
// DISALLOW_COPY_AND_ASSIGN (FactorGraph);
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-12-12 15:29:51 +00:00
#endif // HORUS_FACTORGRAPH_H