diff --git a/packages/CLPBN/clpbn/horus_lifted.yap b/packages/CLPBN/clpbn/horus_lifted.yap index ef91711b0..fae5a4d83 100644 --- a/packages/CLPBN/clpbn/horus_lifted.yap +++ b/packages/CLPBN/clpbn/horus_lifted.yap @@ -2,7 +2,7 @@ Interface to Horus Lifted Solvers. Used by: - Lifted Variable Elimination - + - Lifted First-Order Belief Propagation ********************************************************/ :- module(clpbn_horus_lifted, diff --git a/packages/CLPBN/horus/CountingBp.cpp b/packages/CLPBN/horus/CountingBp.cpp index 1ee3b48f1..faf9f4d18 100644 --- a/packages/CLPBN/horus/CountingBp.cpp +++ b/packages/CLPBN/horus/CountingBp.cpp @@ -236,7 +236,7 @@ CountingBp::createClusters ( const VarNodes& groupVars = it->second; VarCluster* vc = new VarCluster (groupVars); for (size_t i = 0; i < groupVars.size(); i++) { - vid2VarCluster_.insert (make_pair (groupVars[i]->varId(), vc)); + varClusterMap_.insert (make_pair (groupVars[i]->varId(), vc)); } varClusters_.push_back (vc); } @@ -250,7 +250,7 @@ CountingBp::createClusters ( varClusters.reserve (neighs.size()); for (size_t i = 0; i < neighs.size(); i++) { VarId vid = neighs[i]->varId(); - varClusters.push_back (vid2VarCluster_.find (vid)->second); + varClusters.push_back (varClusterMap_.find (vid)->second); } facClusters_.push_back (new FacCluster (it->second, varClusters)); } @@ -294,8 +294,8 @@ CountingBp::getSignature (const FacNode* facNode) VarId CountingBp::getRepresentative (VarId vid) { - assert (Util::contains (vid2VarCluster_, vid)); - VarCluster* vc = vid2VarCluster_.find (vid)->second; + assert (Util::contains (varClusterMap_, vid)); + VarCluster* vc = varClusterMap_.find (vid)->second; return vc->representative()->varId(); } diff --git a/packages/CLPBN/horus/CountingBp.h b/packages/CLPBN/horus/CountingBp.h index d54a47fee..7bc45c632 100644 --- a/packages/CLPBN/horus/CountingBp.h +++ b/packages/CLPBN/horus/CountingBp.h @@ -10,8 +10,6 @@ class VarCluster; class FacCluster; -class VarSignHash; -class FacSignHash; class WeightedBp; typedef long Color; @@ -22,40 +20,44 @@ typedef vector FacSignature; typedef unordered_map DistColorMap; typedef unordered_map VarColorMap; -typedef unordered_map VarSignMap; -typedef unordered_map FacSignMap; +typedef unordered_map VarSignMap; +typedef unordered_map FacSignMap; + +typedef unordered_map VarClusterMap; typedef vector VarClusters; typedef vector FacClusters; -typedef unordered_map VarId2VarCluster; - - -struct VarSignHash +template +inline size_t hash_combine (size_t seed, const T& v) { - size_t operator() (const VarSignature &sig) const - { - size_t val = hash()(sig.size()); - for (size_t i = 0; i < sig.size(); i++) { - val ^= hash()(sig[i].first); - val ^= hash()(sig[i].second); - } - return val; - } -}; + return seed ^ (hash()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); +} -struct FacSignHash -{ - size_t operator() (const FacSignature &sig) const +namespace std { + template struct hash> { - size_t val = hash()(sig.size()); - for (size_t i = 0; i < sig.size(); i++) { - val ^= hash()(sig[i]); + size_t operator() (const std::pair& p) const + { + return hash_combine (std::hash()(p.first), p.second); } - return val; - } -}; + }; + + template struct hash> + { + size_t operator() (const std::vector& vec) const + { + size_t h = 0; + typename vector::const_iterator first = vec.begin(); + typename vector::const_iterator last = vec.end(); + for (; first != last; ++first) { + h = hash_combine (h, *first); + } + return h; + } + }; +} class VarCluster @@ -72,8 +74,8 @@ class VarCluster void setRepresentative (VarNode* vn) { repr_ = vn; } private: - VarNodes members_; - VarNode* repr_; + VarNodes members_; + VarNode* repr_; }; @@ -86,17 +88,17 @@ class FacCluster const FacNode* first (void) const { return members_.front(); } const FacNodes& members (void) const { return members_; } - - VarClusters& varClusters (void) { return varClusters_; } - + FacNode* representative (void) const { return repr_; } void setRepresentative (FacNode* fn) { repr_ = fn; } + + VarClusters& varClusters (void) { return varClusters_; } private: FacNodes members_; - VarClusters varClusters_; FacNode* repr_; + VarClusters varClusters_; }; @@ -171,9 +173,9 @@ class CountingBp : public Solver Colors facColors_; VarClusters varClusters_; FacClusters facClusters_; - VarId2VarCluster vid2VarCluster_; + VarClusterMap varClusterMap_; const FactorGraph* compressedFg_; - WeightedBp* solver_; + WeightedBp* solver_; }; #endif // HORUS_COUNTINGBP_H diff --git a/packages/CLPBN/horus/ElimGraph.cpp b/packages/CLPBN/horus/ElimGraph.cpp index 60c88ce3d..50870d1b6 100644 --- a/packages/CLPBN/horus/ElimGraph.cpp +++ b/packages/CLPBN/horus/ElimGraph.cpp @@ -184,26 +184,43 @@ ElimGraph::getLowestCostNode (void) const { EgNode* bestNode = 0; unsigned minCost = std::numeric_limits::max(); - unsigned cost = 0; EGNeighs::const_iterator it; switch (elimHeuristic) { case MIN_NEIGHBORS: { for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) { - cost = getNeighborsCost (*it); + unsigned cost = getNeighborsCost (*it); if (cost < minCost) { bestNode = *it; minCost = cost; } }} break; - case MIN_WEIGHT: - //cost = getWeightCost (unmarked_[i]); + case MIN_WEIGHT: { + for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) { + unsigned cost = getWeightCost (*it); + if (cost < minCost) { + bestNode = *it; + minCost = cost; + } + }} break; - case MIN_FILL: - //cost = getFillCost (unmarked_[i]); + case MIN_FILL: { + for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) { + unsigned cost = getFillCost (*it); + if (cost < minCost) { + bestNode = *it; + minCost = cost; + } + }} break; - case WEIGHTED_MIN_FILL: - //cost = getWeightedFillCost (unmarked_[i]); + case WEIGHTED_MIN_FILL: { + for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) { + unsigned cost = getWeightedFillCost (*it); + if (cost < minCost) { + bestNode = *it; + minCost = cost; + } + }} break; default: assert (false); diff --git a/packages/CLPBN/horus/ElimGraph.h b/packages/CLPBN/horus/ElimGraph.h index cd77e0b01..2f4d60d15 100644 --- a/packages/CLPBN/horus/ElimGraph.h +++ b/packages/CLPBN/horus/ElimGraph.h @@ -130,9 +130,9 @@ class ElimGraph void connectAllNeighbors (const EgNode*); - vector nodes_; - TinySet unmarked_; - unordered_map varMap_; + vector nodes_; + TinySet unmarked_; + unordered_map varMap_; }; #endif // HORUS_ELIMGRAPH_H diff --git a/packages/CLPBN/horus/TODO b/packages/CLPBN/horus/TODO index 367c16231..9f2dfa108 100644 --- a/packages/CLPBN/horus/TODO +++ b/packages/CLPBN/horus/TODO @@ -1,5 +1,3 @@ - Find a way to decrease the time required to find an elimination order for variable elimination -- Consider using hashs instead of vectors of colors to calculate the groups in - counting bp