From 2efca0c85a81fa41c91f10c7f9e88d7176f51a3a Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Fri, 25 May 2012 21:16:08 +0100 Subject: [PATCH] refactor indexer classes and receive te ranges as a constant reference --- packages/CLPBN/horus/BpSolver.cpp | 8 +-- packages/CLPBN/horus/Factor.cpp | 2 +- packages/CLPBN/horus/Factor.h | 10 +-- packages/CLPBN/horus/Indexer.h | 102 +++++++++++++---------------- packages/CLPBN/horus/Parfactor.cpp | 26 ++++---- packages/CLPBN/horus/TODO | 1 - packages/CLPBN/horus/Util.cpp | 2 +- 7 files changed, 71 insertions(+), 80 deletions(-) diff --git a/packages/CLPBN/horus/BpSolver.cpp b/packages/CLPBN/horus/BpSolver.cpp index 5518271a4..468d82155 100644 --- a/packages/CLPBN/horus/BpSolver.cpp +++ b/packages/CLPBN/horus/BpSolver.cpp @@ -399,18 +399,18 @@ BpSolver::getJointByConditioning (const VarIds& jointVarIds) const observedVars.push_back (fg->getVarNode (observedVids[j])); observedRanges.push_back (observedVars.back()->range()); } - StatesIndexer idx (observedRanges, false); - while (idx.valid()) { + Indexer indexer (observedRanges, false); + while (indexer.valid()) { for (size_t j = 0; j < observedVars.size(); j++) { - observedVars[j]->setEvidence (idx[j]); + observedVars[j]->setEvidence (indexer[j]); } - ++ idx; BpSolver solver (*fg); solver.runSolver(); Params beliefs = solver.getPosterioriOf (jointVarIds[i]); for (size_t k = 0; k < beliefs.size(); k++) { newBeliefs.push_back (beliefs[k]); } + ++ indexer; } int count = -1; diff --git a/packages/CLPBN/horus/Factor.cpp b/packages/CLPBN/horus/Factor.cpp index 5de704286..5bfbfc642 100644 --- a/packages/CLPBN/horus/Factor.cpp +++ b/packages/CLPBN/horus/Factor.cpp @@ -219,7 +219,7 @@ Factor::sumOutArgs (const vector& mask) Params newps (new_size, LogAware::addIdenty()); Params::const_iterator first = params_.begin(); Params::const_iterator last = params_.end(); - MappingIndexer indexer (oldRanges, mask); + CutIndexer indexer (oldRanges, mask); if (Globals::logDomain) { while (first != last) { newps[indexer] = Util::logSum (newps[indexer], *first++); diff --git a/packages/CLPBN/horus/Factor.h b/packages/CLPBN/horus/Factor.h index deb88bcea..fb0b41640 100644 --- a/packages/CLPBN/horus/Factor.h +++ b/packages/CLPBN/horus/Factor.h @@ -109,7 +109,7 @@ class TFactor } } } else { - StatesIndexer indexer (ranges_, false); + Indexer indexer (ranges_, false); while (indexer.valid()) { size_t g_li = 0; size_t prod = 1; @@ -136,7 +136,7 @@ class TFactor Params newps (new_size, LogAware::addIdenty()); Params::const_iterator first = params_.begin(); Params::const_iterator last = params_.end(); - MappingIndexer indexer (ranges_, idx); + CutIndexer indexer (ranges_, idx); if (Globals::logDomain) { while (first != last) { newps[indexer] = Util::logSum (newps[indexer], *first++); @@ -161,13 +161,13 @@ class TFactor Params copy = params_; params_.clear(); params_.reserve (copy.size() / ranges_[idx]); - StatesIndexer indexer (ranges_); + Indexer indexer (ranges_); for (unsigned i = 0; i < evidence; i++) { - indexer.increment (idx); + indexer.incrementDimension (idx); } while (indexer.valid()) { params_.push_back (copy[indexer]); - indexer.incrementExcluding (idx); + indexer.incrementExceptDimension (idx); } args_.erase (args_.begin() + idx); ranges_.erase (ranges_.begin() + idx); diff --git a/packages/CLPBN/horus/Indexer.h b/packages/CLPBN/horus/Indexer.h index a647e49b0..7fc4bf3c4 100644 --- a/packages/CLPBN/horus/Indexer.h +++ b/packages/CLPBN/horus/Indexer.h @@ -1,29 +1,23 @@ -#ifndef HORUS_STATESINDEXER_H -#define HORUS_STATESINDEXER_H +#ifndef HORUS_INDEXER_H +#define HORUS_INDEXER_H #include -#include #include #include #include -#include "Var.h" #include "Util.h" - -class StatesIndexer +class Indexer { public: - - StatesIndexer (const Ranges& ranges, bool calcOffsets = true) + Indexer (const Ranges& ranges, bool calcOffsets = true) + : index_(0), indices_(ranges.size(), 0), ranges_(ranges) { - li_ = 0; size_ = std::accumulate (ranges.begin(), ranges.end(), 1, std::multiplies()); - indices_.resize (ranges.size(), 0); - ranges_ = ranges; if (calcOffsets) { calculateOffsets(); } @@ -39,47 +33,37 @@ class StatesIndexer indices_[i] = 0; } } - li_ ++; + index_ ++; } - void increment (size_t dim) + void incrementDimension (size_t dim) { assert (dim < ranges_.size()); assert (ranges_.size() == offsets_.size()); assert (indices_[dim] < ranges_[dim]); indices_[dim] ++; - li_ += offsets_[dim]; + index_ += offsets_[dim]; } - void incrementExcluding (size_t skipDim) + void incrementExceptDimension (size_t dim) { assert (ranges_.size() == offsets_.size()); for (size_t i = ranges_.size(); i-- > 0; ) { - if (i != (int)skipDim) { + if (i != dim) { indices_[i] ++; - li_ += offsets_[i]; + index_ += offsets_[i]; if (indices_[i] != ranges_[i]) { return; } else { indices_[i] = 0; - li_ -= offsets_[i] * ranges_[i]; + index_ -= offsets_[i] * ranges_[i]; } } } - li_ = size_; + index_ = size_; } - size_t linearIndex (void) const - { - return li_; - } - - const vector& indices (void) const - { - return indices_; - } - - StatesIndexer& operator ++ (void) + Indexer& operator++ (void) { increment(); return *this; @@ -87,7 +71,7 @@ class StatesIndexer operator size_t (void) const { - return li_; + return index_; } unsigned operator[] (size_t dim) const @@ -99,19 +83,19 @@ class StatesIndexer bool valid (void) const { - return li_ < size_; + return index_ < size_; } void reset (void) { std::fill (indices_.begin(), indices_.end(), 0); - li_ = 0; + index_ = 0; } - void reset (size_t dim) + void resetDimension (size_t dim) { indices_[dim] = 0; - li_ -= offsets_[dim] * ranges_[dim]; + index_ -= offsets_[dim] * ranges_[dim]; } size_t size (void) const @@ -119,12 +103,7 @@ class StatesIndexer return size_ ; } - friend ostream& operator<< (ostream &os, const StatesIndexer& idx) - { - os << "(" << std::setw (2) << std::setfill('0') << idx.li_ << ") " ; - os << idx.indices_; - return os; - } + friend std::ostream& operator<< (std::ostream&, const Indexer&); private: void calculateOffsets (void) @@ -137,19 +116,31 @@ class StatesIndexer } } - size_t li_; - size_t size_; - vector indices_; - vector ranges_; - vector offsets_; + size_t index_; + Ranges indices_; + const Ranges& ranges_; + size_t size_; + vector offsets_; }; -class MappingIndexer +inline std::ostream& +operator<< (std::ostream& os, const Indexer& indexer) +{ + os << "(" ; + os << std::setw (2) << std::setfill('0') << indexer.index_; + os << ") " ; + os << indexer.indices_; + return os; +} + + + +class CutIndexer { public: - MappingIndexer (const Ranges& ranges, const vector& mask) + CutIndexer (const Ranges& ranges, const vector& mask) : index_(0), indices_(ranges.size(), 0), ranges_(ranges), valid_(true) { @@ -164,7 +155,7 @@ class MappingIndexer assert (ranges.size() == mask.size()); } - MappingIndexer (const Ranges& ranges, size_t dim) + CutIndexer (const Ranges& ranges, size_t dim) : index_(0), indices_(ranges.size(), 0), ranges_(ranges), valid_(true) { @@ -178,7 +169,7 @@ class MappingIndexer } } - MappingIndexer& operator++ (void) + CutIndexer& operator++ (void) { assert (valid_); for (size_t i = ranges_.size(); i-- > 0; ) { @@ -218,7 +209,7 @@ class MappingIndexer index_ = 0; } - friend std::ostream& operator<< (std::ostream&, const MappingIndexer&); + friend std::ostream& operator<< (std::ostream&, const CutIndexer&); private: size_t index_; @@ -230,15 +221,16 @@ class MappingIndexer -inline std::ostream& operator<< (ostream &os, const MappingIndexer& mi) +inline std::ostream& +operator<< (std::ostream &os, const CutIndexer& indexer) { os << "(" ; - os << std::setw (2) << std::setfill('0') << mi.index_; + os << std::setw (2) << std::setfill('0') << indexer.index_; os << ") " ; - os << mi.indices_; + os << indexer.indices_; return os; } -#endif // HORUS_STATESINDEXER_H +#endif // HORUS_INDEXER_H diff --git a/packages/CLPBN/horus/Parfactor.cpp b/packages/CLPBN/horus/Parfactor.cpp index 1d247207b..b454f05f4 100644 --- a/packages/CLPBN/horus/Parfactor.cpp +++ b/packages/CLPBN/horus/Parfactor.cpp @@ -134,7 +134,7 @@ Parfactor::sumOutIndex (size_t fIdx) args_[fIdx].countedLogVar()); unsigned R = args_[fIdx].range(); vector numAssigns = HistogramSet::getNumAssigns (N, R); - StatesIndexer indexer (ranges_, fIdx); + Indexer indexer (ranges_, fIdx); while (indexer.valid()) { if (Globals::logDomain) { params_[indexer] += numAssigns[ indexer[fIdx] ]; @@ -210,29 +210,29 @@ Parfactor::countConvert (LogVar X) unsigned H = HistogramSet::nrHistograms (N, R); vector histograms = HistogramSet::getHistograms (N, R); - StatesIndexer indexer (ranges_); + Indexer indexer (ranges_); vector sumout (params_.size() / R); unsigned count = 0; while (indexer.valid()) { sumout[count].reserve (R); for (unsigned r = 0; r < R; r++) { sumout[count].push_back (params_[indexer]); - indexer.increment (fIdx); + indexer.incrementDimension (fIdx); } count ++; - indexer.reset (fIdx); - indexer.incrementExcluding (fIdx); + indexer.resetDimension (fIdx); + indexer.incrementExceptDimension (fIdx); } params_.clear(); params_.reserve (sumout.size() * H); ranges_[fIdx] = H; - MappingIndexer mapIndexer (ranges_, fIdx); - while (mapIndexer.valid()) { + CutIndexer cutIndexer (ranges_, fIdx); + while (cutIndexer.valid()) { double prod = LogAware::multIdenty(); - size_t i = mapIndexer; - unsigned h = mapIndexer[fIdx]; + size_t i = cutIndexer; + unsigned h = cutIndexer[fIdx]; for (unsigned r = 0; r < R; r++) { if (Globals::logDomain) { prod += LogAware::pow (sumout[i][r], histograms[h][r]); @@ -241,7 +241,7 @@ Parfactor::countConvert (LogVar X) } } params_.push_back (prod); - ++ mapIndexer; + ++ cutIndexer; } args_[fIdx].setCountedLogVar (X); simplifyCountingFormulas (fIdx); @@ -310,7 +310,7 @@ Parfactor::fullExpand (LogVar X) sumIndexes.reserve (N * R); Ranges expandRanges (N, R); - StatesIndexer indexer (expandRanges); + Indexer indexer (expandRanges); while (indexer.valid()) { vector hist (R, 0); for (unsigned n = 0; n < N; n++) { @@ -572,7 +572,7 @@ void Parfactor::printParameters (void) const { vector jointStrings; - StatesIndexer indexer (ranges_); + Indexer indexer (ranges_); while (indexer.valid()) { stringstream ss; for (size_t i = 0; i < args_.size(); i++) { @@ -740,7 +740,7 @@ Parfactor::simplifyParfactor (size_t fIdx1, size_t fIdx2) { Params copy = params_; params_.clear(); - StatesIndexer indexer (ranges_); + Indexer indexer (ranges_); while (indexer.valid()) { if (indexer[fIdx1] == indexer[fIdx2]) { params_.push_back (copy[indexer]); diff --git a/packages/CLPBN/horus/TODO b/packages/CLPBN/horus/TODO index cfcf7c9ee..09913f884 100644 --- a/packages/CLPBN/horus/TODO +++ b/packages/CLPBN/horus/TODO @@ -1,4 +1,3 @@ -- Receive ranges as a constant reference in Indexer - Check if evidence remains in the compressed factor graph - Consider using hashs instead of vectors of colors to calculate the groups in counting bp diff --git a/packages/CLPBN/horus/Util.cpp b/packages/CLPBN/horus/Util.cpp index 520cb42f1..f88ceb9cc 100644 --- a/packages/CLPBN/horus/Util.cpp +++ b/packages/CLPBN/horus/Util.cpp @@ -183,7 +183,7 @@ getStateLines (const Vars& vars) for (size_t i = 0; i < vars.size(); i++) { ranges.push_back (vars[i]->range()); } - StatesIndexer indexer (ranges); + Indexer indexer (ranges); vector jointStrings; while (indexer.valid()) { stringstream ss;