-/* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
- Radboud University Nijmegen, The Netherlands
+/* This file is part of libDAI - http://www.libdai.org/
+ *
+ * libDAI is licensed under the terms of the GNU General Public License version
+ * 2, or (at your option) any later version. libDAI is distributed without any
+ * warranty. See the file COPYING for more details.
+ *
+ * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
+ * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
+ */
- This file is part of libDAI.
- libDAI is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- libDAI is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with libDAI; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+/// \file
+/// \brief Defines class LC
+/// \todo Improve documentation
#ifndef __defined_libdai_lc_h
namespace dai {
+/// Approximate inference algorithm "Loop Corrected Belief Propagation" by Mooij and Kappen
class LC : public DAIAlgFG {
private:
std::vector<Factor> _pancakes; // used by all LC types (psi_I is stored in the pancake)
std::vector<Factor> _beliefs;
/// Maximum difference encountered so far
- double _maxdiff;
+ Real _maxdiff;
/// Number of iterations needed
size_t _iters;
public:
+ /// Parameters of this inference algorithm
struct Properties {
+ /// Enumeration of possible ways to initialize the cavities
+ DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM);
+
+ /// Enumeration of different update schedules
+ DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE);
+
+ /// Verbosity
size_t verbose;
+
+ /// Maximum number of iterations
size_t maxiter;
- double tol;
+
+ /// Tolerance
+ Real tol;
+
+ /// Complete or partial reinit of cavity graphs?
bool reinit;
- double damping;
- DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM)
+
+ /// Damping constant
+ Real damping;
+
+ /// How to initialize the cavities
CavityType cavity;
- DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE)
+
+ /// What update schedule to use
UpdateType updates;
+
+ /// Name of the algorithm used to initialize the cavity distributions
std::string cavainame; // FIXME: needs assignment operator?
+
+ /// Parameters for the algorithm used to initialize the cavity distributions
PropertySet cavaiopts; // FIXME: needs assignment operator?
} props;
- /// Name of this inference method
+
+ /// Name of this inference algorithm
static const char *Name;
public:
/// 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), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
- /// Clone *this (virtual copy constructor)
+ /// @name General InfAlg interface
+ //@{
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 ) {
- if( this != &x ) {
- 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;
- }
-
- /// 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 Factor belief( const VarSet &/*ns*/ ) const { DAI_THROW(NOT_IMPLEMENTED); return Factor(); }
virtual std::vector<Factor> beliefs() const { return _beliefs; }
-
- /// Get log partition sum
- virtual Real logZ() const {
- DAI_THROW(NOT_IMPLEMENTED);
- return 0.0;
- }
-
- /// Clear messages and beliefs
+ virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
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 Real run();
+ virtual Real maxDiff() const { return _maxdiff; }
virtual size_t Iterations() const { return _iters; }
+ //@}
+
+ Factor beliefV( size_t i ) const { return _beliefs[i]; }
- double CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
- double InitCavityDists( const std::string &name, const PropertySet &opts );
+ /// @name Additional interface specific for LC
+ //@{
+ Real CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
+ Real InitCavityDists( const std::string &name, const PropertySet &opts );
long SetCavityDists( std::vector<Factor> &Q );
Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
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]; };
+ //@}
+ private:
void setProperties( const PropertySet &opts );
PropertySet getProperties() const;
std::string printProperties() const;