This commit is contained in:
Vitor Santos Costa 2012-07-03 16:03:27 +01:00
commit b4b1e68c35
6 changed files with 70 additions and 53 deletions

View File

@ -2,7 +2,7 @@
Interface to Horus Lifted Solvers. Used by: Interface to Horus Lifted Solvers. Used by:
- Lifted Variable Elimination - Lifted Variable Elimination
- Lifted First-Order Belief Propagation
********************************************************/ ********************************************************/
:- module(clpbn_horus_lifted, :- module(clpbn_horus_lifted,

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;
namespace std {
template <typename T1, typename T2> struct hash<std::pair<T1,T2>>
{
size_t operator() (const std::pair<T1,T2>& p) const
{
return hash_combine (std::hash<T1>()(p.first), p.second);
} }
}; };
template <typename T> struct hash<std::vector<T>>
struct FacSignHash
{ {
size_t operator() (const FacSignature &sig) const size_t operator() (const std::vector<T>& vec) const
{ {
size_t val = hash<size_t>()(sig.size()); size_t h = 0;
for (size_t i = 0; i < sig.size(); i++) { typename vector<T>::const_iterator first = vec.begin();
val ^= hash<size_t>()(sig[i]); typename vector<T>::const_iterator last = vec.end();
for (; first != last; ++first) {
h = hash_combine (h, *first);
} }
return val; return h;
} }
}; };
}
class VarCluster class VarCluster
@ -87,16 +89,16 @@ class FacCluster
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,7 +173,7 @@ 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_;
}; };

View File

@ -184,26 +184,43 @@ ElimGraph::getLowestCostNode (void) const
{ {
EgNode* bestNode = 0; EgNode* bestNode = 0;
unsigned minCost = std::numeric_limits<unsigned>::max(); unsigned minCost = std::numeric_limits<unsigned>::max();
unsigned cost = 0;
EGNeighs::const_iterator it; EGNeighs::const_iterator it;
switch (elimHeuristic) { switch (elimHeuristic) {
case MIN_NEIGHBORS: { case MIN_NEIGHBORS: {
for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) { for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) {
cost = getNeighborsCost (*it); unsigned cost = getNeighborsCost (*it);
if (cost < minCost) { if (cost < minCost) {
bestNode = *it; bestNode = *it;
minCost = cost; minCost = cost;
} }
}} }}
break; break;
case MIN_WEIGHT: case MIN_WEIGHT: {
//cost = getWeightCost (unmarked_[i]); for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) {
unsigned cost = getWeightCost (*it);
if (cost < minCost) {
bestNode = *it;
minCost = cost;
}
}}
break; break;
case MIN_FILL: case MIN_FILL: {
//cost = getFillCost (unmarked_[i]); for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) {
unsigned cost = getFillCost (*it);
if (cost < minCost) {
bestNode = *it;
minCost = cost;
}
}}
break; break;
case WEIGHTED_MIN_FILL: case WEIGHTED_MIN_FILL: {
//cost = getWeightedFillCost (unmarked_[i]); for (it = unmarked_.begin(); it != unmarked_.end(); ++ it) {
unsigned cost = getWeightedFillCost (*it);
if (cost < minCost) {
bestNode = *it;
minCost = cost;
}
}}
break; break;
default: default:
assert (false); assert (false);

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