From 42a5bc493a118f4f4837dbd9bf21c06234268b05 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Wed, 6 Feb 2013 00:24:02 +0000 Subject: [PATCH] Move methods with more than two lines to outside of class definition --- packages/CLPBN/horus/BayesBall.cpp | 17 + packages/CLPBN/horus/BayesBall.h | 12 +- packages/CLPBN/horus/BayesBallGraph.h | 4 +- packages/CLPBN/horus/BeliefProp.cpp | 90 +++++ packages/CLPBN/horus/BeliefProp.h | 89 ++--- packages/CLPBN/horus/ConstraintTree.cpp | 11 + packages/CLPBN/horus/ConstraintTree.h | 68 ++-- packages/CLPBN/horus/CountingBp.h | 106 +++-- packages/CLPBN/horus/ElimGraph.h | 136 ++++--- packages/CLPBN/horus/Factor.h | 452 ++++++++++++--------- packages/CLPBN/horus/FactorGraph.h | 15 +- packages/CLPBN/horus/GroundSolver.h | 1 + packages/CLPBN/horus/Histogram.cpp | 16 +- packages/CLPBN/horus/Histogram.h | 4 +- packages/CLPBN/horus/Indexer.h | 449 +++++++++++++-------- packages/CLPBN/horus/LiftedUtils.h | 138 ++++--- packages/CLPBN/horus/LiftedWCNF.h | 22 +- packages/CLPBN/horus/Parfactor.h | 2 +- packages/CLPBN/horus/ProbFormula.cpp | 16 + packages/CLPBN/horus/ProbFormula.h | 17 +- packages/CLPBN/horus/TinySet.h | 503 +++++++++++++++--------- packages/CLPBN/horus/Util.h | 2 + packages/CLPBN/horus/Var.cpp | 35 ++ packages/CLPBN/horus/Var.h | 75 ++-- packages/CLPBN/horus/WeightedBp.h | 18 +- 25 files changed, 1442 insertions(+), 856 deletions(-) diff --git a/packages/CLPBN/horus/BayesBall.cpp b/packages/CLPBN/horus/BayesBall.cpp index da0c73ff5..01da5edc0 100644 --- a/packages/CLPBN/horus/BayesBall.cpp +++ b/packages/CLPBN/horus/BayesBall.cpp @@ -3,6 +3,23 @@ #include "BayesBall.h" +BayesBall::BayesBall (FactorGraph& fg) + : fg_(fg) , dag_(fg.getStructure()) +{ + dag_.clear(); +} + + + +FactorGraph* +BayesBall::getMinimalFactorGraph (FactorGraph& fg, VarIds vids) +{ + BayesBall bb (fg); + return bb.getMinimalFactorGraph (vids); +} + + + FactorGraph* BayesBall::getMinimalFactorGraph (const VarIds& queryIds) { diff --git a/packages/CLPBN/horus/BayesBall.h b/packages/CLPBN/horus/BayesBall.h index 2057b6f01..af590fd7f 100644 --- a/packages/CLPBN/horus/BayesBall.h +++ b/packages/CLPBN/horus/BayesBall.h @@ -29,19 +29,11 @@ typedef queue> Scheduling; class BayesBall { public: - BayesBall (FactorGraph& fg) - : fg_(fg) , dag_(fg.getStructure()) - { - dag_.clear(); - } + BayesBall (FactorGraph& fg); FactorGraph* getMinimalFactorGraph (const VarIds&); - static FactorGraph* getMinimalFactorGraph (FactorGraph& fg, VarIds vids) - { - BayesBall bb (fg); - return bb.getMinimalFactorGraph (vids); - } + static FactorGraph* getMinimalFactorGraph (FactorGraph& fg, VarIds vids); private: diff --git a/packages/CLPBN/horus/BayesBallGraph.h b/packages/CLPBN/horus/BayesBallGraph.h index eb44f0ae8..de2da8310 100644 --- a/packages/CLPBN/horus/BayesBallGraph.h +++ b/packages/CLPBN/horus/BayesBallGraph.h @@ -56,6 +56,8 @@ class BayesBallGraph public: BayesBallGraph (void) { } + bool empty (void) const { return nodes_.empty(); } + void addNode (BBNode* n); void addEdge (VarId vid1, VarId vid2); @@ -64,8 +66,6 @@ class BayesBallGraph BBNode* getNode (VarId vid); - bool empty (void) const { return nodes_.empty(); } - void setIndexes (void); void clear (void); diff --git a/packages/CLPBN/horus/BeliefProp.cpp b/packages/CLPBN/horus/BeliefProp.cpp index 5ec3aafd5..0622869bf 100644 --- a/packages/CLPBN/horus/BeliefProp.cpp +++ b/packages/CLPBN/horus/BeliefProp.cpp @@ -9,11 +9,61 @@ #include "Horus.h" +BpLink::BpLink (FacNode* fn, VarNode* vn) +{ + fac_ = fn; + var_ = vn; + v1_.resize (vn->range(), LogAware::log (1.0 / vn->range())); + v2_.resize (vn->range(), LogAware::log (1.0 / vn->range())); + currMsg_ = &v1_; + nextMsg_ = &v2_; + residual_ = 0.0; +} + + + +void +BpLink::clearResidual (void) +{ + residual_ = 0.0; +} + + + +void +BpLink::updateResidual (void) +{ + residual_ = LogAware::getMaxNorm (v1_, v2_); +} + + + +void +BpLink::updateMessage (void) +{ + swap (currMsg_, nextMsg_); +} + + + +string +BpLink::toString (void) const +{ + stringstream ss; + ss << fac_->getLabel(); + ss << " -- " ; + ss << var_->label(); + return ss.str(); +} + + + double BeliefProp::accuracy_ = 0.0001; unsigned BeliefProp::maxIter_ = 1000; MsgSchedule BeliefProp::schedule_ = MsgSchedule::SEQ_FIXED; + BeliefProp::BeliefProp (const FactorGraph& fg) : GroundSolver (fg) { runned_ = false; @@ -152,6 +202,46 @@ BeliefProp::getFactorJoint ( +void +BeliefProp::calculateAndUpdateMessage (BpLink* link, bool calcResidual) +{ + if (Globals::verbosity > 2) { + cout << "calculating & updating " << link->toString() << endl; + } + calcFactorToVarMsg (link); + if (calcResidual) { + link->updateResidual(); + } + link->updateMessage(); +} + + + +void +BeliefProp::calculateMessage (BpLink* link, bool calcResidual) +{ + if (Globals::verbosity > 2) { + cout << "calculating " << link->toString() << endl; + } + calcFactorToVarMsg (link); + if (calcResidual) { + link->updateResidual(); + } +} + + + +void +BeliefProp::updateMessage (BpLink* link) +{ + link->updateMessage(); + if (Globals::verbosity > 2) { + cout << "updating " << link->toString() << endl; + } +} + + + void BeliefProp::runSolver (void) { diff --git a/packages/CLPBN/horus/BeliefProp.h b/packages/CLPBN/horus/BeliefProp.h index 5fad0c496..c1b59b476 100644 --- a/packages/CLPBN/horus/BeliefProp.h +++ b/packages/CLPBN/horus/BeliefProp.h @@ -24,16 +24,7 @@ enum MsgSchedule { class BpLink { public: - BpLink (FacNode* fn, VarNode* vn) - { - fac_ = fn; - var_ = vn; - v1_.resize (vn->range(), LogAware::log (1.0 / vn->range())); - v2_.resize (vn->range(), LogAware::log (1.0 / vn->range())); - currMsg_ = &v1_; - nextMsg_ = &v2_; - residual_ = 0.0; - } + BpLink (FacNode* fn, VarNode* vn); virtual ~BpLink (void) { }; @@ -47,26 +38,13 @@ class BpLink double residual (void) const { return residual_; } - void clearResidual (void) { residual_ = 0.0; } + void clearResidual (void); - void updateResidual (void) - { - residual_ = LogAware::getMaxNorm (v1_, v2_); - } + void updateResidual (void); - virtual void updateMessage (void) - { - swap (currMsg_, nextMsg_); - } + virtual void updateMessage (void); - string toString (void) const - { - stringstream ss; - ss << fac_->getLabel(); - ss << " -- " ; - ss << var_->label(); - return ss.str(); - } + string toString (void) const; protected: FacNode* fac_; @@ -126,46 +104,15 @@ class BeliefProp : public GroundSolver static void setMsgSchedule (MsgSchedule sch) { schedule_ = sch; } protected: - SPNodeInfo* ninf (const VarNode* var) const - { - return varsI_[var->getIndex()]; - } + SPNodeInfo* ninf (const VarNode* var) const; - SPNodeInfo* ninf (const FacNode* fac) const - { - return facsI_[fac->getIndex()]; - } + SPNodeInfo* ninf (const FacNode* fac) const; - void calculateAndUpdateMessage (BpLink* link, bool calcResidual = true) - { - if (Globals::verbosity > 2) { - cout << "calculating & updating " << link->toString() << endl; - } - calcFactorToVarMsg (link); - if (calcResidual) { - link->updateResidual(); - } - link->updateMessage(); - } + void calculateAndUpdateMessage (BpLink* link, bool calcResidual = true); - void calculateMessage (BpLink* link, bool calcResidual = true) - { - if (Globals::verbosity > 2) { - cout << "calculating " << link->toString() << endl; - } - calcFactorToVarMsg (link); - if (calcResidual) { - link->updateResidual(); - } - } + void calculateMessage (BpLink* link, bool calcResidual = true); - void updateMessage (BpLink* link) - { - link->updateMessage(); - if (Globals::verbosity > 2) { - cout << "updating " << link->toString() << endl; - } - } + void updateMessage (BpLink* link); struct CompareResidual { @@ -213,5 +160,21 @@ class BeliefProp : public GroundSolver DISALLOW_COPY_AND_ASSIGN (BeliefProp); }; + + +inline SPNodeInfo* +BeliefProp::ninf (const VarNode* var) const +{ + return varsI_[var->getIndex()]; +} + + + +inline SPNodeInfo* +BeliefProp::ninf (const FacNode* fac) const +{ + return facsI_[fac->getIndex()]; +} + #endif // HORUS_BELIEFPROP_H diff --git a/packages/CLPBN/horus/ConstraintTree.cpp b/packages/CLPBN/horus/ConstraintTree.cpp index 599d28f37..6866d20b4 100644 --- a/packages/CLPBN/horus/ConstraintTree.cpp +++ b/packages/CLPBN/horus/ConstraintTree.cpp @@ -216,6 +216,17 @@ ConstraintTree::ConstraintTree (const ConstraintTree& ct) +ConstraintTree::ConstraintTree ( + const CTChilds& rootChilds, + const LogVars& logVars) + : root_(new CTNode (0, 0, rootChilds)), + logVars_(logVars), + logVarSet_(logVars) +{ +} + + + ConstraintTree::~ConstraintTree (void) { CTNode::deleteSubtree (root_); diff --git a/packages/CLPBN/horus/ConstraintTree.h b/packages/CLPBN/horus/ConstraintTree.h index 2c0c09464..02de30da9 100644 --- a/packages/CLPBN/horus/ConstraintTree.h +++ b/packages/CLPBN/horus/ConstraintTree.h @@ -59,11 +59,7 @@ class CTNode bool isLeaf (void) const { return childs_.empty(); } - CTChilds_::iterator findSymbol (Symbol symb) - { - CTNode tmp (symb, 0); - return childs_.find (&tmp); - } + CTChilds_::iterator findSymbol (Symbol symb); void mergeSubtree (CTNode*, bool = true); @@ -91,12 +87,21 @@ class CTNode DISALLOW_ASSIGN (CTNode); }; -ostream& operator<< (ostream &out, const CTNode&); - typedef TinySet CTChilds; +inline CTChilds::iterator +CTNode::findSymbol (Symbol symb) +{ + CTNode tmp (symb, 0); + return childs_.find (&tmp); +} + + +ostream& operator<< (ostream &out, const CTNode&); + + class ConstraintTree { public: @@ -110,10 +115,7 @@ class ConstraintTree ConstraintTree (const ConstraintTree&); - ConstraintTree (const CTChilds& rootChilds, const LogVars& logVars) - : root_(new CTNode (0, 0, rootChilds)), - logVars_(logVars), - logVarSet_(logVars) { } + ConstraintTree (const CTChilds& rootChilds, const LogVars& logVars); ~ConstraintTree (void); @@ -121,23 +123,11 @@ class ConstraintTree bool empty (void) const { return root_->childs().empty(); } - const LogVars& logVars (void) const - { - assert (LogVarSet (logVars_) == logVarSet_); - return logVars_; - } + const LogVars& logVars (void) const; - const LogVarSet& logVarSet (void) const - { - assert (LogVarSet (logVars_) == logVarSet_); - return logVarSet_; - } + const LogVarSet& logVarSet (void) const; - size_t nrLogVars (void) const - { - return logVars_.size(); - assert (LogVarSet (logVars_) == logVarSet_); - } + size_t nrLogVars (void) const; void addTuple (const Tuple&); @@ -230,5 +220,31 @@ class ConstraintTree }; + +inline const LogVars& +ConstraintTree::logVars (void) const +{ + assert (LogVarSet (logVars_) == logVarSet_); + return logVars_; +} + + + +inline const LogVarSet& +ConstraintTree::logVarSet (void) const +{ + assert (LogVarSet (logVars_) == logVarSet_); + return logVarSet_; +} + + + +inline size_t +ConstraintTree::nrLogVars (void) const +{ + return logVars_.size(); + assert (LogVarSet (logVars_) == logVarSet_); +} + #endif // HORUS_CONSTRAINTTREE_H diff --git a/packages/CLPBN/horus/CountingBp.h b/packages/CLPBN/horus/CountingBp.h index 605fa8b22..1dc852c38 100644 --- a/packages/CLPBN/horus/CountingBp.h +++ b/packages/CLPBN/horus/CountingBp.h @@ -27,6 +27,7 @@ typedef unordered_map VarClusterMap; typedef vector VarClusters; typedef vector FacClusters; + template inline size_t hash_combine (size_t seed, const T& v) { @@ -35,27 +36,29 @@ inline size_t hash_combine (size_t seed, const T& v) namespace std { - template struct hash> - { - size_t operator() (const std::pair& p) const - { - return hash_combine (std::hash()(p.first), p.second); - } - }; - template struct hash> +template struct hash> +{ + size_t operator() (const std::pair& p) const { - 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; + return hash_combine (std::hash()(p.first), p.second); + } +}; + +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; + } +}; + } @@ -119,31 +122,15 @@ class CountingBp : public GroundSolver static void setFindIdenticalFactorsFlag (bool fif) { fif_ = fif; } private: - Color getNewColor (void) - { - ++ freeColor_; - return freeColor_ - 1; - } + Color getNewColor (void); - Color getColor (const VarNode* vn) const - { - return varColors_[vn->getIndex()]; - } + Color getColor (const VarNode* vn) const; - Color getColor (const FacNode* fn) const - { - return facColors_[fn->getIndex()]; - } + Color getColor (const FacNode* fn) const; - void setColor (const VarNode* vn, Color c) - { - varColors_[vn->getIndex()] = c; - } + void setColor (const VarNode* vn, Color c); - void setColor (const FacNode* fn, Color c) - { - facColors_[fn->getIndex()] = c; - } + void setColor (const FacNode* fn, Color c); void findIdenticalFactors (void); @@ -184,5 +171,46 @@ class CountingBp : public GroundSolver DISALLOW_COPY_AND_ASSIGN (CountingBp); }; + + +inline Color +CountingBp::getNewColor (void) +{ + ++ freeColor_; + return freeColor_ - 1; +} + + + +inline Color +CountingBp::getColor (const VarNode* vn) const +{ + return varColors_[vn->getIndex()]; +} + + + +inline Color +CountingBp::getColor (const FacNode* fn) const +{ + return facColors_[fn->getIndex()]; +} + + + +inline void +CountingBp::setColor (const VarNode* vn, Color c) +{ + varColors_[vn->getIndex()] = c; +} + + + +inline void +CountingBp::setColor (const FacNode* fn, Color c) +{ + facColors_[fn->getIndex()] = c; +} + #endif // HORUS_COUNTINGBP_H diff --git a/packages/CLPBN/horus/ElimGraph.h b/packages/CLPBN/horus/ElimGraph.h index a636d316d..1c822816c 100644 --- a/packages/CLPBN/horus/ElimGraph.h +++ b/packages/CLPBN/horus/ElimGraph.h @@ -63,64 +63,17 @@ class ElimGraph static void setElimHeuristic (ElimHeuristic eh) { elimHeuristic_ = eh; } private: - void addEdge (EgNode* n1, EgNode* n2) - { - assert (n1 != n2); - n1->addNeighbor (n2); - n2->addNeighbor (n1); - } + void addEdge (EgNode* n1, EgNode* n2); - unsigned getNeighborsCost (const EgNode* n) const - { - return n->neighbors().size(); - } + unsigned getNeighborsCost (const EgNode* n) const; - unsigned getWeightCost (const EgNode* n) const - { - unsigned cost = 1; - const EGNeighs& neighs = n->neighbors(); - for (size_t i = 0; i < neighs.size(); i++) { - cost *= neighs[i]->range(); - } - return cost; - } + unsigned getWeightCost (const EgNode* n) const; - unsigned getFillCost (const EgNode* n) const - { - unsigned cost = 0; - const EGNeighs& neighs = n->neighbors(); - if (neighs.size() > 0) { - for (size_t i = 0; i < neighs.size() - 1; i++) { - for (size_t j = i + 1; j < neighs.size(); j++) { - if ( ! neighbors (neighs[i], neighs[j])) { - cost ++; - } - } - } - } - return cost; - } + unsigned getFillCost (const EgNode* n) const; - unsigned getWeightedFillCost (const EgNode* n) const - { - unsigned cost = 0; - const EGNeighs& neighs = n->neighbors(); - if (neighs.size() > 0) { - for (size_t i = 0; i < neighs.size() - 1; i++) { - for (size_t j = i + 1; j < neighs.size(); j++) { - if ( ! neighbors (neighs[i], neighs[j])) { - cost += neighs[i]->range() * neighs[j]->range(); - } - } - } - } - return cost; - } + unsigned getWeightedFillCost (const EgNode* n) const; - bool neighbors (EgNode* n1, EgNode* n2) const - { - return n1->isNeighbor (n2); - } + bool neighbors (EgNode* n1, EgNode* n2) const; void addNode (EgNode*); @@ -139,5 +92,82 @@ class ElimGraph DISALLOW_COPY_AND_ASSIGN (ElimGraph); }; + + +inline void +ElimGraph::addEdge (EgNode* n1, EgNode* n2) +{ + assert (n1 != n2); + n1->addNeighbor (n2); + n2->addNeighbor (n1); +} + + + +inline unsigned +ElimGraph::getNeighborsCost (const EgNode* n) const +{ + return n->neighbors().size(); +} + + + +inline unsigned +ElimGraph::getWeightCost (const EgNode* n) const +{ + unsigned cost = 1; + const EGNeighs& neighs = n->neighbors(); + for (size_t i = 0; i < neighs.size(); i++) { + cost *= neighs[i]->range(); + } + return cost; +} + + + +inline unsigned +ElimGraph::getFillCost (const EgNode* n) const +{ + unsigned cost = 0; + const EGNeighs& neighs = n->neighbors(); + if (neighs.size() > 0) { + for (size_t i = 0; i < neighs.size() - 1; i++) { + for (size_t j = i + 1; j < neighs.size(); j++) { + if ( ! neighbors (neighs[i], neighs[j])) { + cost ++; + } + } + } + } + return cost; +} + + + +inline unsigned +ElimGraph::getWeightedFillCost (const EgNode* n) const +{ + unsigned cost = 0; + const EGNeighs& neighs = n->neighbors(); + if (neighs.size() > 0) { + for (size_t i = 0; i < neighs.size() - 1; i++) { + for (size_t j = i + 1; j < neighs.size(); j++) { + if ( ! neighbors (neighs[i], neighs[j])) { + cost += neighs[i]->range() * neighs[j]->range(); + } + } + } + } + return cost; +} + + + +inline bool +ElimGraph::neighbors (EgNode* n1, EgNode* n2) const +{ + return n1->isNeighbor (n2); +} + #endif // HORUS_ELIMGRAPH_H diff --git a/packages/CLPBN/horus/Factor.h b/packages/CLPBN/horus/Factor.h index ea11d1137..f2b91230b 100644 --- a/packages/CLPBN/horus/Factor.h +++ b/packages/CLPBN/horus/Factor.h @@ -34,172 +34,31 @@ class TFactor void normalize (void) { LogAware::normalize (params_); } - void randomize (void) - { - for (size_t i = 0; i < params_.size(); ++i) { - params_[i] = (double) std::rand() / RAND_MAX; - } - } + void randomize (void); - void setParams (const Params& newParams) - { - params_ = newParams; - assert (params_.size() == Util::sizeExpected (ranges_)); - } + void setParams (const Params& newParams); - size_t indexOf (const T& t) const - { - return Util::indexOf (args_, t); - } + size_t indexOf (const T& t) const; - const T& argument (size_t idx) const - { - assert (idx < args_.size()); - return args_[idx]; - } + const T& argument (size_t idx) const; - T& argument (size_t idx) - { - assert (idx < args_.size()); - return args_[idx]; - } + T& argument (size_t idx); - unsigned range (size_t idx) const - { - assert (idx < ranges_.size()); - return ranges_[idx]; - } + unsigned range (size_t idx) const; - void multiply (TFactor& g) - { - if (args_ == g.arguments()) { - // optimization - Globals::logDomain - ? params_ += g.params() - : params_ *= g.params(); - return; - } - unsigned range_prod = 1; - bool share_arguments = false; - const vector& g_args = g.arguments(); - const Ranges& g_ranges = g.ranges(); - const Params& g_params = g.params(); - for (size_t i = 0; i < g_args.size(); i++) { - size_t idx = indexOf (g_args[i]); - if (idx == args_.size()) { - range_prod *= g_ranges[i]; - args_.push_back (g_args[i]); - ranges_.push_back (g_ranges[i]); - } else { - share_arguments = true; - } - } - if (share_arguments == false) { - // optimization - cartesianProduct (g_params.begin(), g_params.end()); - } else { - extend (range_prod); - Params::iterator it = params_.begin(); - MapIndexer indexer (args_, ranges_, g_args, g_ranges); - if (Globals::logDomain) { - for (; indexer.valid(); ++it, ++indexer) { - *it += g_params[indexer]; - } - } else { - for (; indexer.valid(); ++it, ++indexer) { - *it *= g_params[indexer]; - } - } - } - } + void multiply (TFactor& g); - void sumOutIndex (size_t idx) - { - assert (idx < args_.size()); - assert (args_.size() > 1); - size_t new_size = params_.size() / ranges_[idx]; - Params newps (new_size, LogAware::addIdenty()); - Params::const_iterator first = params_.begin(); - Params::const_iterator last = params_.end(); - MapIndexer indexer (ranges_, idx); - if (Globals::logDomain) { - for (; first != last; ++indexer) { - newps[indexer] = Util::logSum (newps[indexer], *first++); - } - } else { - for (; first != last; ++indexer) { - newps[indexer] += *first++; - } - } - params_ = newps; - args_.erase (args_.begin() + idx); - ranges_.erase (ranges_.begin() + idx); - } + void sumOutIndex (size_t idx); - void absorveEvidence (const T& arg, unsigned obsIdx) - { - size_t idx = indexOf (arg); - assert (idx != args_.size()); - assert (obsIdx < ranges_[idx]); - Params newps; - newps.reserve (params_.size() / ranges_[idx]); - Indexer indexer (ranges_); - for (unsigned i = 0; i < obsIdx; ++i) { - indexer.incrementDimension (idx); - } - while (indexer.valid()) { - newps.push_back (params_[indexer]); - indexer.incrementExceptDimension (idx); - } - params_ = newps; - args_.erase (args_.begin() + idx); - ranges_.erase (ranges_.begin() + idx); - } + void absorveEvidence (const T& arg, unsigned obsIdx); - void reorderArguments (const vector new_args) - { - assert (new_args.size() == args_.size()); - if (new_args == args_) { - return; // already on the desired order - } - Ranges new_ranges; - for (size_t i = 0; i < new_args.size(); i++) { - size_t idx = indexOf (new_args[i]); - assert (idx != args_.size()); - new_ranges.push_back (ranges_[idx]); - } - Params newps; - newps.reserve (params_.size()); - MapIndexer indexer (new_args, new_ranges, args_, ranges_); - for (; indexer.valid(); ++indexer) { - newps.push_back (params_[indexer]); - } - params_ = newps; - args_ = new_args; - ranges_ = new_ranges; - } + void reorderArguments (const vector new_args); - bool contains (const T& arg) const - { - return Util::contains (args_, arg); - } + bool contains (const T& arg) const; - bool contains (const vector& args) const - { - for (size_t i = 0; i < args.size(); i++) { - if (contains (args[i]) == false) { - return false; - } - } - return true; - } - - double& operator[] (size_t idx) - { - assert (idx < params_.size()); - return params_[idx]; - } + bool contains (const vector& args) const; + double& operator[] (size_t idx); protected: vector args_; @@ -208,46 +67,261 @@ class TFactor unsigned distId_; private: - void extend (unsigned range_prod) - { - Params backup = params_; - params_.clear(); - params_.reserve (backup.size() * range_prod); - Params::const_iterator first = backup.begin(); - Params::const_iterator last = backup.end(); - for (; first != last; ++first) { - for (unsigned reps = 0; reps < range_prod; ++reps) { - params_.push_back (*first); - } - } - } + void extend (unsigned range_prod); void cartesianProduct ( - Params::const_iterator first2, - Params::const_iterator last2) - { - Params backup = params_; - params_.clear(); - params_.reserve (params_.size() * (last2 - first2)); - Params::const_iterator first1 = backup.begin(); - Params::const_iterator last1 = backup.end(); - Params::const_iterator tmp; - if (Globals::logDomain) { - for (; first1 != last1; ++first1) { - for (tmp = first2; tmp != last2; ++tmp) { - params_.push_back ((*first1) + (*tmp)); - } - } - } else { - for (; first1 != last1; ++first1) { - for (tmp = first2; tmp != last2; ++tmp) { - params_.push_back ((*first1) * (*tmp)); - } - } + Params::const_iterator first2, Params::const_iterator last2); +}; + + + +template inline void +TFactor::randomize (void) +{ + for (size_t i = 0; i < params_.size(); ++i) { + params_[i] = (double) std::rand() / RAND_MAX; + } +} + + + +template inline void +TFactor::setParams (const Params& newParams) +{ + params_ = newParams; + assert (params_.size() == Util::sizeExpected (ranges_)); +} + + + +template inline size_t +TFactor::indexOf (const T& t) const +{ + return Util::indexOf (args_, t); +} + + + +template inline const T& +TFactor::argument (size_t idx) const +{ + assert (idx < args_.size()); + return args_[idx]; +} + + + +template inline T& +TFactor::argument (size_t idx) +{ + assert (idx < args_.size()); + return args_[idx]; +} + + + +template inline unsigned +TFactor::range (size_t idx) const +{ + assert (idx < ranges_.size()); + return ranges_[idx]; +} + + + +template inline void +TFactor::multiply (TFactor& g) +{ + if (args_ == g.arguments()) { + // optimization + Globals::logDomain + ? params_ += g.params() + : params_ *= g.params(); + return; + } + unsigned range_prod = 1; + bool share_arguments = false; + const vector& g_args = g.arguments(); + const Ranges& g_ranges = g.ranges(); + const Params& g_params = g.params(); + for (size_t i = 0; i < g_args.size(); i++) { + size_t idx = indexOf (g_args[i]); + if (idx == args_.size()) { + range_prod *= g_ranges[i]; + args_.push_back (g_args[i]); + ranges_.push_back (g_ranges[i]); + } else { + share_arguments = true; + } + } + if (share_arguments == false) { + // optimization + cartesianProduct (g_params.begin(), g_params.end()); + } else { + extend (range_prod); + Params::iterator it = params_.begin(); + MapIndexer indexer (args_, ranges_, g_args, g_ranges); + if (Globals::logDomain) { + for (; indexer.valid(); ++it, ++indexer) { + *it += g_params[indexer]; + } + } else { + for (; indexer.valid(); ++it, ++indexer) { + *it *= g_params[indexer]; } } + } +} -}; + + +template inline void +TFactor::sumOutIndex (size_t idx) +{ + assert (idx < args_.size()); + assert (args_.size() > 1); + size_t new_size = params_.size() / ranges_[idx]; + Params newps (new_size, LogAware::addIdenty()); + Params::const_iterator first = params_.begin(); + Params::const_iterator last = params_.end(); + MapIndexer indexer (ranges_, idx); + if (Globals::logDomain) { + for (; first != last; ++indexer) { + newps[indexer] = Util::logSum (newps[indexer], *first++); + } + } else { + for (; first != last; ++indexer) { + newps[indexer] += *first++; + } + } + params_ = newps; + args_.erase (args_.begin() + idx); + ranges_.erase (ranges_.begin() + idx); +} + + + +template inline void +TFactor::absorveEvidence (const T& arg, unsigned obsIdx) +{ + size_t idx = indexOf (arg); + assert (idx != args_.size()); + assert (obsIdx < ranges_[idx]); + Params newps; + newps.reserve (params_.size() / ranges_[idx]); + Indexer indexer (ranges_); + for (unsigned i = 0; i < obsIdx; ++i) { + indexer.incrementDimension (idx); + } + while (indexer.valid()) { + newps.push_back (params_[indexer]); + indexer.incrementExceptDimension (idx); + } + params_ = newps; + args_.erase (args_.begin() + idx); + ranges_.erase (ranges_.begin() + idx); +} + + + +template inline void +TFactor::reorderArguments (const vector new_args) +{ + assert (new_args.size() == args_.size()); + if (new_args == args_) { + return; // already on the desired order + } + Ranges new_ranges; + for (size_t i = 0; i < new_args.size(); i++) { + size_t idx = indexOf (new_args[i]); + assert (idx != args_.size()); + new_ranges.push_back (ranges_[idx]); + } + Params newps; + newps.reserve (params_.size()); + MapIndexer indexer (new_args, new_ranges, args_, ranges_); + for (; indexer.valid(); ++indexer) { + newps.push_back (params_[indexer]); + } + params_ = newps; + args_ = new_args; + ranges_ = new_ranges; +} + + + +template inline bool +TFactor::contains (const T& arg) const +{ + return Util::contains (args_, arg); +} + + + +template inline bool +TFactor::contains (const vector& args) const +{ + for (size_t i = 0; i < args.size(); i++) { + if (contains (args[i]) == false) { + return false; + } + } + return true; +} + + + +template inline double& +TFactor::operator[] (size_t idx) +{ + assert (idx < params_.size()); + return params_[idx]; +} + + + +template inline void +TFactor::extend (unsigned range_prod) +{ + Params backup = params_; + params_.clear(); + params_.reserve (backup.size() * range_prod); + Params::const_iterator first = backup.begin(); + Params::const_iterator last = backup.end(); + for (; first != last; ++first) { + for (unsigned reps = 0; reps < range_prod; ++reps) { + params_.push_back (*first); + } + } +} + + + +template inline void +TFactor::cartesianProduct ( + Params::const_iterator first2, + Params::const_iterator last2) +{ + Params backup = params_; + params_.clear(); + params_.reserve (params_.size() * (last2 - first2)); + Params::const_iterator first1 = backup.begin(); + Params::const_iterator last1 = backup.end(); + Params::const_iterator tmp; + if (Globals::logDomain) { + for (; first1 != last1; ++first1) { + for (tmp = first2; tmp != last2; ++tmp) { + params_.push_back ((*first1) + (*tmp)); + } + } + } else { + for (; first1 != last1; ++first1) { + for (tmp = first2; tmp != last2; ++tmp) { + params_.push_back ((*first1) * (*tmp)); + } + } + } +} diff --git a/packages/CLPBN/horus/FactorGraph.h b/packages/CLPBN/horus/FactorGraph.h index e1cc9277c..5140736c9 100644 --- a/packages/CLPBN/horus/FactorGraph.h +++ b/packages/CLPBN/horus/FactorGraph.h @@ -82,11 +82,7 @@ class FactorGraph size_t nrFacNodes (void) const { return facNodes_.size(); } - VarNode* getVarNode (VarId vid) const - { - VarMap::const_iterator it = varMap_.find (vid); - return it != varMap_.end() ? it->second : 0; - } + VarNode* getVarNode (VarId vid) const; void readFromUaiFormat (const char*); @@ -166,6 +162,15 @@ class FactorGraph +inline VarNode* +FactorGraph::getVarNode (VarId vid) const +{ + VarMap::const_iterator it = varMap_.find (vid); + return it != varMap_.end() ? it->second : 0; +} + + + struct sortByVarId { bool operator()(VarNode* vn1, VarNode* vn2) { diff --git a/packages/CLPBN/horus/GroundSolver.h b/packages/CLPBN/horus/GroundSolver.h index eac28b045..3d5f62c48 100644 --- a/packages/CLPBN/horus/GroundSolver.h +++ b/packages/CLPBN/horus/GroundSolver.h @@ -30,6 +30,7 @@ class GroundSolver protected: const FactorGraph& fg; + private: DISALLOW_COPY_AND_ASSIGN (GroundSolver); }; diff --git a/packages/CLPBN/horus/Histogram.cpp b/packages/CLPBN/horus/Histogram.cpp index d5cf729e9..7243f4a00 100644 --- a/packages/CLPBN/horus/Histogram.cpp +++ b/packages/CLPBN/horus/Histogram.cpp @@ -118,14 +118,6 @@ HistogramSet::getNumAssigns (unsigned N, unsigned R) -ostream& operator<< (ostream &os, const HistogramSet& hs) -{ - os << "#" << hs.hist_; - return os; -} - - - unsigned HistogramSet::maxCount (size_t idx) const { @@ -144,3 +136,11 @@ HistogramSet::clearAfter (size_t idx) std::fill (hist_.begin() + idx + 1, hist_.end(), 0); } + + +ostream& operator<< (ostream &os, const HistogramSet& hs) +{ + os << "#" << hs.hist_; + return os; +} + diff --git a/packages/CLPBN/horus/Histogram.h b/packages/CLPBN/horus/Histogram.h index d60c2d22f..f28c8c0c7 100644 --- a/packages/CLPBN/horus/Histogram.h +++ b/packages/CLPBN/horus/Histogram.h @@ -33,8 +33,6 @@ class HistogramSet static vector getNumAssigns (unsigned, unsigned); - friend std::ostream& operator<< (ostream &os, const HistogramSet& hs); - private: unsigned maxCount (size_t) const; @@ -43,6 +41,8 @@ class HistogramSet unsigned size_; Histogram hist_; + friend std::ostream& operator<< (ostream &os, const HistogramSet& hs); + DISALLOW_COPY_AND_ASSIGN (HistogramSet); }; diff --git a/packages/CLPBN/horus/Indexer.h b/packages/CLPBN/horus/Indexer.h index a4141ebed..180dea2ab 100644 --- a/packages/CLPBN/horus/Indexer.h +++ b/packages/CLPBN/horus/Indexer.h @@ -13,107 +13,30 @@ class Indexer { public: - Indexer (const Ranges& ranges, bool calcOffsets = true) - : index_(0), indices_(ranges.size(), 0), ranges_(ranges), - size_(Util::sizeExpected (ranges)) - { - if (calcOffsets) { - calculateOffsets(); - } - } + Indexer (const Ranges& ranges, bool calcOffsets = true); - void increment (void) - { - for (size_t i = ranges_.size(); i-- > 0; ) { - indices_[i] ++; - if (indices_[i] != ranges_[i]) { - break; - } else { - indices_[i] = 0; - } - } - index_ ++; - } + void increment (void); - void incrementDimension (size_t dim) - { - assert (dim < ranges_.size()); - assert (ranges_.size() == offsets_.size()); - assert (indices_[dim] < ranges_[dim]); - indices_[dim] ++; - index_ += offsets_[dim]; - } + void incrementDimension (size_t dim); - void incrementExceptDimension (size_t dim) - { - assert (ranges_.size() == offsets_.size()); - for (size_t i = ranges_.size(); i-- > 0; ) { - if (i != dim) { - indices_[i] ++; - index_ += offsets_[i]; - if (indices_[i] != ranges_[i]) { - return; - } else { - indices_[i] = 0; - index_ -= offsets_[i] * ranges_[i]; - } - } - } - index_ = size_; - } + void incrementExceptDimension (size_t dim); - Indexer& operator++ (void) - { - increment(); - return *this; - } + Indexer& operator++ (void); - operator size_t (void) const - { - return index_; - } + operator size_t (void) const; - unsigned operator[] (size_t dim) const - { - assert (valid()); - assert (dim < ranges_.size()); - return indices_[dim]; - } + unsigned operator[] (size_t dim) const; - bool valid (void) const - { - return index_ < size_; - } + bool valid (void) const; - void reset (void) - { - std::fill (indices_.begin(), indices_.end(), 0); - index_ = 0; - } + void reset (void); - void resetDimension (size_t dim) - { - indices_[dim] = 0; - index_ -= offsets_[dim] * ranges_[dim]; - } + void resetDimension (size_t dim); - size_t size (void) const - { - return size_ ; - } - - friend std::ostream& operator<< (std::ostream&, const Indexer&); + size_t size (void) const; private: - void calculateOffsets (void) - { - size_t prod = 1; - offsets_.resize (ranges_.size()); - for (size_t i = ranges_.size(); i-- > 0; ) { - offsets_[i] = prod; - prod *= ranges_[i]; - } - } + void calculateOffsets (void); size_t index_; Ranges indices_; @@ -121,11 +44,148 @@ class Indexer size_t size_; vector offsets_; + friend std::ostream& operator<< (std::ostream&, const Indexer&); + DISALLOW_COPY_AND_ASSIGN (Indexer); }; +inline +Indexer::Indexer (const Ranges& ranges, bool calcOffsets) + : index_(0), indices_(ranges.size(), 0), ranges_(ranges), + size_(Util::sizeExpected (ranges)) +{ + if (calcOffsets) { + calculateOffsets(); + } +} + + + +inline void +Indexer::increment (void) +{ + for (size_t i = ranges_.size(); i-- > 0; ) { + indices_[i] ++; + if (indices_[i] != ranges_[i]) { + break; + } else { + indices_[i] = 0; + } + } + index_ ++; +} + + + +inline void +Indexer::incrementDimension (size_t dim) +{ + assert (dim < ranges_.size()); + assert (ranges_.size() == offsets_.size()); + assert (indices_[dim] < ranges_[dim]); + indices_[dim] ++; + index_ += offsets_[dim]; +} + + + +inline void +Indexer::incrementExceptDimension (size_t dim) +{ + assert (ranges_.size() == offsets_.size()); + for (size_t i = ranges_.size(); i-- > 0; ) { + if (i != dim) { + indices_[i] ++; + index_ += offsets_[i]; + if (indices_[i] != ranges_[i]) { + return; + } else { + indices_[i] = 0; + index_ -= offsets_[i] * ranges_[i]; + } + } + } + index_ = size_; +} + + + +inline Indexer& +Indexer::operator++ (void) +{ + increment(); + return *this; +} + + + +inline +Indexer::operator size_t (void) const +{ + return index_; +} + + + +inline unsigned +Indexer::operator[] (size_t dim) const +{ + assert (valid()); + assert (dim < ranges_.size()); + return indices_[dim]; +} + + + +inline bool +Indexer::valid (void) const +{ + return index_ < size_; +} + + + +inline void +Indexer::reset (void) +{ + std::fill (indices_.begin(), indices_.end(), 0); + index_ = 0; +} + + + +inline void +Indexer::resetDimension (size_t dim) +{ + indices_[dim] = 0; + index_ -= offsets_[dim] * ranges_[dim]; +} + + + +inline size_t +Indexer::size (void) const +{ + return size_ ; +} + + + +inline void +Indexer::calculateOffsets (void) +{ + size_t prod = 1; + offsets_.resize (ranges_.size()); + for (size_t i = ranges_.size(); i-- > 0; ) { + offsets_[i] = prod; + prod *= ranges_[i]; + } +} + + + inline std::ostream& operator<< (std::ostream& os, const Indexer& indexer) { @@ -141,99 +201,26 @@ operator<< (std::ostream& os, const Indexer& indexer) class MapIndexer { public: - MapIndexer (const Ranges& ranges, const vector& mask) - : index_(0), indices_(ranges.size(), 0), ranges_(ranges), - valid_(true) - { - size_t prod = 1; - offsets_.resize (ranges.size(), 0); - for (size_t i = ranges.size(); i-- > 0; ) { - if (mask[i]) { - offsets_[i] = prod; - prod *= ranges[i]; - } - } - assert (ranges.size() == mask.size()); - } + MapIndexer (const Ranges& ranges, const vector& mask); - MapIndexer (const Ranges& ranges, size_t dim) - : index_(0), indices_(ranges.size(), 0), ranges_(ranges), - valid_(true) - { - size_t prod = 1; - offsets_.resize (ranges.size(), 0); - for (size_t i = ranges.size(); i-- > 0; ) { - if (i != dim) { - offsets_[i] = prod; - prod *= ranges[i]; - } - } - } + MapIndexer (const Ranges& ranges, size_t dim); - template - MapIndexer ( + template + MapIndexer ( const vector& allArgs, const Ranges& allRanges, const vector& wantedArgs, - const Ranges& wantedRanges) - : index_(0), indices_(allArgs.size(), 0), ranges_(allRanges), - valid_(true) - { - size_t prod = 1; - vector offsets (wantedRanges.size()); - for (size_t i = wantedRanges.size(); i-- > 0; ) { - offsets[i] = prod; - prod *= wantedRanges[i]; - } - offsets_.reserve (allArgs.size()); - for (size_t i = 0; i < allArgs.size(); i++) { - size_t idx = Util::indexOf (wantedArgs, allArgs[i]); - offsets_.push_back (idx != wantedArgs.size() ? offsets[idx] : 0); - } - } + const Ranges& wantedRanges); - MapIndexer& operator++ (void) - { - assert (valid_); - for (size_t i = ranges_.size(); i-- > 0; ) { - indices_[i] ++; - index_ += offsets_[i]; - if (indices_[i] != ranges_[i]) { - return *this; - } else { - indices_[i] = 0; - index_ -= offsets_[i] * ranges_[i]; - } - } - valid_ = false; - return *this; - } + MapIndexer& operator++ (void); - operator size_t (void) const - { - assert (valid()); - return index_; - } + operator size_t (void) const; - unsigned operator[] (size_t dim) const - { - assert (valid()); - assert (dim < ranges_.size()); - return indices_[dim]; - } + unsigned operator[] (size_t dim) const; - bool valid (void) const - { - return valid_; - } + bool valid (void) const; - void reset (void) - { - std::fill (indices_.begin(), indices_.end(), 0); - index_ = 0; - } - - friend std::ostream& operator<< (std::ostream&, const MapIndexer&); + void reset (void); private: size_t index_; @@ -242,11 +229,128 @@ class MapIndexer bool valid_; vector offsets_; + friend std::ostream& operator<< (std::ostream&, const MapIndexer&); + DISALLOW_COPY_AND_ASSIGN (MapIndexer); }; +inline +MapIndexer::MapIndexer (const Ranges& ranges, const vector& mask) + : index_(0), indices_(ranges.size(), 0), ranges_(ranges), + valid_(true) +{ + size_t prod = 1; + offsets_.resize (ranges.size(), 0); + for (size_t i = ranges.size(); i-- > 0; ) { + if (mask[i]) { + offsets_[i] = prod; + prod *= ranges[i]; + } + } + assert (ranges.size() == mask.size()); +} + + + +inline +MapIndexer::MapIndexer (const Ranges& ranges, size_t dim) + : index_(0), indices_(ranges.size(), 0), ranges_(ranges), + valid_(true) +{ + size_t prod = 1; + offsets_.resize (ranges.size(), 0); + for (size_t i = ranges.size(); i-- > 0; ) { + if (i != dim) { + offsets_[i] = prod; + prod *= ranges[i]; + } + } +} + + + +template inline +MapIndexer::MapIndexer ( + const vector& allArgs, + const Ranges& allRanges, + const vector& wantedArgs, + const Ranges& wantedRanges) + : index_(0), indices_(allArgs.size(), 0), ranges_(allRanges), + valid_(true) +{ + size_t prod = 1; + vector offsets (wantedRanges.size()); + for (size_t i = wantedRanges.size(); i-- > 0; ) { + offsets[i] = prod; + prod *= wantedRanges[i]; + } + offsets_.reserve (allArgs.size()); + for (size_t i = 0; i < allArgs.size(); i++) { + size_t idx = Util::indexOf (wantedArgs, allArgs[i]); + offsets_.push_back (idx != wantedArgs.size() ? offsets[idx] : 0); + } +} + + + +inline MapIndexer& +MapIndexer::operator++ (void) +{ + assert (valid_); + for (size_t i = ranges_.size(); i-- > 0; ) { + indices_[i] ++; + index_ += offsets_[i]; + if (indices_[i] != ranges_[i]) { + return *this; + } else { + indices_[i] = 0; + index_ -= offsets_[i] * ranges_[i]; + } + } + valid_ = false; + return *this; +} + + + +inline +MapIndexer::operator size_t (void) const +{ + assert (valid()); + return index_; +} + + + +inline unsigned +MapIndexer::operator[] (size_t dim) const +{ + assert (valid()); + assert (dim < ranges_.size()); + return indices_[dim]; +} + + + +inline bool +MapIndexer::valid (void) const +{ + return valid_; +} + + + +inline void +MapIndexer::reset (void) +{ + std::fill (indices_.begin(), indices_.end(), 0); + index_ = 0; +} + + + inline std::ostream& operator<< (std::ostream &os, const MapIndexer& indexer) { @@ -257,6 +361,5 @@ operator<< (std::ostream &os, const MapIndexer& indexer) return os; } - #endif // HORUS_INDEXER_H diff --git a/packages/CLPBN/horus/LiftedUtils.h b/packages/CLPBN/horus/LiftedUtils.h index 66d5c6e07..f067d5a92 100644 --- a/packages/CLPBN/horus/LiftedUtils.h +++ b/packages/CLPBN/horus/LiftedUtils.h @@ -26,10 +26,10 @@ class Symbol static Symbol invalid (void) { return Symbol(); } - friend ostream& operator<< (ostream &os, const Symbol& s); - private: unsigned id_; + + friend ostream& operator<< (ostream &os, const Symbol& s); }; @@ -42,35 +42,50 @@ class LogVar operator unsigned (void) const { return id_; } - LogVar& operator++ (void) - { - assert (valid()); - id_ ++; - return *this; - } + LogVar& operator++ (void); - bool valid (void) const - { - return id_ != Util::maxUnsigned(); - } - - friend ostream& operator<< (ostream &os, const LogVar& X); + bool valid (void) const; private: unsigned id_; + + friend ostream& operator<< (ostream &os, const LogVar& X); }; + +inline LogVar& +LogVar::operator++ (void) +{ + assert (valid()); + id_ ++; + return *this; +} + + + +inline bool +LogVar::valid (void) const +{ + return id_ != Util::maxUnsigned(); +} + + + namespace std { + template <> struct hash { size_t operator() (const Symbol& s) const { return std::hash() (s); - }}; + } +}; template <> struct hash { size_t operator() (const LogVar& X) const { return std::hash() (X); - }}; + } +}; + }; @@ -87,8 +102,11 @@ ostream& operator<< (ostream &os, const Tuple& t); namespace LiftedUtils { + Symbol getSymbol (const string&); -void printSymbolDictionary (void); + +void printSymbolDictionary (void); + } @@ -108,11 +126,11 @@ class Ground bool isAtom (void) const { return args_.empty(); } - friend ostream& operator<< (ostream &os, const Ground& gr); - private: Symbol functor_; Symbols args_; + + friend ostream& operator<< (ostream &os, const Ground& gr); }; typedef vector Grounds; @@ -122,43 +140,73 @@ typedef vector Grounds; class Substitution { public: - void add (LogVar X_old, LogVar X_new) - { - assert (Util::contains (subs_, X_old) == false); - subs_.insert (make_pair (X_old, X_new)); - } + void add (LogVar X_old, LogVar X_new); - void rename (LogVar X_old, LogVar X_new) - { - assert (Util::contains (subs_, X_old)); - subs_.find (X_old)->second = X_new; - } + void rename (LogVar X_old, LogVar X_new); - LogVar newNameFor (LogVar X) const - { - unordered_map::const_iterator it; - it = subs_.find (X); - if (it != subs_.end()) { - return subs_.find (X)->second; - } - return X; - } + LogVar newNameFor (LogVar X) const; - bool containsReplacementFor (LogVar X) const - { - return Util::contains (subs_, X); - } + bool containsReplacementFor (LogVar X) const; - size_t nrReplacements (void) const { return subs_.size(); } + size_t nrReplacements (void) const; LogVars getDiscardedLogVars (void) const; - friend ostream& operator<< (ostream &os, const Substitution& theta); - private: unordered_map subs_; + friend ostream& operator<< (ostream &os, const Substitution& theta); + }; + + + +inline void +Substitution::add (LogVar X_old, LogVar X_new) +{ + assert (Util::contains (subs_, X_old) == false); + subs_.insert (make_pair (X_old, X_new)); +} + + + +inline void +Substitution::rename (LogVar X_old, LogVar X_new) +{ + assert (Util::contains (subs_, X_old)); + subs_.find (X_old)->second = X_new; +} + + + +inline LogVar +Substitution::newNameFor (LogVar X) const +{ + unordered_map::const_iterator it; + it = subs_.find (X); + if (it != subs_.end()) { + return subs_.find (X)->second; + } + return X; +} + + + +inline bool +Substitution::containsReplacementFor (LogVar X) const +{ + return Util::contains (subs_, X); +} + + + +inline size_t +Substitution::nrReplacements (void) const +{ + return subs_.size(); +} + + #endif // HORUS_LIFTEDUTILS_H diff --git a/packages/CLPBN/horus/LiftedWCNF.h b/packages/CLPBN/horus/LiftedWCNF.h index 619d2c5a5..9da3e1602 100644 --- a/packages/CLPBN/horus/LiftedWCNF.h +++ b/packages/CLPBN/horus/LiftedWCNF.h @@ -51,12 +51,12 @@ class Literal LogVarSet posCountedLvs = LogVarSet(), LogVarSet negCountedLvs = LogVarSet()) const; - friend std::ostream& operator<< (std::ostream &os, const Literal& lit); - private: LiteralId lid_; LogVars logVars_; bool negated_; + + friend std::ostream& operator<< (std::ostream &os, const Literal& lit); }; typedef vector Literals; @@ -138,16 +138,16 @@ class Clause static void deleteClauses (vector& clauses); - friend std::ostream& operator<< (ostream &os, const Clause& clause); - private: LogVarSet getLogVarSetExcluding (size_t idx) const; - Literals literals_; - LogVarSet ipgLvs_; - LogVarSet posCountedLvs_; - LogVarSet negCountedLvs_; - ConstraintTree constr_; + Literals literals_; + LogVarSet ipgLvs_; + LogVarSet posCountedLvs_; + LogVarSet negCountedLvs_; + ConstraintTree constr_; + + friend std::ostream& operator<< (ostream &os, const Clause& clause); DISALLOW_ASSIGN (Clause); }; @@ -185,11 +185,11 @@ class LitLvTypes void setAllFullLogVars (void) { std::fill (lvTypes_.begin(), lvTypes_.end(), LogVarType::FULL_LV); } - friend std::ostream& operator<< (std::ostream &os, const LitLvTypes& lit); - private: LiteralId lid_; LogVarTypes lvTypes_; + + friend std::ostream& operator<< (std::ostream &os, const LitLvTypes& lit); }; typedef TinySet LitLvTypesSet; diff --git a/packages/CLPBN/horus/Parfactor.h b/packages/CLPBN/horus/Parfactor.h index f21fc5f69..f85eab623 100644 --- a/packages/CLPBN/horus/Parfactor.h +++ b/packages/CLPBN/horus/Parfactor.h @@ -110,7 +110,7 @@ class Parfactor : public TFactor static void alignLogicalVars (Parfactor*, Parfactor*); - ConstraintTree* constr_; + ConstraintTree* constr_; DISALLOW_ASSIGN (Parfactor); }; diff --git a/packages/CLPBN/horus/ProbFormula.cpp b/packages/CLPBN/horus/ProbFormula.cpp index 67473734c..e654f09cd 100644 --- a/packages/CLPBN/horus/ProbFormula.cpp +++ b/packages/CLPBN/horus/ProbFormula.cpp @@ -131,6 +131,22 @@ ProbFormula::getNewGroup (void) +ObservedFormula::ObservedFormula (Symbol f, unsigned a, unsigned ev) + : functor_(f), arity_(a), evidence_(ev), constr_(a) +{ + +} + + + +ObservedFormula::ObservedFormula (Symbol f, unsigned ev, const Tuple& tuple) + : functor_(f), arity_(tuple.size()), evidence_(ev), constr_(arity_) +{ + constr_.addTuple (tuple); +} + + + ostream& operator<< (ostream &os, const ObservedFormula& of) { os << of.functor_ << "/" << of.arity_; diff --git a/packages/CLPBN/horus/ProbFormula.h b/packages/CLPBN/horus/ProbFormula.h index 48824b5db..dcb878d64 100644 --- a/packages/CLPBN/horus/ProbFormula.h +++ b/packages/CLPBN/horus/ProbFormula.h @@ -58,10 +58,10 @@ class ProbFormula static PrvGroup getNewGroup (void); - friend std::ostream& operator<< (ostream &os, const ProbFormula& f); - friend bool operator== (const ProbFormula& f1, const ProbFormula& f2); + friend std::ostream& operator<< (ostream &os, const ProbFormula& f); + private: Symbol functor_; LogVars logVars_; @@ -77,14 +77,9 @@ typedef vector ProbFormulas; class ObservedFormula { public: - ObservedFormula (Symbol f, unsigned a, unsigned ev) - : functor_(f), arity_(a), evidence_(ev), constr_(a) { } + ObservedFormula (Symbol f, unsigned a, unsigned ev); - ObservedFormula (Symbol f, unsigned ev, const Tuple& tuple) - : functor_(f), arity_(tuple.size()), evidence_(ev), constr_(arity_) - { - constr_.addTuple (tuple); - } + ObservedFormula (Symbol f, unsigned ev, const Tuple& tuple); Symbol functor (void) const { return functor_; } @@ -100,13 +95,13 @@ class ObservedFormula void addTuple (const Tuple& tuple) { constr_.addTuple (tuple); } - friend ostream& operator<< (ostream &os, const ObservedFormula& of); - private: Symbol functor_; unsigned arity_; unsigned evidence_; ConstraintTree constr_; + + friend ostream& operator<< (ostream &os, const ObservedFormula& of); }; typedef vector ObservedFormulas; diff --git a/packages/CLPBN/horus/TinySet.h b/packages/CLPBN/horus/TinySet.h index f307e4530..57592e565 100644 --- a/packages/CLPBN/horus/TinySet.h +++ b/packages/CLPBN/horus/TinySet.h @@ -25,191 +25,76 @@ class TinySet TinySet (const T& t, const Compare& cmp = Compare()) : vec_(1, t), cmp_(cmp) { } - TinySet (const vector& elements, const Compare& cmp = Compare()) - : vec_(elements), cmp_(cmp) - { - std::sort (begin(), end(), cmp_); - iterator it = unique_cmp (begin(), end()); - vec_.resize (it - begin()); - } + TinySet (const vector& elements, const Compare& cmp = Compare()); - iterator insert (const T& t) - { - iterator it = std::lower_bound (begin(), end(), t, cmp_); - if (it == end() || cmp_(t, *it)) { - vec_.insert (it, t); - } - return it; - } + iterator insert (const T& t); - void insert_sorted (const T& t) - { - vec_.push_back (t); - assert (consistent()); - } + void insert_sorted (const T& t); - void remove (const T& t) - { - iterator it = std::lower_bound (begin(), end(), t, cmp_); - if (it != end()) { - vec_.erase (it); - } - } + void remove (const T& t); - const_iterator find (const T& t) const - { - const_iterator it = std::lower_bound (begin(), end(), t, cmp_); - return it == end() || cmp_(t, *it) ? end() : it; - } - - iterator find (const T& t) - { - iterator it = std::lower_bound (begin(), end(), t, cmp_); - return it == end() || cmp_(t, *it) ? end() : it; - } + const_iterator find (const T& t) const; + iterator find (const T& t); + /* set union */ - TinySet operator| (const TinySet& s) const - { - TinySet res; - std::set_union ( - vec_.begin(), vec_.end(), - s.vec_.begin(), s.vec_.end(), - std::back_inserter (res.vec_), - cmp_); - return res; - } + TinySet operator| (const TinySet& s) const; /* set intersection */ - TinySet operator& (const TinySet& s) const - { - TinySet res; - std::set_intersection ( - vec_.begin(), vec_.end(), - s.vec_.begin(), s.vec_.end(), - std::back_inserter (res.vec_), - cmp_); - return res; - } + TinySet operator& (const TinySet& s) const; /* set difference */ - TinySet operator- (const TinySet& s) const - { - TinySet res; - std::set_difference ( - vec_.begin(), vec_.end(), - s.vec_.begin(), s.vec_.end(), - std::back_inserter (res.vec_), - cmp_); - return res; - } + TinySet operator- (const TinySet& s) const; - TinySet& operator|= (const TinySet& s) - { - return *this = (*this | s); - } + TinySet& operator|= (const TinySet& s); - TinySet& operator&= (const TinySet& s) - { - return *this = (*this & s); - } + TinySet& operator&= (const TinySet& s); - TinySet& operator-= (const TinySet& s) - { - return *this = (*this - s); - } + TinySet& operator-= (const TinySet& s); - bool contains (const T& t) const - { - return std::binary_search ( - vec_.begin(), vec_.end(), t, cmp_); - } + bool contains (const T& t) const; - bool contains (const TinySet& s) const - { - return std::includes ( - vec_.begin(), - vec_.end(), - s.vec_.begin(), - s.vec_.end(), - cmp_); - } + bool contains (const TinySet& s) const; - bool in (const TinySet& s) const - { - return std::includes ( - s.vec_.begin(), - s.vec_.end(), - vec_.begin(), - vec_.end(), - cmp_); - } + bool in (const TinySet& s) const; - bool intersects (const TinySet& s) const - { - return (*this & s).size() > 0; - } + bool intersects (const TinySet& s) const; - const T& operator[] (typename vector::size_type i) const - { - return vec_[i]; - } + const T& operator[] (typename vector::size_type i) const; - T& operator[] (typename vector::size_type i) - { - return vec_[i]; - } + T& operator[] (typename vector::size_type i); - T front (void) const - { - return vec_.front(); - } + T front (void) const; - T& front (void) - { - return vec_.front(); - } + T& front (void); - T back (void) const - { - return vec_.back(); - } + T back (void) const; - T& back (void) - { - return vec_.back(); - } + T& back (void); - const vector& elements (void) const - { - return vec_; - } + const vector& elements (void) const; - bool empty (void) const - { - return vec_.empty(); - } + bool empty (void) const; - typename vector::size_type size (void) const - { - return vec_.size(); - } + typename vector::size_type size (void) const; - void clear (void) - { - vec_.clear(); - } + void clear (void); - void reserve (typename vector::size_type size) - { - vec_.reserve (size); - } + void reserve (typename vector::size_type size); iterator begin (void) { return vec_.begin(); } iterator end (void) { return vec_.end(); } const_iterator begin (void) const { return vec_.begin(); } const_iterator end (void) const { return vec_.end(); } + private: + iterator unique_cmp (iterator first, iterator last); + + bool consistent (void) const; + + vector vec_; + Compare cmp_; + friend bool operator== (const TinySet& s1, const TinySet& s2) { return s1.vec_ == s2.vec_; @@ -231,35 +116,295 @@ class TinySet return out; } - private: - iterator unique_cmp (iterator first, iterator last) - { - if (first == last) { - return last; - } - iterator result = first; - while (++first != last) { - if (cmp_(*result, *first)) { - *(++result) = *first; - } - } - return ++result; - } - - bool consistent (void) const - { - typename vector::size_type i; - for (i = 0; i < vec_.size() - 1; i++) { - if ( ! cmp_(vec_[i], vec_[i + 1])) { - return false; - } - } - return true; - } - - vector vec_; - Compare cmp_; }; + + +template inline +TinySet::TinySet (const vector& elements, const C& cmp) + : vec_(elements), cmp_(cmp) +{ + std::sort (begin(), end(), cmp_); + iterator it = unique_cmp (begin(), end()); + vec_.resize (it - begin()); +} + + + +template inline typename TinySet::iterator +TinySet::insert (const T& t) +{ + iterator it = std::lower_bound (begin(), end(), t, cmp_); + if (it == end() || cmp_(t, *it)) { + vec_.insert (it, t); + } + return it; +} + + + +template inline void +TinySet::insert_sorted (const T& t) +{ + vec_.push_back (t); + assert (consistent()); +} + + + +template inline void +TinySet::remove (const T& t) +{ + iterator it = std::lower_bound (begin(), end(), t, cmp_); + if (it != end()) { + vec_.erase (it); + } +} + + + +template inline typename TinySet::const_iterator +TinySet::find (const T& t) const +{ + const_iterator it = std::lower_bound (begin(), end(), t, cmp_); + return it == end() || cmp_(t, *it) ? end() : it; +} + + + +template inline typename TinySet::iterator +TinySet::find (const T& t) +{ + iterator it = std::lower_bound (begin(), end(), t, cmp_); + return it == end() || cmp_(t, *it) ? end() : it; +} + + + +/* set union */ +template inline TinySet +TinySet::operator| (const TinySet& s) const +{ + TinySet res; + std::set_union ( + vec_.begin(), vec_.end(), + s.vec_.begin(), s.vec_.end(), + std::back_inserter (res.vec_), + cmp_); + return res; +} + + + +/* set intersection */ +template inline TinySet +TinySet::operator& (const TinySet& s) const +{ + TinySet res; + std::set_intersection ( + vec_.begin(), vec_.end(), + s.vec_.begin(), s.vec_.end(), + std::back_inserter (res.vec_), + cmp_); + return res; +} + + + +/* set difference */ +template inline TinySet +TinySet::operator- (const TinySet& s) const +{ + TinySet res; + std::set_difference ( + vec_.begin(), vec_.end(), + s.vec_.begin(), s.vec_.end(), + std::back_inserter (res.vec_), + cmp_); + return res; +} + + + +template inline TinySet& +TinySet::operator|= (const TinySet& s) +{ + return *this = (*this | s); +} + + + +template inline TinySet& +TinySet::operator&= (const TinySet& s) +{ + return *this = (*this & s); +} + + + +template inline TinySet& +TinySet::operator-= (const TinySet& s) +{ + return *this = (*this - s); +} + + + +template inline bool +TinySet::contains (const T& t) const +{ + return std::binary_search ( + vec_.begin(), vec_.end(), t, cmp_); +} + + + +template inline bool +TinySet::contains (const TinySet& s) const +{ + return std::includes ( + vec_.begin(), vec_.end(), + s.vec_.begin(), s.vec_.end(), + cmp_); +} + + + +template inline bool +TinySet::in (const TinySet& s) const +{ + return std::includes ( + s.vec_.begin(), s.vec_.end(), + vec_.begin(), vec_.end(), + cmp_); +} + + + +template inline bool +TinySet::intersects (const TinySet& s) const +{ + return (*this & s).size() > 0; +} + + + +template inline const T& +TinySet::operator[] (typename vector::size_type i) const +{ + return vec_[i]; +} + + + +template inline T& +TinySet::operator[] (typename vector::size_type i) +{ + return vec_[i]; +} + + + +template inline T +TinySet::front (void) const +{ + return vec_.front(); +} + + + +template inline T& +TinySet::front (void) +{ + return vec_.front(); +} + + + +template inline T +TinySet::back (void) const +{ + return vec_.back(); +} + + + +template inline T& +TinySet::back (void) +{ + return vec_.back(); +} + + + +template inline const vector& +TinySet::elements (void) const +{ + return vec_; +} + + + +template inline bool +TinySet::empty (void) const +{ + return vec_.empty(); +} + + + +template inline typename vector::size_type +TinySet::size (void) const +{ + return vec_.size(); +} + + + +template inline void +TinySet::clear (void) +{ + vec_.clear(); +} + + + +template inline void +TinySet::reserve (typename vector::size_type size) +{ + vec_.reserve (size); +} + + + +template typename TinySet::iterator +TinySet::unique_cmp (iterator first, iterator last) +{ + if (first == last) { + return last; + } + iterator result = first; + while (++first != last) { + if (cmp_(*result, *first)) { + *(++result) = *first; + } + } + return ++result; +} + + + +template inline bool +TinySet::consistent (void) const +{ + typename vector::size_type i; + for (i = 0; i < vec_.size() - 1; i++) { + if ( ! cmp_(vec_[i], vec_[i + 1])) { + return false; + } + } + return true; +} + #endif // HORUS_TINYSET_H diff --git a/packages/CLPBN/horus/Util.h b/packages/CLPBN/horus/Util.h index 8eaa06b58..72e77b621 100644 --- a/packages/CLPBN/horus/Util.h +++ b/packages/CLPBN/horus/Util.h @@ -21,7 +21,9 @@ using namespace std; namespace { + const double NEG_INF = -std::numeric_limits::infinity(); + }; diff --git a/packages/CLPBN/horus/Var.cpp b/packages/CLPBN/horus/Var.cpp index 99540718a..5b2afec0b 100644 --- a/packages/CLPBN/horus/Var.cpp +++ b/packages/CLPBN/horus/Var.cpp @@ -73,3 +73,38 @@ Var::states (void) const return states; } + + +inline void +Var::addVarInfo ( + VarId vid, string label, const States& states) +{ + assert (Util::contains (varsInfo_, vid) == false); + varsInfo_.insert (make_pair (vid, VarInfo (label, states))); +} + + + +inline VarInfo +Var::getVarInfo (VarId vid) +{ + assert (Util::contains (varsInfo_, vid)); + return varsInfo_.find (vid)->second; +} + + + +inline bool +Var::varsHaveInfo (void) +{ + return varsInfo_.empty() == false; +} + + + +inline void +Var::clearVarsInfo (void) +{ + varsInfo_.clear(); +} + diff --git a/packages/CLPBN/horus/Var.h b/packages/CLPBN/horus/Var.h index 3ae6eeed8..f61876047 100644 --- a/packages/CLPBN/horus/Var.h +++ b/packages/CLPBN/horus/Var.h @@ -39,23 +39,13 @@ class Var void setIndex (size_t idx) { index_ = idx; } - bool hasEvidence (void) const - { - return evidence_ != Constants::NO_EVIDENCE; - } + bool hasEvidence (void) const; - operator size_t (void) const { return index_; } + operator size_t (void) const; - bool operator== (const Var& var) const - { - assert (!(varId_ == var.varId() && range_ != var.range())); - return varId_ == var.varId(); - } + bool operator== (const Var& var) const; - bool operator!= (const Var& var) const - { - return !(*this == var); - } + bool operator!= (const Var& var) const; bool isValidState (int); @@ -66,27 +56,13 @@ class Var States states (void) const; static void addVarInfo ( - VarId vid, string label, const States& states) - { - assert (Util::contains (varsInfo_, vid) == false); - varsInfo_.insert (make_pair (vid, VarInfo (label, states))); - } + VarId vid, string label, const States& states); - static VarInfo getVarInfo (VarId vid) - { - assert (Util::contains (varsInfo_, vid)); - return varsInfo_.find (vid)->second; - } + static VarInfo getVarInfo (VarId vid); - static bool varsHaveInfo (void) - { - return varsInfo_.empty() == false; - } + static bool varsHaveInfo (void); - static void clearVarsInfo (void) - { - varsInfo_.clear(); - } + static void clearVarsInfo (void); private: VarId varId_; @@ -95,8 +71,41 @@ class Var size_t index_; static unordered_map varsInfo_; - }; + + +inline bool +Var::hasEvidence (void) const +{ + return evidence_ != Constants::NO_EVIDENCE; +} + + + +inline +Var::operator size_t (void) const +{ + return index_; +} + + + +inline bool +Var::operator== (const Var& var) const +{ + assert (!(varId_ == var.varId() && range_ != var.range())); + return varId_ == var.varId(); +} + + + +inline bool +Var::operator!= (const Var& var) const +{ + return !(*this == var); +} + + #endif // HORUS_VAR_H diff --git a/packages/CLPBN/horus/WeightedBp.h b/packages/CLPBN/horus/WeightedBp.h index 9bb40fe95..39f3d7a8b 100644 --- a/packages/CLPBN/horus/WeightedBp.h +++ b/packages/CLPBN/horus/WeightedBp.h @@ -3,6 +3,7 @@ #include "BeliefProp.h" + class WeightedLink : public BpLink { public: @@ -16,12 +17,7 @@ class WeightedLink : public BpLink const Params& powMessage (void) const { return pwdMsg_; } - void updateMessage (void) - { - pwdMsg_ = *nextMsg_; - swap (currMsg_, nextMsg_); - LogAware::pow (pwdMsg_, weight_); - } + void updateMessage (void); private: DISALLOW_COPY_AND_ASSIGN (WeightedLink); @@ -33,6 +29,16 @@ class WeightedLink : public BpLink +inline void +WeightedLink::updateMessage (void) +{ + pwdMsg_ = *nextMsg_; + swap (currMsg_, nextMsg_); + LogAware::pow (pwdMsg_, weight_); +} + + + class WeightedBp : public BeliefProp { public: