From 3738d0b2c6bc4ffddc12dd73850a48d996197332 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Thu, 7 Feb 2013 22:37:45 +0000 Subject: [PATCH] Move some things around. No functional changes --- packages/CLPBN/horus/BeliefProp.h | 30 +++++++--------- packages/CLPBN/horus/ConstraintTree.cpp | 3 +- packages/CLPBN/horus/ConstraintTree.h | 37 +++++++++----------- packages/CLPBN/horus/FactorGraph.h | 19 +++++----- packages/CLPBN/horus/Histogram.cpp | 3 +- packages/CLPBN/horus/Histogram.h | 4 +-- packages/CLPBN/horus/Indexer.h | 8 ++--- packages/CLPBN/horus/LiftedUtils.cpp | 15 +++++--- packages/CLPBN/horus/LiftedUtils.h | 37 ++++++++++---------- packages/CLPBN/horus/LiftedWCNF.cpp | 6 ++-- packages/CLPBN/horus/LiftedWCNF.h | 46 ++++++++++++------------- packages/CLPBN/horus/ProbFormula.cpp | 9 +++-- packages/CLPBN/horus/ProbFormula.h | 10 +++--- packages/CLPBN/horus/TinySet.h | 10 +++--- 14 files changed, 118 insertions(+), 119 deletions(-) diff --git a/packages/CLPBN/horus/BeliefProp.h b/packages/CLPBN/horus/BeliefProp.h index 06ecc2380..77783069f 100644 --- a/packages/CLPBN/horus/BeliefProp.h +++ b/packages/CLPBN/horus/BeliefProp.h @@ -100,6 +100,14 @@ class BeliefProp : public GroundSolver static void setMsgSchedule (MsgSchedule sch) { schedule_ = sch; } protected: + struct CmpResidual { + bool operator() (const BpLink* l1, const BpLink* l2) { + return l1->residual() > l2->residual(); + }}; + + typedef std::multiset SortedOrder; + typedef std::unordered_map BpLinkMap; + SPNodeInfo* ninf (const VarNode* var) const; SPNodeInfo* ninf (const FacNode* fac) const; @@ -110,14 +118,6 @@ class BeliefProp : public GroundSolver void updateMessage (BpLink* link); - struct CompareResidual - { - inline bool operator() (const BpLink* link1, const BpLink* link2) - { - return link1->residual() > link2->residual(); - } - }; - void runSolver (void); virtual void createLinks (void); @@ -135,16 +135,12 @@ class BeliefProp : public GroundSolver std::vector varsI_; std::vector facsI_; bool runned_; + SortedOrder sortedOrder_; + BpLinkMap linkMap_; - typedef std::multiset SortedOrder; - SortedOrder sortedOrder_; - - typedef std::unordered_map BpLinkMap; - BpLinkMap linkMap_; - - static double accuracy_; - static unsigned maxIter_; - static MsgSchedule schedule_; + static double accuracy_; + static unsigned maxIter_; + static MsgSchedule schedule_; private: void initializeSolver (void); diff --git a/packages/CLPBN/horus/ConstraintTree.cpp b/packages/CLPBN/horus/ConstraintTree.cpp index f5a36ec1c..fa4797add 100644 --- a/packages/CLPBN/horus/ConstraintTree.cpp +++ b/packages/CLPBN/horus/ConstraintTree.cpp @@ -145,7 +145,8 @@ CTNode::deleteSubtree (CTNode* n) -std::ostream& operator<< (std::ostream &out, const CTNode& n) +std::ostream& +operator<< (std::ostream& out, const CTNode& n) { out << "(" << n.level() << ") " ; out << n.symbol(); diff --git a/packages/CLPBN/horus/ConstraintTree.h b/packages/CLPBN/horus/ConstraintTree.h index 1c0173dc5..c2970aeff 100644 --- a/packages/CLPBN/horus/ConstraintTree.h +++ b/packages/CLPBN/horus/ConstraintTree.h @@ -13,31 +13,28 @@ class CTNode; -typedef std::vector CTNodes; - class ConstraintTree; + + +typedef std::vector CTNodes; typedef std::vector ConstraintTrees; class CTNode { - public: - struct CompareSymbol - { - bool operator() (const CTNode* n1, const CTNode* n2) const - { - return n1->symbol() < n2->symbol(); - } - }; - private: - typedef TinySet CTChilds_; + struct CmpSymbol { + bool operator() (const CTNode* n1, const CTNode* n2) const { + return n1->symbol() < n2->symbol(); + }}; public: - CTNode (const CTNode& n, const CTChilds_& chs = CTChilds_()) + typedef TinySet CTChilds; + + CTNode (const CTNode& n, const CTChilds& chs = CTChilds()) : symbol_(n.symbol()), childs_(chs), level_(n.level()) { } - CTNode (Symbol s, unsigned l, const CTChilds_& chs = CTChilds_()) + CTNode (Symbol s, unsigned l, const CTChilds& chs = CTChilds()) : symbol_(s), childs_(chs), level_(l) { } unsigned level (void) const { return level_; } @@ -48,9 +45,9 @@ class CTNode void setSymbol (const Symbol s) { symbol_ = s; } - CTChilds_& childs (void) { return childs_; } + CTChilds& childs (void) { return childs_; } - const CTChilds_& childs (void) const { return childs_; } + const CTChilds& childs (void) const { return childs_; } size_t nrChilds (void) const { return childs_.size(); } @@ -58,7 +55,7 @@ class CTNode bool isLeaf (void) const { return childs_.empty(); } - CTChilds_::iterator findSymbol (Symbol symb); + CTChilds::iterator findSymbol (Symbol symb); void mergeSubtree (CTNode*, bool = true); @@ -80,14 +77,14 @@ class CTNode void updateChildLevels (CTNode*, unsigned); Symbol symbol_; - CTChilds_ childs_; + CTChilds childs_; unsigned level_; DISALLOW_ASSIGN (CTNode); }; -typedef TinySet CTChilds; +typedef CTNode::CTChilds CTChilds; inline CTChilds::iterator @@ -98,7 +95,7 @@ CTNode::findSymbol (Symbol symb) } -std::ostream& operator<< (std::ostream &out, const CTNode&); +std::ostream& operator<< (std::ostream&, const CTNode&); class ConstraintTree diff --git a/packages/CLPBN/horus/FactorGraph.h b/packages/CLPBN/horus/FactorGraph.h index 11c26b767..f829ecd1e 100644 --- a/packages/CLPBN/horus/FactorGraph.h +++ b/packages/CLPBN/horus/FactorGraph.h @@ -135,6 +135,8 @@ class FactorGraph static void disablePrintFactorGraph (void) { printFg_ = false; } private: + typedef std::unordered_map VarMap; + void ignoreLines (std::ifstream&) const; bool containsCycle (void) const; @@ -145,19 +147,16 @@ class FactorGraph bool containsCycle (const FacNode*, const VarNode*, std::vector&, std::vector&) const; - VarNodes varNodes_; - FacNodes facNodes_; - + VarNodes varNodes_; + FacNodes facNodes_; + VarMap varMap_; BayesBallGraph structure_; bool bayesFactors_; - typedef std::unordered_map VarMap; - VarMap varMap_; - - static bool exportLd_; - static bool exportUai_; - static bool exportGv_; - static bool printFg_; + static bool exportLd_; + static bool exportUai_; + static bool exportGv_; + static bool printFg_; DISALLOW_ASSIGN (FactorGraph); }; diff --git a/packages/CLPBN/horus/Histogram.cpp b/packages/CLPBN/horus/Histogram.cpp index 0a324fc69..23540a68c 100644 --- a/packages/CLPBN/horus/Histogram.cpp +++ b/packages/CLPBN/horus/Histogram.cpp @@ -138,7 +138,8 @@ HistogramSet::clearAfter (size_t idx) -std::ostream& operator<< (std::ostream &os, const HistogramSet& hs) +std::ostream& +operator<< (std::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 16044e487..2129fcdbb 100644 --- a/packages/CLPBN/horus/Histogram.h +++ b/packages/CLPBN/horus/Histogram.h @@ -36,11 +36,11 @@ class HistogramSet void clearAfter (size_t); + friend std::ostream& operator<< (std::ostream&, const HistogramSet&); + unsigned size_; Histogram hist_; - friend std::ostream& operator<< (std::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 f85b86b22..7f2e82c70 100644 --- a/packages/CLPBN/horus/Indexer.h +++ b/packages/CLPBN/horus/Indexer.h @@ -38,14 +38,14 @@ class Indexer private: void calculateOffsets (void); + friend std::ostream& operator<< (std::ostream&, const Indexer&); + size_t index_; Ranges indices_; const Ranges& ranges_; size_t size_; std::vector offsets_; - friend std::ostream& operator<< (std::ostream&, const Indexer&); - DISALLOW_COPY_AND_ASSIGN (Indexer); }; @@ -223,14 +223,14 @@ class MapIndexer void reset (void); private: + friend std::ostream& operator<< (std::ostream&, const MapIndexer&); + size_t index_; Ranges indices_; const Ranges& ranges_; bool valid_; std::vector offsets_; - friend std::ostream& operator<< (std::ostream&, const MapIndexer&); - DISALLOW_COPY_AND_ASSIGN (MapIndexer); }; diff --git a/packages/CLPBN/horus/LiftedUtils.cpp b/packages/CLPBN/horus/LiftedUtils.cpp index 2527bd266..6b839055c 100644 --- a/packages/CLPBN/horus/LiftedUtils.cpp +++ b/packages/CLPBN/horus/LiftedUtils.cpp @@ -40,7 +40,8 @@ printSymbolDictionary (void) -std::ostream& operator<< (std::ostream &os, const Symbol& s) +std::ostream& +operator<< (std::ostream& os, const Symbol& s) { std::unordered_map::const_iterator it = LiftedUtils::symbolDict.begin(); @@ -54,7 +55,8 @@ std::ostream& operator<< (std::ostream &os, const Symbol& s) -std::ostream& operator<< (std::ostream &os, const LogVar& X) +std::ostream& +operator<< (std::ostream& os, const LogVar& X) { const std::string labels[] = { "A", "B", "C", "D", "E", "F", @@ -65,7 +67,8 @@ std::ostream& operator<< (std::ostream &os, const LogVar& X) -std::ostream& operator<< (std::ostream &os, const Tuple& t) +std::ostream& +operator<< (std::ostream& os, const Tuple& t) { os << "(" ; for (size_t i = 0; i < t.size(); i++) { @@ -77,7 +80,8 @@ std::ostream& operator<< (std::ostream &os, const Tuple& t) -std::ostream& operator<< (std::ostream &os, const Ground& gr) +std::ostream& +operator<< (std::ostream& os, const Ground& gr) { os << gr.functor(); os << "(" ; @@ -111,7 +115,8 @@ Substitution::getDiscardedLogVars (void) const -std::ostream& operator<< (std::ostream &os, const Substitution& theta) +std::ostream& +operator<< (std::ostream& os, const Substitution& theta) { std::unordered_map::const_iterator it; os << "[" ; diff --git a/packages/CLPBN/horus/LiftedUtils.h b/packages/CLPBN/horus/LiftedUtils.h index 3cc734b2f..02c9cdf72 100644 --- a/packages/CLPBN/horus/LiftedUtils.h +++ b/packages/CLPBN/horus/LiftedUtils.h @@ -24,9 +24,9 @@ class Symbol static Symbol invalid (void) { return Symbol(); } private: - unsigned id_; + friend std::ostream& operator<< (std::ostream&, const Symbol&); - friend std::ostream& operator<< (std::ostream &os, const Symbol& s); + unsigned id_; }; @@ -44,9 +44,9 @@ class LogVar bool valid (void) const; private: - unsigned id_; + friend std::ostream& operator<< (std::ostream&, const LogVar&); - friend std::ostream& operator<< (std::ostream &os, const LogVar& X); + unsigned id_; }; @@ -86,16 +86,16 @@ template <> struct hash { }; -typedef std::vector Symbols; -typedef std::vector Tuple; -typedef std::vector Tuples; -typedef std::vector LogVars; -typedef TinySet SymbolSet; -typedef TinySet LogVarSet; -typedef TinySet TupleSet; +typedef std::vector Symbols; +typedef std::vector Tuple; +typedef std::vector Tuples; +typedef std::vector LogVars; +typedef TinySet SymbolSet; +typedef TinySet LogVarSet; +typedef TinySet TupleSet; -std::ostream& operator<< (std::ostream &os, const Tuple& t); +std::ostream& operator<< (std::ostream&, const Tuple&); namespace LiftedUtils { @@ -113,7 +113,8 @@ class Ground public: Ground (Symbol f) : functor_(f) { } - Ground (Symbol f, const Symbols& args) : functor_(f), args_(args) { } + Ground (Symbol f, const Symbols& args) + : functor_(f), args_(args) { } Symbol functor (void) const { return functor_; } @@ -124,10 +125,10 @@ class Ground bool isAtom (void) const { return args_.empty(); } private: + friend std::ostream& operator<< (std::ostream&, const Ground&); + Symbol functor_; Symbols args_; - - friend std::ostream& operator<< (std::ostream &os, const Ground& gr); }; typedef std::vector Grounds; @@ -150,11 +151,10 @@ class Substitution LogVars getDiscardedLogVars (void) const; private: - std::unordered_map subs_; - friend std::ostream& operator<< ( - std::ostream &os, const Substitution& theta); + std::ostream&, const Substitution&); + std::unordered_map subs_; }; @@ -205,6 +205,5 @@ Substitution::nrReplacements (void) const return subs_.size(); } - #endif // YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_ diff --git a/packages/CLPBN/horus/LiftedWCNF.cpp b/packages/CLPBN/horus/LiftedWCNF.cpp index 8b218f50e..73f7eca8f 100644 --- a/packages/CLPBN/horus/LiftedWCNF.cpp +++ b/packages/CLPBN/horus/LiftedWCNF.cpp @@ -65,7 +65,7 @@ Literal::toString ( std::ostream& -operator<< (std::ostream &os, const Literal& lit) +operator<< (std::ostream& os, const Literal& lit) { os << lit.toString(); return os; @@ -342,7 +342,7 @@ Clause::deleteClauses (Clauses& clauses) std::ostream& -operator<< (std::ostream &os, const Clause& clause) +operator<< (std::ostream& os, const Clause& clause) { for (unsigned i = 0; i < clause.literals_.size(); i++) { if (i != 0) os << " v " ; @@ -374,7 +374,7 @@ Clause::getLogVarSetExcluding (size_t idx) const std::ostream& -operator<< (std::ostream &os, const LitLvTypes& lit) +operator<< (std::ostream& os, const LitLvTypes& lit) { os << lit.lid_ << "<" ; for (size_t i = 0; i < lit.lvTypes_.size(); i++) { diff --git a/packages/CLPBN/horus/LiftedWCNF.h b/packages/CLPBN/horus/LiftedWCNF.h index 35632dfbb..d0da678d8 100644 --- a/packages/CLPBN/horus/LiftedWCNF.h +++ b/packages/CLPBN/horus/LiftedWCNF.h @@ -54,11 +54,11 @@ class Literal LogVarSet negCountedLvs = LogVarSet()) const; private: + friend std::ostream& operator<< (std::ostream&, const Literal&); + LiteralId lid_; LogVars logVars_; bool negated_; - - friend std::ostream& operator<< (std::ostream &os, const Literal& lit); }; typedef std::vector Literals; @@ -145,14 +145,14 @@ class Clause private: LogVarSet getLogVarSetExcluding (size_t idx) const; + friend std::ostream& operator<< (std::ostream&, const Clause&); + Literals literals_; LogVarSet ipgLvs_; LogVarSet posCountedLvs_; LogVarSet negCountedLvs_; ConstraintTree constr_; - friend std::ostream& operator<< (std::ostream &os, const Clause& clause); - DISALLOW_ASSIGN (Clause); }; @@ -163,22 +163,6 @@ typedef std::vector Clauses; class LitLvTypes { public: - struct CompareLitLvTypes - { - bool operator() ( - const LitLvTypes& types1, - const LitLvTypes& types2) const - { - if (types1.lid_ < types2.lid_) { - return true; - } - if (types1.lid_ == types2.lid_) { - return types1.lvTypes_ < types2.lvTypes_; - } - return false; - } - }; - LitLvTypes (LiteralId lid, const LogVarTypes& lvTypes) : lid_(lid), lvTypes_(lvTypes) { } @@ -190,13 +174,29 @@ class LitLvTypes std::fill (lvTypes_.begin(), lvTypes_.end(), LogVarType::FULL_LV); } private: + friend std::ostream& operator<< (std::ostream&, const LitLvTypes&); + LiteralId lid_; LogVarTypes lvTypes_; - - friend std::ostream& operator<< (std::ostream &os, const LitLvTypes& lit); }; -typedef TinySet LitLvTypesSet; + + +struct CmpLitLvTypes +{ + bool operator() (const LitLvTypes& types1, const LitLvTypes& types2) const + { + if (types1.lid() < types2.lid()) { + return true; + } + if (types1.lid() == types2.lid()){ + return types1.logVarTypes() < types2.logVarTypes(); + } + return false; + } +}; + +typedef TinySet LitLvTypesSet; diff --git a/packages/CLPBN/horus/ProbFormula.cpp b/packages/CLPBN/horus/ProbFormula.cpp index f5b7933fe..76fa9a36f 100644 --- a/packages/CLPBN/horus/ProbFormula.cpp +++ b/packages/CLPBN/horus/ProbFormula.cpp @@ -97,7 +97,8 @@ ProbFormula::rename (LogVar oldName, LogVar newName) -bool operator== (const ProbFormula& f1, const ProbFormula& f2) +bool +operator== (const ProbFormula& f1, const ProbFormula& f2) { return f1.group_ == f2.group_ && f1.logVars_ == f2.logVars_; @@ -105,7 +106,8 @@ bool operator== (const ProbFormula& f1, const ProbFormula& f2) -std::ostream& operator<< (std::ostream &os, const ProbFormula& f) +std::ostream& +operator<< (std::ostream& os, const ProbFormula& f) { os << f.functor_; if (f.isAtom() == false) { @@ -151,7 +153,8 @@ ObservedFormula::ObservedFormula (Symbol f, unsigned ev, const Tuple& tuple) -std::ostream& operator<< (std::ostream &os, const ObservedFormula& of) +std::ostream& +operator<< (std::ostream& os, const ObservedFormula& of) { os << of.functor_ << "/" << of.arity_; os << "|" << of.constr_.tupleSet(); diff --git a/packages/CLPBN/horus/ProbFormula.h b/packages/CLPBN/horus/ProbFormula.h index 35a9c975c..f595537d6 100644 --- a/packages/CLPBN/horus/ProbFormula.h +++ b/packages/CLPBN/horus/ProbFormula.h @@ -60,13 +60,13 @@ class ProbFormula static PrvGroup getNewGroup (void); + private: friend bool operator== ( const ProbFormula& f1, const ProbFormula& f2); friend std::ostream& operator<< ( - std::ostream &os, const ProbFormula& f); + std::ostream&, const ProbFormula&); - private: Symbol functor_; LogVars logVars_; unsigned range_; @@ -100,13 +100,13 @@ class ObservedFormula void addTuple (const Tuple& tuple) { constr_.addTuple (tuple); } private: + friend std::ostream& operator<< ( + std::ostream&, const ObservedFormula&); + Symbol functor_; unsigned arity_; unsigned evidence_; ConstraintTree constr_; - - friend std::ostream& operator<< ( - std::ostream &os, const ObservedFormula& of); }; typedef std::vector ObservedFormulas; diff --git a/packages/CLPBN/horus/TinySet.h b/packages/CLPBN/horus/TinySet.h index 0cd22685b..e21c0edeb 100644 --- a/packages/CLPBN/horus/TinySet.h +++ b/packages/CLPBN/horus/TinySet.h @@ -10,9 +10,8 @@ template > class TinySet { public: - - typedef typename std::vector::iterator iterator; - typedef typename std::vector::const_iterator const_iterator; + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; TinySet (const TinySet& s) : vec_(s.vec_), cmp_(s.cmp_) { } @@ -90,9 +89,6 @@ class TinySet bool consistent (void) const; - std::vector vec_; - Compare cmp_; - friend bool operator== (const TinySet& s1, const TinySet& s2) { return s1.vec_ == s2.vec_; @@ -114,6 +110,8 @@ class TinySet return out; } + std::vector vec_; + Compare cmp_; };