nr_IRs() -> nrIRs()
ORs() -> ORs
IRs() -> IRs
- - *::Regenerate() -> *::create()
+ - *::Regenerate() -> *::construct()
- Renamed Index -> IndexFor
- Diffs:
max() -> maxDiff()
dai_writefg.m
matlab.cpp
matlab.h
-
utils/
fg2dot.cpp
fginfo.cpp
testall
testregression
test.cpp
+mf.h
+mf.cpp
FILES IN SVN HEAD THAT ARE STILL RELEVANT:
ChangeLog
jtree.cpp
lc.h
lc.cpp
-mf.h
-mf.cpp
mr.h
mr.cpp
treeep.h
* The value_type of an EdgeInputIterator should be Edge.
*/
template<typename EdgeInputIterator>
- void create( size_t nr1, size_t nr2, EdgeInputIterator begin, EdgeInputIterator end );
+ void construct( size_t nr1, size_t nr2, EdgeInputIterator begin, EdgeInputIterator end );
/// Construct bipartite graph from a range of edges.
/** nr1 is the number of nodes of type 1, nr2 the number of nodes of type 2.
*/
template<typename EdgeInputIterator>
BipartiteGraph( size_t nr1, size_t nr2, EdgeInputIterator begin, EdgeInputIterator end ) : _nb1( nr1 ), _nb2( nr2 ) {
- create( nr1, nr2, begin, end );
+ construct( nr1, nr2, begin, end );
}
/// Returns constant reference to the _i2'th neighbor of node i1 of type 1
template<typename EdgeInputIterator>
-void BipartiteGraph::create( size_t nr1, size_t nr2, EdgeInputIterator begin, EdgeInputIterator end ) {
+void BipartiteGraph::construct( size_t nr1, size_t nr2, EdgeInputIterator begin, EdgeInputIterator end ) {
_nb1.clear();
_nb1.resize( nr1 );
_nb2.clear();
/// Construct from FactorGraph fg and PropertySet opts
BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), edges(), props(), maxdiff(0.0) {
setProperties( opts );
- create();
+ construct();
}
/// Assignment operator
BP& operator=( const BP & x ) {
const double & residual(size_t i, size_t _I) const { return edges[i][_I].residual; }
std::string identify() const;
- void create();
+ void construct();
void init();
/// Clear messages and beliefs corresponding to the nodes in ns
virtual void init( const VarSet &ns );
/// Construct from FactorGraph fg and PropertySet opts
ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
setProperties( opts );
- create();
+ construct();
}
/// Assignment operator
}
/// Create (virtual constructor)
- virtual ExactInf* create() const {
- return new ExactInf();
- }
+ virtual ExactInf* create() const { return new ExactInf(); }
/// Return maximum difference between single node
/// beliefs for two consecutive iterations
/// Name of this inference method
static const char *Name;
- void create();
+ void construct();
void restoreFactors( const VarSet &ns ) { FactorGraph::restoreFactors(ns); init(ns); }
void setProperties( const PropertySet &opts );
PropertySet getProperties() const;
virtual ~FactorGraph() {}
/// Create (virtual default constructor)
- virtual FactorGraph* create() const {
- return new FactorGraph(*this);
- }
+ virtual FactorGraph* create() const { return new FactorGraph(*this); }
/// Clone (virtual copy constructor)
- virtual FactorGraph* clone() const {
- return new FactorGraph();
- }
+ virtual FactorGraph* clone() const { return new FactorGraph(); }
// aliases
Var & var(size_t i) { return vars[i]; }
void restoreFactors( const VarSet &ns );
void backupFactors( const VarSet &ns );
/// Part of constructors (creates edges, neighbors and adjacency matrix)
- void createGraph( size_t nrEdges );
+ void constructGraph( size_t nrEdges );
};
vars.push_back( *p1 );
// create graph structure
- createGraph( nrEdges );
+ constructGraph( nrEdges );
}
class MF : public DAIAlgFG {
protected:
std::vector<Factor> _beliefs;
+ /// Maximum difference encountered so far
+ double _maxdiff;
+ /// Number of iterations needed
+ size_t _iters;
public:
struct Properties {
size_t verbose;
size_t maxiter;
double tol;
+ double damping;
} props;
- double maxdiff;
-
+ static const char *Name;
+
public:
- // default constructor
- MF() : DAIAlgFG(), _beliefs(), props(), maxdiff(0.0) {}
- // copy constructor
- MF( const MF& x ) : DAIAlgFG(x), _beliefs(x._beliefs), props(x.props), maxdiff(x.maxdiff) {}
- MF* clone() const { return new MF(*this); }
- /// Create (virtual constructor)
- virtual MF* create() const { return new MF(); }
+ /// Default constructor
+ MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
+
// construct MF object from FactorGraph
- MF( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), props(), maxdiff(0.0) {
+ MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
setProperties( opts );
- create();
+ construct();
}
- // assignment operator
- MF& operator=( const MF &x ) {
+
+ /// Copy constructor
+ MF( const MF &x ) : DAIAlgFG(x), _beliefs(x._beliefs), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
+
+ /// Assignment operator
+ MF & operator=( const MF &x ) {
if( this != &x ) {
DAIAlgFG::operator=( x );
_beliefs = x._beliefs;
- props = x.props;
- maxdiff = x.maxdiff;
+ _maxdiff = x._maxdiff;
+ _iters = x._iters;
+ props = x.props;
}
return *this;
}
- static const char *Name;
+ /// Clone *this (virtual copy constructor)
+ virtual MF* clone() const { return new MF(*this); }
+
+ /// Create (virtual constructor)
+ virtual MF* create() const { return new MF(); }
+
+ /// Return number of passes over the factorgraph needed
+ virtual size_t Iterations() const { return _iters; }
+
+ /// Return maximum difference between single node beliefs for two consecutive iterations
+ double maxDiff() const { return _maxdiff; }
+
+ /// Identify *this for logging purposes
std::string identify() const;
- void create();
+
+ /// Get single node belief
+ Factor belief( const Var &n ) const;
+
+ /// Get general belief
+ Factor belief( const VarSet &ns ) const;
+
+ /// Get all beliefs
+ std::vector<Factor> beliefs() const;
+
+ /// Get log partition sum
+ Real logZ() const;
+
+ void construct();
+
void init();
+
/// Clear messages and beliefs corresponding to the nodes in ns
virtual void init( const VarSet &ns );
+
+ /// The actual approximate inference algorithm
double run();
- Factor beliefV (size_t i) const;
- Factor belief (const Var &n) const;
- Factor belief (const VarSet &ns) const;
- std::vector<Factor> beliefs() const;
- Real logZ() const;
- void restoreFactors( const VarSet &ns ) { FactorGraph::restoreFactors(ns); init(ns); }
void setProperties( const PropertySet &opts );
PropertySet getProperties() const;
std::string printProperties() const;
- double maxDiff() const { return maxdiff; }
+
+ Factor beliefV( size_t i ) const;
};
}
/// Create (virtual default constructor)
- virtual RegionGraph* create() const {
- return new RegionGraph();
- }
+ virtual RegionGraph* create() const { return new RegionGraph(); }
/// Clone (virtual copy constructor)
- virtual RegionGraph* clone() const {
- return new RegionGraph(*this);
- }
+ virtual RegionGraph* clone() const { return new RegionGraph(*this); }
/// Set the content of the I'th factor and make a backup of its old content if backup == true
virtual void setFactor( size_t I, const Factor &newFactor, bool backup = false ) {
}
-void BP::create() {
+void BP::construct() {
// create edge properties
edges.clear();
edges.reserve( nrVars() );
}
// Create bipartite graph
- G.create( vars.size(), clusters.size(), edges.begin(), edges.end() );
+ G.construct( vars.size(), clusters.size(), edges.begin(), edges.end() );
}
}
-void ExactInf::create() {
+void ExactInf::construct() {
// clear variable beliefs and reserve space
_beliefsV.clear();
_beliefsV.reserve( nrVars() );
vars.push_back( *p1 );
// create graph structure
- createGraph( nrEdges );
+ constructGraph( nrEdges );
}
/// Part of constructors (creates edges, neighbours and adjacency matrix)
-void FactorGraph::createGraph( size_t nrEdges ) {
+void FactorGraph::constructGraph( size_t nrEdges ) {
// create a mapping for indices
hash_map<size_t, size_t> hashmap;
}
// create bipartite graph
- G.create( nrVars(), nrFactors(), edges.begin(), edges.end() );
+ G.construct( nrVars(), nrFactors(), edges.begin(), edges.end() );
}
}
// create bipartite graph
- G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
+ G.construct( nrORs(), nrIRs(), edges.begin(), edges.end() );
// Create messages and beliefs
_Qa.clear();
void MF::setProperties( const PropertySet &opts ) {
assert( opts.hasKey("tol") );
assert( opts.hasKey("maxiter") );
- assert( opts.hasKey("verbose") );
props.tol = opts.getStringAs<double>("tol");
props.maxiter = opts.getStringAs<size_t>("maxiter");
- props.verbose = opts.getStringAs<size_t>("verbose");
+ if( opts.hasKey("verbose") )
+ props.verbose = opts.getStringAs<size_t>("verbose");
+ else
+ props.verbose = 0U;
+ if( opts.hasKey("damping") )
+ props.damping = opts.getStringAs<double>("damping");
+ else
+ props.damping = 0.0;
}
opts.Set( "tol", props.tol );
opts.Set( "maxiter", props.maxiter );
opts.Set( "verbose", props.verbose );
+ opts.Set( "damping", props.damping );
return opts;
}
s << "[";
s << "tol=" << props.tol << ",";
s << "maxiter=" << props.maxiter << ",";
- s << "verbose=" << props.verbose << "]";
+ s << "verbose=" << props.verbose << ",";
+ s << "damping=" << props.damping << "]";
return s.str();
}
-void MF::create() {
- // clear beliefs
+void MF::construct() {
+ // create beliefs
_beliefs.clear();
_beliefs.reserve( nrVars() );
-
- // create beliefs
for( size_t i = 0; i < nrVars(); ++i )
- _beliefs.push_back(Factor(var(i)));
+ _beliefs.push_back( Factor( var(i) ) );
}
jan *= piet;
}
- jan.normalize( Prob::NORMPROB );
+ jan.normalize();
if( jan.hasNaNs() ) {
cout << "MF::run(): ERROR: jan has NaNs!" << endl;
return 1.0;
}
+ if( props.damping != 0.0 )
+ jan = (jan^(1.0 - props.damping)) * (_beliefs[i]^props.damping);
diffs.push( dist( jan, _beliefs[i], Prob::DISTLINF ) );
_beliefs[i] = jan;
}
- if( diffs.maxDiff() > maxdiff )
- maxdiff = diffs.maxDiff();
+ _iters = t / pass_size;
+ if( diffs.maxDiff() > _maxdiff )
+ _maxdiff = diffs.maxDiff();
if( props.verbose >= 1 ) {
if( diffs.maxDiff() > props.tol ) {
}
-Factor MF::beliefV (size_t i) const {
+Factor MF::beliefV( size_t i ) const {
Factor piet;
piet = _beliefs[i];
- piet.normalize( Prob::NORMPROB );
+ piet.normalize();
return(piet);
}
Factor henk;
foreach( const Neighbor &j, nbF(I) ) // for all j in I
henk *= _beliefs[j];
- henk.normalize( Prob::NORMPROB );
+ henk.normalize();
Factor piet;
piet = factor(I).log0();
piet *= henk;
RecomputeORs();
// create bipartite graph
- G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
+ G.construct( nrORs(), nrIRs(), edges.begin(), edges.end() );
// Check counting numbers
#ifdef DAI_DEBUG
}
// create bipartite graph
- G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
+ G.construct( nrORs(), nrIRs(), edges.begin(), edges.end() );
// Calculate counting numbers
Calc_Counting_Numbers();
}
// create bipartite graph
- G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
+ G.construct( nrORs(), nrIRs(), edges.begin(), edges.end() );
// Check counting numbers
Check_Counting_Numbers();
# --- MF ----------------------
-MF_SEQRND: MF[tol=1e-9,maxiter=10000,verbose=0]
+MF_SEQRND: MF[tol=1e-9,maxiter=10000]
# --- TREEEP ------------------
q.clear();
for( size_t i = 0; i < fg.nrVars(); i++ )
q.push_back( Factor(Var(i,2), zero) );
- logZ = NAN;
+ logZ = 0.0;
maxdiff = 0.0;
iters = 1;
+ has_logZ = false;
+ has_maxdiff = false;
+ has_iters = false;
} else
obj = newInfAlg( name, fg, opts );
time += toc() - tic;
edges.push_back( BipartiteGraph::Edge(stubs1[e], stubs2[e]) );
// finish construction
- G.create( N, K, edges.begin(), edges.end() );
+ G.construct( N, K, edges.begin(), edges.end() );
return G;
}
edges.push_back( Edge(1,3) ); edges.push_back( Edge(2,3) ); edges.push_back( Edge(3,3) );
// finish construction
- G.create( N, K, edges.begin(), edges.end() );
+ G.construct( N, K, edges.begin(), edges.end() );
return G;
}
}
// finish construction
- G.create( N, K, edges.begin(), edges.end() );
+ G.construct( N, K, edges.begin(), edges.end() );
return G;
}