improve the way we compute hashs for counting bp

This commit is contained in:
Tiago Gomes 2012-06-30 19:25:29 +01:00
parent 5e2d9acc4f
commit 7a3d39551b
3 changed files with 41 additions and 41 deletions

View File

@ -236,7 +236,7 @@ CountingBp::createClusters (
const VarNodes& groupVars = it->second; const VarNodes& groupVars = it->second;
VarCluster* vc = new VarCluster (groupVars); VarCluster* vc = new VarCluster (groupVars);
for (size_t i = 0; i < groupVars.size(); i++) { 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); varClusters_.push_back (vc);
} }
@ -250,7 +250,7 @@ CountingBp::createClusters (
varClusters.reserve (neighs.size()); varClusters.reserve (neighs.size());
for (size_t i = 0; i < neighs.size(); i++) { for (size_t i = 0; i < neighs.size(); i++) {
VarId vid = neighs[i]->varId(); 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)); facClusters_.push_back (new FacCluster (it->second, varClusters));
} }
@ -294,8 +294,8 @@ CountingBp::getSignature (const FacNode* facNode)
VarId VarId
CountingBp::getRepresentative (VarId vid) CountingBp::getRepresentative (VarId vid)
{ {
assert (Util::contains (vid2VarCluster_, vid)); assert (Util::contains (varClusterMap_, vid));
VarCluster* vc = vid2VarCluster_.find (vid)->second; VarCluster* vc = varClusterMap_.find (vid)->second;
return vc->representative()->varId(); return vc->representative()->varId();
} }

View File

@ -10,8 +10,6 @@
class VarCluster; class VarCluster;
class FacCluster; class FacCluster;
class VarSignHash;
class FacSignHash;
class WeightedBp; class WeightedBp;
typedef long Color; typedef long Color;
@ -22,40 +20,44 @@ typedef vector<Color> FacSignature;
typedef unordered_map<unsigned, Color> DistColorMap; typedef unordered_map<unsigned, Color> DistColorMap;
typedef unordered_map<unsigned, Colors> VarColorMap; typedef unordered_map<unsigned, Colors> VarColorMap;
typedef unordered_map<VarSignature, VarNodes, VarSignHash> VarSignMap; typedef unordered_map<VarSignature, VarNodes> VarSignMap;
typedef unordered_map<FacSignature, FacNodes, FacSignHash> FacSignMap; typedef unordered_map<FacSignature, FacNodes> FacSignMap;
typedef unordered_map<VarId, VarCluster*> VarClusterMap;
typedef vector<VarCluster*> VarClusters; typedef vector<VarCluster*> VarClusters;
typedef vector<FacCluster*> FacClusters; typedef vector<FacCluster*> FacClusters;
typedef unordered_map<VarId, VarCluster*> VarId2VarCluster; template <class T>
inline size_t hash_combine (size_t seed, const T& v)
struct VarSignHash
{ {
size_t operator() (const VarSignature &sig) const return seed ^ (hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
{ }
size_t val = hash<size_t>()(sig.size());
for (size_t i = 0; i < sig.size(); i++) {
val ^= hash<size_t>()(sig[i].first);
val ^= hash<size_t>()(sig[i].second);
}
return val;
}
};
struct FacSignHash namespace std {
{ template <typename T1, typename T2> struct hash<std::pair<T1,T2>>
size_t operator() (const FacSignature &sig) const
{ {
size_t val = hash<size_t>()(sig.size()); size_t operator() (const std::pair<T1,T2>& p) const
for (size_t i = 0; i < sig.size(); i++) { {
val ^= hash<size_t>()(sig[i]); return hash_combine (std::hash<T1>()(p.first), p.second);
} }
return val; };
}
}; template <typename T> struct hash<std::vector<T>>
{
size_t operator() (const std::vector<T>& vec) const
{
size_t h = 0;
typename vector<T>::const_iterator first = vec.begin();
typename vector<T>::const_iterator last = vec.end();
for (; first != last; ++first) {
h = hash_combine (h, *first);
}
return h;
}
};
}
class VarCluster class VarCluster
@ -72,8 +74,8 @@ class VarCluster
void setRepresentative (VarNode* vn) { repr_ = vn; } void setRepresentative (VarNode* vn) { repr_ = vn; }
private: private:
VarNodes members_; VarNodes members_;
VarNode* repr_; VarNode* repr_;
}; };
@ -86,17 +88,17 @@ class FacCluster
const FacNode* first (void) const { return members_.front(); } const FacNode* first (void) const { return members_.front(); }
const FacNodes& members (void) const { return members_; } const FacNodes& members (void) const { return members_; }
VarClusters& varClusters (void) { return varClusters_; }
FacNode* representative (void) const { return repr_; } FacNode* representative (void) const { return repr_; }
void setRepresentative (FacNode* fn) { repr_ = fn; } void setRepresentative (FacNode* fn) { repr_ = fn; }
VarClusters& varClusters (void) { return varClusters_; }
private: private:
FacNodes members_; FacNodes members_;
VarClusters varClusters_;
FacNode* repr_; FacNode* repr_;
VarClusters varClusters_;
}; };
@ -171,9 +173,9 @@ class CountingBp : public Solver
Colors facColors_; Colors facColors_;
VarClusters varClusters_; VarClusters varClusters_;
FacClusters facClusters_; FacClusters facClusters_;
VarId2VarCluster vid2VarCluster_; VarClusterMap varClusterMap_;
const FactorGraph* compressedFg_; const FactorGraph* compressedFg_;
WeightedBp* solver_; WeightedBp* solver_;
}; };
#endif // HORUS_COUNTINGBP_H #endif // HORUS_COUNTINGBP_H

View File

@ -1,5 +1,3 @@
- Find a way to decrease the time required to find an - Find a way to decrease the time required to find an
elimination order for variable elimination elimination order for variable elimination
- Consider using hashs instead of vectors of colors to calculate the groups in
counting bp