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/horus/FactorGraph.h

179 lines
3.9 KiB
C
Raw Normal View History

2012-05-23 14:56:01 +01:00
#ifndef HORUS_FACTORGRAPH_H
#define HORUS_FACTORGRAPH_H
#include <vector>
#include "Factor.h"
2012-06-19 14:32:12 +01:00
#include "BayesBallGraph.h"
2012-05-23 14:56:01 +01:00
#include "Horus.h"
using namespace std;
class FacNode;
class VarNode : public Var
{
public:
2012-12-17 18:39:42 +00:00
VarNode (VarId varId, unsigned nrStates,
int evidence = Constants::NO_EVIDENCE)
2012-05-23 14:56:01 +01:00
: Var (varId, nrStates, evidence) { }
VarNode (const Var* v) : Var (v) { }
void addNeighbor (FacNode* fn) { neighs_.push_back (fn); }
const FacNodes& neighbors (void) const { return neighs_; }
private:
FacNodes neighs_;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (VarNode);
2012-05-23 14:56:01 +01:00
};
2012-05-28 17:00:46 +01:00
2012-05-23 14:56:01 +01:00
class FacNode
{
public:
FacNode (const Factor& f) : factor_(f), index_(-1) { }
const Factor& factor (void) const { return factor_; }
Factor& factor (void) { return factor_; }
void addNeighbor (VarNode* vn) { neighs_.push_back (vn); }
const VarNodes& neighbors (void) const { return neighs_; }
2012-05-24 22:55:20 +01:00
size_t getIndex (void) const { return index_; }
2012-05-23 14:56:01 +01:00
2012-05-24 22:55:20 +01:00
void setIndex (size_t index) { index_ = index; }
2012-05-23 14:56:01 +01:00
string getLabel (void) { return factor_.getLabel(); }
private:
VarNodes neighs_;
Factor factor_;
2012-05-24 22:55:20 +01:00
size_t index_;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (FacNode);
2012-05-23 14:56:01 +01:00
};
class FactorGraph
{
public:
FactorGraph (void) : bayesFactors_(false) { }
2012-05-23 14:56:01 +01:00
FactorGraph (const FactorGraph&);
~FactorGraph (void);
const VarNodes& varNodes (void) const { return varNodes_; }
const FacNodes& facNodes (void) const { return facNodes_; }
void setFactorsAsBayesian (void) { bayesFactors_ = true; }
2012-12-20 23:19:10 +00:00
2012-12-17 18:39:42 +00:00
bool bayesianFactors (void) const { return bayesFactors_; }
2012-05-23 14:56:01 +01:00
2012-05-24 22:55:20 +01:00
size_t nrVarNodes (void) const { return varNodes_.size(); }
2012-05-23 14:56:01 +01:00
2012-05-24 22:55:20 +01:00
size_t nrFacNodes (void) const { return facNodes_.size(); }
2012-05-23 14:56:01 +01:00
VarNode* getVarNode (VarId vid) const
{
VarMap::const_iterator it = varMap_.find (vid);
return it != varMap_.end() ? it->second : 0;
}
void readFromUaiFormat (const char*);
void readFromLibDaiFormat (const char*);
void addFactor (const Factor& factor);
void addVarNode (VarNode*);
void addFacNode (FacNode*);
void addEdge (VarNode*, FacNode*);
bool isTree (void) const;
2012-06-19 14:32:12 +01:00
BayesBallGraph& getStructure (void);
2012-05-23 14:56:01 +01:00
void print (void) const;
void exportToLibDai (const char*) const;
void exportToUai (const char*) const;
2012-05-23 14:56:01 +01:00
void exportToGraphViz (const char*) const;
static bool exportToLibDai (void) { return exportLd_; }
static bool exportToUai (void) { return exportUai_; }
static bool exportGraphViz (void) { return exportGv_; }
static bool printFactorGraph (void) { return printFg_; }
static void enableExportToLibDai (void) { exportLd_ = true; }
static void disableExportToLibDai (void) { exportLd_ = false; }
2012-05-23 14:56:01 +01:00
static void enableExportToUai (void) { exportUai_ = true; }
static void disableExportToUai (void) { exportUai_ = false; }
static void enableExportToGraphViz (void) { exportGv_ = true; }
static void disableExportToGraphViz (void) { exportGv_ = false; }
static void enablePrintFactorGraph (void) { printFg_ = true; }
static void disablePrintFactorGraph (void) { printFg_ = false; }
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
private:
void ignoreLines (std::ifstream&) const;
bool containsCycle (void) const;
bool containsCycle (const VarNode*, const FacNode*,
vector<bool>&, vector<bool>&) const;
bool containsCycle (const FacNode*, const VarNode*,
vector<bool>&, vector<bool>&) const;
VarNodes varNodes_;
FacNodes facNodes_;
2012-12-27 22:25:45 +00:00
BayesBallGraph structure_;
bool bayesFactors_;
2012-05-23 14:56:01 +01:00
typedef unordered_map<unsigned, VarNode*> VarMap;
VarMap varMap_;
2012-12-27 22:25:45 +00:00
static bool exportLd_;
static bool exportUai_;
static bool exportGv_;
static bool printFg_;
2012-12-27 22:25:45 +00:00
DISALLOW_ASSIGN (FactorGraph);
2012-05-23 14:56:01 +01:00
};
2012-05-28 17:00:46 +01:00
struct sortByVarId
2012-12-17 18:39:42 +00:00
{
bool operator()(VarNode* vn1, VarNode* vn2) {
2012-05-28 17:00:46 +01:00
return vn1->varId() < vn2->varId();
}
};
2012-05-23 14:56:01 +01:00
#endif // HORUS_FACTORGRAPH_H