workaround c++ bad design: don't allow creating a factor graph from some pointer

This commit is contained in:
Tiago Gomes 2012-05-31 22:42:38 +01:00
parent 3f0f41c8a9
commit f91e543d9d
4 changed files with 16 additions and 12 deletions

View File

@ -14,7 +14,7 @@
FactorGraph* FactorGraph*
BayesBall::getMinimalFactorGraph (const VarIds& queryIds) BayesBall::getMinimalFactorGraph (const VarIds& queryIds)
{ {
assert (fg_.isFromBayesNetwork()); assert (fg_.bayesianFactors());
Scheduling scheduling; Scheduling scheduling;
for (size_t i = 0; i < queryIds.size(); i++) { for (size_t i = 0; i < queryIds.size(); i++) {
assert (dag_.getNode (queryIds[i])); assert (dag_.getNode (queryIds[i]));

View File

@ -28,7 +28,7 @@ FactorGraph::FactorGraph (const FactorGraph& fg)
addEdge (varNodes_[neighs[j]->getIndex()], facNode); addEdge (varNodes_[neighs[j]->getIndex()], facNode);
} }
} }
fromBayesNet_ = fg.isFromBayesNetwork(); bayesFactors_ = fg.bayesianFactors();
} }
@ -239,7 +239,7 @@ FactorGraph::isTree (void) const
DAGraph& DAGraph&
FactorGraph::getStructure (void) FactorGraph::getStructure (void)
{ {
assert (fromBayesNet_); assert (bayesFactors_);
if (structure_.empty()) { if (structure_.empty()) {
for (size_t i = 0; i < varNodes_.size(); i++) { for (size_t i = 0; i < varNodes_.size(); i++) {
structure_.addNode (new DAGraphNode (varNodes_[i])); structure_.addNode (new DAGraphNode (varNodes_[i]));

View File

@ -65,7 +65,7 @@ class FacNode
class FactorGraph class FactorGraph
{ {
public: public:
FactorGraph (bool fbn = false) : fromBayesNet_(fbn) { } FactorGraph (void) : bayesFactors_(false) { }
FactorGraph (const FactorGraph&); FactorGraph (const FactorGraph&);
@ -75,7 +75,9 @@ class FactorGraph
const FacNodes& facNodes (void) const { return facNodes_; } const FacNodes& facNodes (void) const { return facNodes_; }
bool isFromBayesNetwork (void) const { return fromBayesNet_ ; } void setFactorsAsBayesian (void) { bayesFactors_ = true; }
bool bayesianFactors (void) const { return bayesFactors_ ; }
size_t nrVarNodes (void) const { return varNodes_.size(); } size_t nrVarNodes (void) const { return varNodes_.size(); }
@ -128,7 +130,7 @@ class FactorGraph
FacNodes facNodes_; FacNodes facNodes_;
DAGraph structure_; DAGraph structure_;
bool fromBayesNet_; bool bayesFactors_;
typedef unordered_map<unsigned, VarNode*> VarMap; typedef unordered_map<unsigned, VarNode*> VarMap;
VarMap varMap_; VarMap varMap_;

View File

@ -219,8 +219,10 @@ int
createGroundNetwork (void) createGroundNetwork (void)
{ {
string factorsType ((char*) YAP_AtomName (YAP_AtomOfTerm (YAP_ARG1))); string factorsType ((char*) YAP_AtomName (YAP_AtomOfTerm (YAP_ARG1)));
bool fromBayesNet = factorsType == "bayes"; FactorGraph* fg = new FactorGraph();
FactorGraph* fg = new FactorGraph (fromBayesNet); if (factorsType == "bayes") {
fg->setFactorsAsBayesian();
}
YAP_Term factorList = YAP_ARG2; YAP_Term factorList = YAP_ARG2;
while (factorList != YAP_TermNil()) { while (factorList != YAP_TermNil()) {
YAP_Term factor = YAP_HeadOfTerm (factorList); YAP_Term factor = YAP_HeadOfTerm (factorList);
@ -400,7 +402,7 @@ void runVeSolver (
results.reserve (tasks.size()); results.reserve (tasks.size());
for (size_t i = 0; i < tasks.size(); i++) { for (size_t i = 0; i < tasks.size(); i++) {
FactorGraph* mfg = fg; FactorGraph* mfg = fg;
if (fg->isFromBayesNetwork()) { if (fg->bayesianFactors()) {
// mfg = BayesBall::getMinimalFactorGraph (*fg, tasks[i]); // mfg = BayesBall::getMinimalFactorGraph (*fg, tasks[i]);
} }
// VarElimSolver solver (*mfg); // VarElimSolver solver (*mfg);
@ -410,7 +412,7 @@ void runVeSolver (
cout << endl; cout << endl;
} }
results.push_back (solver.solveQuery (tasks[i])); results.push_back (solver.solveQuery (tasks[i]));
if (fg->isFromBayesNetwork()) { if (fg->bayesianFactors()) {
// delete mfg; // delete mfg;
} }
} }
@ -429,7 +431,7 @@ void runBpSolver (
} }
Solver* solver = 0; Solver* solver = 0;
FactorGraph* mfg = fg; FactorGraph* mfg = fg;
if (fg->isFromBayesNetwork()) { if (fg->bayesianFactors()) {
//mfg = BayesBall::getMinimalFactorGraph ( //mfg = BayesBall::getMinimalFactorGraph (
// *fg, VarIds (vids.begin(),vids.end())); // *fg, VarIds (vids.begin(),vids.end()));
} }
@ -450,7 +452,7 @@ void runBpSolver (
for (size_t i = 0; i < tasks.size(); i++) { for (size_t i = 0; i < tasks.size(); i++) {
results.push_back (solver->solveQuery (tasks[i])); results.push_back (solver->solveQuery (tasks[i]));
} }
if (fg->isFromBayesNetwork()) { if (fg->bayesianFactors()) {
//delete mfg; //delete mfg;
} }
delete solver; delete solver;