From 86b57e961eaf37dded981a506b2165a84fa99c21 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 11 Sep 2012 18:40:41 +0100 Subject: [PATCH 1/5] fix memory leak --- packages/CLPBN/horus/LiftedBp.cpp | 13 +++++++------ packages/CLPBN/horus/LiftedBp.h | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/CLPBN/horus/LiftedBp.cpp b/packages/CLPBN/horus/LiftedBp.cpp index 8fc8573b6..eb538f12a 100644 --- a/packages/CLPBN/horus/LiftedBp.cpp +++ b/packages/CLPBN/horus/LiftedBp.cpp @@ -8,7 +8,8 @@ LiftedBp::LiftedBp (const ParfactorList& pfList) : pfList_(pfList) { refineParfactors(); - solver_ = new WeightedBp (*getFactorGraph(), getWeights()); + createFactorGraph(); + solver_ = new WeightedBp (*fg_, getWeights()); } @@ -16,6 +17,7 @@ LiftedBp::LiftedBp (const ParfactorList& pfList) LiftedBp::~LiftedBp (void) { delete solver_; + delete fg_; } @@ -131,10 +133,10 @@ LiftedBp::getQueryGroups (const Grounds& query) -FactorGraph* -LiftedBp::getFactorGraph (void) +void +LiftedBp::createFactorGraph (void) { - FactorGraph* fg = new FactorGraph(); + fg_ = new FactorGraph(); ParfactorList::const_iterator it = pfList_.begin(); for (; it != pfList_.end(); ++it) { vector groups = (*it)->getAllGroups(); @@ -142,9 +144,8 @@ LiftedBp::getFactorGraph (void) for (size_t i = 0; i < groups.size(); i++) { varIds.push_back (groups[i]); } - fg->addFactor (Factor (varIds, (*it)->ranges(), (*it)->params())); + fg_->addFactor (Factor (varIds, (*it)->ranges(), (*it)->params())); } - return fg; } diff --git a/packages/CLPBN/horus/LiftedBp.h b/packages/CLPBN/horus/LiftedBp.h index 01e18310e..c34956320 100644 --- a/packages/CLPBN/horus/LiftedBp.h +++ b/packages/CLPBN/horus/LiftedBp.h @@ -24,7 +24,7 @@ class LiftedBp vector getQueryGroups (const Grounds&); - FactorGraph* getFactorGraph (void); + void createFactorGraph (void); vector> getWeights (void) const; @@ -34,6 +34,7 @@ class LiftedBp ParfactorList pfList_; WeightedBp* solver_; + FactorGraph* fg_; }; From febb17d7530ba8545f8fb65199e0b3abee91c874 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 11 Sep 2012 18:48:16 +0100 Subject: [PATCH 2/5] fix factor joint --- packages/CLPBN/horus/BeliefProp.cpp | 5 ++--- packages/CLPBN/horus/BeliefProp.h | 2 +- packages/CLPBN/horus/LiftedBp.cpp | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/CLPBN/horus/BeliefProp.cpp b/packages/CLPBN/horus/BeliefProp.cpp index d65c36a01..ed94e8c12 100644 --- a/packages/CLPBN/horus/BeliefProp.cpp +++ b/packages/CLPBN/horus/BeliefProp.cpp @@ -118,20 +118,19 @@ BeliefProp::getJointDistributionOf (const VarIds& jointVarIds) if (idx == facNodes.size()) { return getJointByConditioning (jointVarIds); } - return getFactorJoint (idx, jointVarIds); + return getFactorJoint (facNodes[idx], jointVarIds); } Params BeliefProp::getFactorJoint ( - size_t fnIdx, + FacNode* fn, const VarIds& jointVarIds) { if (runned_ == false) { runSolver(); } - FacNode* fn = fg.facNodes()[fnIdx]; Factor res (fn->factor()); const BpLinks& links = ninf(fn)->getLinks(); for (size_t i = 0; i < links.size(); i++) { diff --git a/packages/CLPBN/horus/BeliefProp.h b/packages/CLPBN/horus/BeliefProp.h index 44c867dbc..1545abfc8 100644 --- a/packages/CLPBN/horus/BeliefProp.h +++ b/packages/CLPBN/horus/BeliefProp.h @@ -112,7 +112,7 @@ class BeliefProp : public Solver virtual Params getJointByConditioning (const VarIds&) const; public: - Params getFactorJoint (size_t fnIdx, const VarIds&); + Params getFactorJoint (FacNode* fn, const VarIds&); protected: SPNodeInfo* ninf (const VarNode* var) const diff --git a/packages/CLPBN/horus/LiftedBp.cpp b/packages/CLPBN/horus/LiftedBp.cpp index eb538f12a..05a5ea6af 100644 --- a/packages/CLPBN/horus/LiftedBp.cpp +++ b/packages/CLPBN/horus/LiftedBp.cpp @@ -49,7 +49,7 @@ LiftedBp::solveQuery (const Grounds& query) for (unsigned i = 0; i < groups.size(); i++) { queryVids.push_back (groups[i]); } - res = solver_->getFactorJoint (idx, queryVids); + res = solver_->getFactorJoint (fg_->facNodes()[idx], queryVids); } } return res; From 701911ef096e636b46e1f82f2174b9d857391eac Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 11 Sep 2012 18:48:54 +0100 Subject: [PATCH 3/5] forgot this --- packages/CLPBN/horus/CountingBp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/CLPBN/horus/CountingBp.cpp b/packages/CLPBN/horus/CountingBp.cpp index faf9f4d18..6d796a3c6 100644 --- a/packages/CLPBN/horus/CountingBp.cpp +++ b/packages/CLPBN/horus/CountingBp.cpp @@ -81,7 +81,7 @@ CountingBp::solveQuery (VarIds queryVids) for (size_t i = 0; i < queryVids.size(); i++) { reprArgs.push_back (getRepresentative (queryVids[i])); } - res = solver_->getFactorJoint (idx, reprArgs); + res = solver_->getFactorJoint (facNodes[idx], reprArgs); } } return res; From 10b15f606a857b8ac64cf78e209ba974358eb815 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 18 Sep 2012 17:24:22 +0100 Subject: [PATCH 4/5] fix counting bp calculation of joint distribution --- packages/CLPBN/horus/CountingBp.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/CLPBN/horus/CountingBp.cpp b/packages/CLPBN/horus/CountingBp.cpp index 6d796a3c6..365ff7098 100644 --- a/packages/CLPBN/horus/CountingBp.cpp +++ b/packages/CLPBN/horus/CountingBp.cpp @@ -81,7 +81,9 @@ CountingBp::solveQuery (VarIds queryVids) for (size_t i = 0; i < queryVids.size(); i++) { reprArgs.push_back (getRepresentative (queryVids[i])); } - res = solver_->getFactorJoint (facNodes[idx], reprArgs); + FacNode* reprFac = getRepresentative (facNodes[idx]); + assert (reprFac != 0); + res = solver_->getFactorJoint (reprFac, reprArgs); } } return res; From 559b3e3b541c9c12d091063ba04c6c1e50b53e11 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Sun, 30 Sep 2012 22:04:44 +0100 Subject: [PATCH 5/5] watch out for division by 0 in message calculation for BP --- packages/CLPBN/horus/BeliefProp.cpp | 10 ++++++---- packages/CLPBN/horus/WeightedBp.cpp | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/CLPBN/horus/BeliefProp.cpp b/packages/CLPBN/horus/BeliefProp.cpp index ed94e8c12..314f4a6c5 100644 --- a/packages/CLPBN/horus/BeliefProp.cpp +++ b/packages/CLPBN/horus/BeliefProp.cpp @@ -349,20 +349,22 @@ BeliefProp::getVarToFactorMsg (const BpLink* link) const const BpLinks& links = ninf (src)->getLinks(); if (Globals::logDomain) { for (it = links.begin(); it != links.end(); ++it) { - msg += (*it)->message(); + if (*it != link) { + msg += (*it)->message(); + } if (Constants::SHOW_BP_CALCS) { cout << " x " << (*it)->message(); } } - msg -= link->message(); } else { for (it = links.begin(); it != links.end(); ++it) { - msg *= (*it)->message(); + if (*it != link) { + msg *= (*it)->message(); + } if (Constants::SHOW_BP_CALCS) { cout << " x " << (*it)->message(); } } - msg /= link->message(); } if (Constants::SHOW_BP_CALCS) { cout << " = " << msg; diff --git a/packages/CLPBN/horus/WeightedBp.cpp b/packages/CLPBN/horus/WeightedBp.cpp index c4e308d4f..d8a32a246 100644 --- a/packages/CLPBN/horus/WeightedBp.cpp +++ b/packages/CLPBN/horus/WeightedBp.cpp @@ -275,7 +275,7 @@ void WeightedBp::printLinkInformation (void) const { for (size_t i = 0; i < links_.size(); i++) { - WeightedLink* l = static_cast (links_[i]); + WeightedLink* l = static_cast (links_[i]); cout << l->toString() << ":" << endl; cout << " curr msg = " << l->message() << endl; cout << " next msg = " << l->nextMessage() << endl;