X-Git-Url: http://git.tuebingen.mpg.de/?p=libdai.git;a=blobdiff_plain;f=include%2Fdai%2Flc.h;h=b49da41025fb1c1e1ac26e5b0b00ddc1405334fb;hp=b7d5feebe87b832ad58298ad6d27568199131ef7;hb=7f2ae0d584185669fd276f75bf41e46690e0ab51;hpb=9d0ed5f28ac768eea9f4e6f18c897ec13f8bb1c1 diff --git a/include/dai/lc.h b/include/dai/lc.h index b7d5fee..b49da41 100644 --- a/include/dai/lc.h +++ b/include/dai/lc.h @@ -27,6 +27,8 @@ #include #include #include +#include +#include namespace dai { @@ -34,74 +36,117 @@ namespace dai { class LC : public DAIAlgFG { protected: - typedef struct { size_t i; size_t I; } _iI_type; - std::vector _pancakes; // used by all LC types (psi_I is stored in the pancake) std::vector _cavitydists; // used by all LC types to store the approximate cavity distribution - /// _phis[VV2E(i,I)] corresponds to \f$\phi^{\setminus i}_I(x_{I \setminus i}) \f$ - std::vector _phis; + /// _phis[i][_I] corresponds to \f$\phi^{\setminus i}_I(x_{I \setminus i}) \f$ + std::vector > _phis; /// Single variable beliefs std::vector _beliefs; - /// For each pair (i,j) with j in delta(i), store i and the common factor I - std::vector<_iI_type> _iI; + /// Maximum difference encountered so far + double _maxdiff; + /// Number of iterations needed + size_t _iters; public: - ENUM6(CavityType,FULL,PAIR,PAIR2,PAIRINT,PAIRCUM,UNIFORM) - ENUM3(UpdateType,SEQFIX,SEQRND,NONE) + struct Properties { + size_t verbose; + size_t maxiter; + double tol; + bool reinit; + double damping; + DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM) + CavityType cavity; + DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE) + UpdateType updates; + std::string cavainame; // FIXME: needs assignment operator? + PropertySet cavaiopts; // FIXME: needs assignment operator? + } props; + /// Name of this inference method + static const char *Name; - CavityType Cavity() const { return GetPropertyAs("cavity"); } - UpdateType Updates() const { return GetPropertyAs("updates"); } - bool reInit() const { return GetPropertyAs("reinit"); } - + public: /// Default constructor - LC() : DAIAlgFG() {}; + LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {} + + /// Construct from FactorGraph fg and PropertySet opts + LC( const FactorGraph &fg, const PropertySet &opts ); + /// Copy constructor - LC(const LC & x) : DAIAlgFG(x), _pancakes(x._pancakes), _cavitydists(x._cavitydists), _phis(x._phis), _beliefs(x._beliefs), _iI(x._iI) {}; - /// Clone function - LC* clone() const { return new LC(*this); } - /// Construct LC object from a FactorGraph and parameters - LC(const FactorGraph & fg, const Properties &opts); + LC( const LC &x ) : DAIAlgFG(x), _pancakes(x._pancakes), _cavitydists(x._cavitydists), _phis(x._phis), _beliefs(x._beliefs), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {} + + /// Clone *this (virtual copy constructor) + virtual LC* clone() const { return new LC(*this); } + + /// Create (virtual default constructor) + virtual LC* create() const { return new LC(); } + /// Assignment operator - LC& operator=(const LC & x) { + LC& operator=( const LC &x ) { if( this != &x ) { - DAIAlgFG::operator=(x); - _pancakes = x._pancakes; - _cavitydists = x._cavitydists; - _phis = x._phis; - _beliefs = x._beliefs; - _iI = x._iI; + DAIAlgFG::operator=( x ); + _pancakes = x._pancakes; + _cavitydists = x._cavitydists; + _phis = x._phis; + _beliefs = x._beliefs; + _maxdiff = x._maxdiff; + _iters = x._iters; + props = x.props; } return *this; } - static const char *Name; - double CalcCavityDist( size_t i, const std::string &name, const Properties &opts ); - double InitCavityDists( const std::string &name, const Properties &opts ); + /// Identifies itself for logging purposes + virtual std::string identify() const; + + /// Get single node belief + virtual Factor belief( const Var &n ) const { return( _beliefs[findVar(n)] ); } + + /// Get general belief + virtual Factor belief( const VarSet &/*ns*/ ) const { + DAI_THROW(NOT_IMPLEMENTED); + return Factor(); + } + + /// Get all beliefs + virtual std::vector beliefs() const { return _beliefs; } + + /// Get log partition sum + virtual Real logZ() const { + DAI_THROW(NOT_IMPLEMENTED); + return 0.0; + } + + /// Clear messages and beliefs + virtual void init(); + + /// Clear messages and beliefs corresponding to the nodes in ns + virtual void init( const VarSet &/*ns*/ ) { init(); } + + /// The actual approximate inference algorithm + virtual double run(); + + /// Return maximum difference between single node beliefs in the last pass + virtual double maxDiff() const { return _maxdiff; } + + /// Return number of passes over the factorgraph + virtual size_t Iterations() const { return _iters; } + + double CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts ); + double InitCavityDists( const std::string &name, const PropertySet &opts ); long SetCavityDists( std::vector &Q ); - void init(); - Factor NewPancake (size_t iI, bool & hasNaNs); - double run(); + Factor NewPancake (size_t i, size_t _I, bool & hasNaNs); - std::string identify() const; - Factor belief (const Var &n) const { return( _beliefs[findVar(n)] ); } - Factor belief (const VarSet &/*ns*/) const { assert( 0 == 1 ); } - std::vector beliefs() const { return _beliefs; } - Complex logZ() const { return NAN; } void CalcBelief (size_t i); - const Factor & belief (size_t i) const { return _beliefs[i]; }; - const Factor & pancake (size_t i) const { return _pancakes[i]; }; - const Factor & cavitydist (size_t i) const { return _cavitydists[i]; }; - size_t nr_iI() const { return _iI.size(); }; - - void clamp( const Var &/*n*/, size_t /*i*/ ) { assert( 0 == 1 ); } - void undoProbs( const VarSet &/*ns*/ ) { assert( 0 == 1 ); } - void saveProbs( const VarSet &/*ns*/ ) { assert( 0 == 1 ); } - void makeFactorCavity(size_t /*I*/) { assert( 0 == 1 ); } - virtual void makeCavity(const Var & /*n*/) { assert( 0 == 1 ); } - bool checkProperties(); + const Factor &belief (size_t i) const { return _beliefs[i]; }; + const Factor &pancake (size_t i) const { return _pancakes[i]; }; + const Factor &cavitydist (size_t i) const { return _cavitydists[i]; }; + + void setProperties( const PropertySet &opts ); + PropertySet getProperties() const; + std::string printProperties() const; };