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:
- Lifted Variable Elimination
- Lifted First-Order Belief Propagation
********************************************************/
:- module(clpbn_horus_lifted,

View File

@ -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();
}

View File

@ -10,8 +10,6 @@
class VarCluster;
class FacCluster;
class VarSignHash;
class FacSignHash;
class WeightedBp;
typedef long Color;
@ -22,40 +20,44 @@ typedef vector<Color> FacSignature;
typedef unordered_map<unsigned, Color> DistColorMap;
typedef unordered_map<unsigned, Colors> VarColorMap;
typedef unordered_map<VarSignature, VarNodes, VarSignHash> VarSignMap;
typedef unordered_map<FacSignature, FacNodes, FacSignHash> FacSignMap;
typedef unordered_map<VarSignature, VarNodes> VarSignMap;
typedef unordered_map<FacSignature, FacNodes> FacSignMap;
typedef unordered_map<VarId, VarCluster*> VarClusterMap;
typedef vector<VarCluster*> VarClusters;
typedef vector<FacCluster*> FacClusters;
typedef unordered_map<VarId, VarCluster*> VarId2VarCluster;
struct VarSignHash
template <class T>
inline size_t hash_combine (size_t seed, const T& v)
{
size_t operator() (const VarSignature &sig) const
{
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;
}
};
return seed ^ (hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
struct FacSignHash
{
size_t operator() (const FacSignature &sig) const
namespace std {
template <typename T1, typename T2> struct hash<std::pair<T1,T2>>
{
size_t val = hash<size_t>()(sig.size());
for (size_t i = 0; i < sig.size(); i++) {
val ^= hash<size_t>()(sig[i]);
size_t operator() (const std::pair<T1,T2>& p) const
{
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
@ -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

View File

@ -184,26 +184,43 @@ ElimGraph::getLowestCostNode (void) const
{
EgNode* bestNode = 0;
unsigned minCost = std::numeric_limits<unsigned>::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);

View File

@ -130,9 +130,9 @@ class ElimGraph
void connectAllNeighbors (const EgNode*);
vector<EgNode*> nodes_;
TinySet<EgNode*> unmarked_;
unordered_map<VarId, EgNode*> varMap_;
vector<EgNode*> nodes_;
TinySet<EgNode*> unmarked_;
unordered_map<VarId, EgNode*> varMap_;
};
#endif // HORUS_ELIMGRAPH_H

View File

@ -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