#ifndef YAP_PACKAGES_CLPBN_HORUS_COUNTINGBP_H_ #define YAP_PACKAGES_CLPBN_HORUS_COUNTINGBP_H_ #include #include #include "GroundSolver.h" #include "FactorGraph.h" #include "Horus.h" namespace Horus { class VarCluster; class FacCluster; class WeightedBp; template inline size_t hash_combine (size_t seed, const T& v) { return seed ^ (std::hash()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); } } // namespace Horus namespace std { template struct hash> { size_t operator() (const std::pair& p) const { return Horus::hash_combine (std::hash()(p.first), p.second); }}; template struct hash> { size_t operator() (const std::vector& vec) const { size_t h = 0; typename std::vector::const_iterator first = vec.begin(); typename std::vector::const_iterator last = vec.end(); for (; first != last; ++first) { h = Horus::hash_combine (h, *first); } return h; } }; } // namespace std namespace Horus { class CountingBp : public GroundSolver { public: CountingBp (const FactorGraph& fg); ~CountingBp (void); void printSolverFlags (void) const; Params solveQuery (VarIds); static void setFindIdenticalFactorsFlag (bool fif) { fif_ = fif; } private: typedef long Color; typedef std::vector Colors; typedef std::vector> VarSignature; typedef std::vector FacSignature; typedef std::vector VarClusters; typedef std::vector FacClusters; typedef std::unordered_map DistColorMap; typedef std::unordered_map VarColorMap; typedef std::unordered_map VarSignMap; typedef std::unordered_map FacSignMap; typedef std::unordered_map VarClusterMap; Color getNewColor (void); Color getColor (const VarNode* vn) const; Color getColor (const FacNode* fn) const; void setColor (const VarNode* vn, Color c); void setColor (const FacNode* fn, Color c); void findIdenticalFactors (void); void setInitialColors (void); void createGroups (void); void createClusters (const VarSignMap&, const FacSignMap&); VarSignature getSignature (const VarNode*); FacSignature getSignature (const FacNode*); void printGroups (const VarSignMap&, const FacSignMap&) const; VarId getRepresentative (VarId vid); FacNode* getRepresentative (FacNode*); FactorGraph* getCompressedFactorGraph (void); std::vector> getWeights (void) const; unsigned getWeight (const FacCluster*, const VarCluster*, size_t index) const; Color freeColor_; Colors varColors_; Colors facColors_; VarClusters varClusters_; FacClusters facClusters_; VarClusterMap varClusterMap_; const FactorGraph* compressedFg_; WeightedBp* solver_; static bool fif_; DISALLOW_COPY_AND_ASSIGN (CountingBp); }; inline CountingBp::Color CountingBp::getNewColor (void) { ++ freeColor_; return freeColor_ - 1; } inline CountingBp::Color CountingBp::getColor (const VarNode* vn) const { return varColors_[vn->getIndex()]; } inline CountingBp::Color CountingBp::getColor (const FacNode* fn) const { return facColors_[fn->getIndex()]; } inline void CountingBp::setColor (const VarNode* vn, CountingBp::Color c) { varColors_[vn->getIndex()] = c; } inline void CountingBp::setColor (const FacNode* fn, Color c) { facColors_[fn->getIndex()] = c; } } // namespace Horus #endif // YAP_PACKAGES_CLPBN_HORUS_COUNTINGBP_H_