From 401ad145a5859ef5a1f2bf677f57c9795f5b6466 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Sat, 14 Apr 2012 17:57:24 +0100 Subject: [PATCH] improve the way the factor graph is constructed from a set from factors --- packages/CLPBN/clpbn/bp/FactorGraph.cpp | 17 ++++++++--------- packages/CLPBN/clpbn/bp/FactorGraph.h | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/CLPBN/clpbn/bp/FactorGraph.cpp b/packages/CLPBN/clpbn/bp/FactorGraph.cpp index e1d1bf93f..a9e9e0036 100644 --- a/packages/CLPBN/clpbn/bp/FactorGraph.cpp +++ b/packages/CLPBN/clpbn/bp/FactorGraph.cpp @@ -13,7 +13,7 @@ #include "Util.h" -bool FactorGraph::orderFactorVariables = false; +bool FactorGraph::orderVariables = false; FactorGraph::FactorGraph (const FactorGraph& fg) @@ -31,6 +31,7 @@ FactorGraph::FactorGraph (const FactorGraph& fg) addEdge (varNodes_[neighs[j]->getIndex()], facNode); } } + fromBayesNet_ = fg.isFromBayesNetwork(); } @@ -101,6 +102,7 @@ FactorGraph::readFromUaiFormat (const char* fileName) if (Globals::logDomain) { Util::toLog (params); } + // TODO order vars is flag on addFactor (Factor (factorVarIds[i], factorRanges[i], params)); } is.close(); @@ -160,6 +162,7 @@ FactorGraph::readFromLibDaiFormat (const char* fileName) if (Globals::logDomain) { Util::toLog (params); } + // TODO order vars is flag on addFactor (Factor (vids, ranges, params)); } is.close(); @@ -186,14 +189,10 @@ FactorGraph::addFactor (const Factor& factor) addFacNode (fn); const VarIds& vids = factor.arguments(); for (unsigned i = 0; i < vids.size(); i++) { - bool found = false; - for (unsigned j = 0; j < varNodes_.size(); j++) { - if (varNodes_[j]->varId() == vids[i]) { - addEdge (varNodes_[j], fn); - found = true; - } - } - if (found == false) { + VarMap::const_iterator it = varMap_.find (vids[i]); + if (it != varMap_.end()) { + addEdge (it->second, fn); + } else { VarNode* vn = new VarNode (vids[i], factor.range (i)); addVarNode (vn); addEdge (vn, fn); diff --git a/packages/CLPBN/clpbn/bp/FactorGraph.h b/packages/CLPBN/clpbn/bp/FactorGraph.h index e3cd3656d..8503b1aa0 100644 --- a/packages/CLPBN/clpbn/bp/FactorGraph.h +++ b/packages/CLPBN/clpbn/bp/FactorGraph.h @@ -113,7 +113,7 @@ class FactorGraph void exportToLibDaiFormat (const char*) const; - static bool orderFactorVariables; + static bool orderVariables; private: // DISALLOW_COPY_AND_ASSIGN (FactorGraph);