From 7a3d39551b1946bd26a6357e770c526ed1ab415a Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Sat, 30 Jun 2012 19:25:29 +0100 Subject: [PATCH] improve the way we compute hashs for counting bp --- packages/CLPBN/horus/CountingBp.cpp | 8 ++-- packages/CLPBN/horus/CountingBp.h | 72 +++++++++++++++-------------- packages/CLPBN/horus/TODO | 2 - 3 files changed, 41 insertions(+), 41 deletions(-) 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/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