dai::BP Class Reference

Approximate inference algorithm "(Loopy) Belief Propagation". More...

#include <dai/bp.h>

Inheritance diagram for dai::BP:

dai::DAIAlg< GRM > dai::InfAlg dai::FBP dai::TRWBP

List of all members.

Public Member Functions

Constructors/destructors
 BP ()
 Default constructor.
 BP (const FactorGraph &fg, const PropertySet &opts)
 Construct from FactorGraph fg and PropertySet opts.
 BP (const BP &x)
 Copy constructor.
BPoperator= (const BP &x)
 Assignment operator.
General InfAlg interface
virtual BPclone () const
 Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor).
virtual std::string identify () const
 Identifies itself for logging purposes.
virtual Factor belief (const Var &v) const
 Returns the (approximate) marginal probability distribution of a variable.
virtual Factor belief (const VarSet &vs) const
 Returns the (approximate) marginal probability distribution of a set of variables.
virtual Factor beliefV (size_t i) const
 Returns the (approximate) marginal probability distribution of the variable with index i.
virtual Factor beliefF (size_t I) const
 Returns the (approximate) marginal probability distribution of the variables on which factor I depends.
virtual std::vector< Factorbeliefs () const
 Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm.
virtual Real logZ () const
 Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph).
std::vector< std::size_t > findMaximum () const
virtual void init ()
 Initializes all data structures of the approximate inference algorithm.
virtual void init (const VarSet &ns)
 Initializes all data structures corresponding to some set of variables.
virtual Real run ()
 Runs the approximate inference algorithm.
virtual Real maxDiff () const
 Returns maximum difference between single variable beliefs in the last iteration.
virtual size_t Iterations () const
 Returns number of iterations done (one iteration passes over the complete factorgraph).
virtual void setMaxIter (size_t maxiter)
 Sets maximum number of iterations (one iteration passes over the complete factorgraph).
virtual void setProperties (const PropertySet &opts)
 Set parameters of this inference algorithm.
virtual PropertySet getProperties () const
 Returns parameters of this inference algorithm converted into a PropertySet.
virtual std::string printProperties () const
 Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1,key2=val2,...,keyn=valn]".
Additional interface specific for BP
const std::vector< std::pair
< std::size_t, std::size_t > > & 
getSentMessages () const
 Returns history of which messages have been updated.
void clearSentMessages ()
 Clears history of which messages have been updated.

Public Attributes

struct dai::BP::Properties props
bool recordSentMessages
 Specifies whether the history of message updates should be recorded.

Static Public Attributes

static const char * Name = "BP"
 Name of this inference algorithm.

Protected Types

typedef std::vector< size_t > ind_t
 Type used for index cache.
typedef std::multimap< Real,
std::pair< std::size_t,
std::size_t > > 
LutType
 Type of lookup table (only used for maximum-residual BP).

Protected Member Functions

const Probmessage (size_t i, size_t _I) const
 Returns constant reference to message from the _I 'th neighbor of variable i to variable i.
Probmessage (size_t i, size_t _I)
 Returns reference to message from the _I 'th neighbor of variable i to variable i.
const ProbnewMessage (size_t i, size_t _I) const
 Returns constant reference to updated message from the _I 'th neighbor of variable i to variable i.
ProbnewMessage (size_t i, size_t _I)
 Returns reference to updated message from the _I 'th neighbor of variable i to variable i.
const ind_tindex (size_t i, size_t _I) const
 Returns constant reference to cached index for the edge between variable i and its _I 'th neighbor.
ind_tindex (size_t i, size_t _I)
 Returns reference to cached index for the edge between variable i and its _I 'th neighbor.
const Realresidual (size_t i, size_t _I) const
 Returns constant reference to residual for the edge between variable i and its _I 'th neighbor.
Realresidual (size_t i, size_t _I)
 Returns reference to residual for the edge between variable i and its _I 'th neighbor.
virtual Prob calcIncomingMessageProduct (size_t I, bool without_i, size_t i) const
 Calculate the product of factor I and the incoming messages.
virtual void calcNewMessage (size_t i, size_t _I)
 Calculate the updated message from the _I 'th neighbor of variable i to variable i.
void updateMessage (size_t i, size_t _I)
 Replace the "old" message from the _I 'th neighbor of variable i to variable i by the "new" (updated) message.
void updateResidual (size_t i, size_t _I, Real r)
 Set the residual (difference between new and old message) for the edge between variable i and its _I 'th neighbor to r.
void findMaxResidual (size_t &i, size_t &_I)
 Finds the edge which has the maximum residual (difference between new and old message).
virtual void calcBeliefV (size_t i, Prob &p) const
 Calculates unnormalized belief of variable i.
virtual void calcBeliefF (size_t I, Prob &p) const
 Calculates unnormalized belief of factor I.
virtual void construct ()
 Helper function for constructors.

Protected Attributes

std::vector< std::vector
< EdgeProp > > 
_edges
 Stores all edge properties.
std::vector< std::vector
< LutType::iterator > > 
_edge2lut
 Lookup table (only used for maximum-residual BP).
LutType _lut
 Lookup table (only used for maximum-residual BP).
Real _maxdiff
 Maximum difference between variable beliefs encountered so far.
size_t _iters
 Number of iterations needed.
std::vector< std::pair
< std::size_t, std::size_t > > 
_sentMessages
 The history of message updates (only recorded if recordSentMessages is true).
std::vector< Factor_oldBeliefsV
 Stores variable beliefs of previous iteration.
std::vector< Factor_oldBeliefsF
 Stores factor beliefs of previous iteration.
std::vector< Edge_updateSeq
 Stores the update schedule.

Classes

struct  EdgeProp
 Type used for storing edge properties. More...
struct  Properties
 Parameters for BP. More...


Detailed Description

Approximate inference algorithm "(Loopy) Belief Propagation".

The Loopy Belief Propagation algorithm uses message passing to approximate marginal probability distributions ("beliefs") for variables and factors (more precisely, for the subset of variables depending on the factor). There are two variants, the sum-product algorithm (corresponding to finite temperature) and the max-product algorithm (corresponding to zero temperature).

The messages $m_{I\to i}(x_i)$ are passed from factors $I$ to variables $i$. In case of the sum-product algorith, the update equation is:

\[ m_{I\to i}(x_i) \propto \sum_{x_{N_I\setminus\{i\}}} f_I(x_I) \prod_{j\in N_I\setminus\{i\}} \prod_{J\in N_j\setminus\{I\}} m_{J\to j}\]

and in case of the max-product algorithm:

\[ m_{I\to i}(x_i) \propto \max_{x_{N_I\setminus\{i\}}} f_I(x_I) \prod_{j\in N_I\setminus\{i\}} \prod_{J\in N_j\setminus\{I\}} m_{J\to j}\]

In order to improve convergence, the updates can be damped. For improved numerical stability, the updates can be done in the log-domain alternatively.

After convergence, the variable beliefs are calculated by:

\[ b_i(x_i) \propto \prod_{I\in N_i} m_{I\to i}(x_i)\]

and the factor beliefs are calculated by:

\[ b_I(x_I) \propto f_I(x_I) \prod_{j\in N_I} \prod_{J\in N_j\setminus\{I\}} m_{J\to j}(x_j) \]

The logarithm of the partition sum is calculated by:

\[ \log Z = \sum_i (1 - |N_i|) \sum_{x_i} b_i(x_i) \log b_i(x_i) - \sum_I \sum_{x_I} b_I(x_I) \log \frac{b_I(x_I)}{f_I(x_I)} \]

For the max-product algorithm, a heuristic way of finding the MAP state (the joint configuration of all variables which has maximum probability) is provided by the findMaximum() method, which can be called after convergence.

Note:
There are two implementations, an optimized one (the default) which caches IndexFor objects, and a slower, less complicated one which is easier to maintain/understand. The slower one can be enabled by defining DAI_BP_FAST as false in the source file.
Examples:

example.cpp.


Member Typedef Documentation

typedef std::vector<size_t> dai::BP::ind_t [protected]

Type used for index cache.

typedef std::multimap<Real, std::pair<std::size_t, std::size_t> > dai::BP::LutType [protected]

Type of lookup table (only used for maximum-residual BP).


Constructor & Destructor Documentation

dai::BP::BP (  )  [inline]

Default constructor.

dai::BP::BP ( const FactorGraph fg,
const PropertySet opts 
) [inline]

Construct from FactorGraph fg and PropertySet opts.

Parameters:
fg Factor graph.
opts Parameters
See also:
Properties

dai::BP::BP ( const BP x  )  [inline]

Copy constructor.


Member Function Documentation

BP& dai::BP::operator= ( const BP x  )  [inline]

Assignment operator.

virtual BP* dai::BP::clone (  )  const [inline, virtual]

Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor).

Implements dai::InfAlg.

Reimplemented in dai::FBP, and dai::TRWBP.

string dai::BP::identify (  )  const [virtual]

Identifies itself for logging purposes.

Implements dai::InfAlg.

Reimplemented in dai::FBP, and dai::TRWBP.

virtual Factor dai::BP::belief ( const Var v  )  const [inline, virtual]

Returns the (approximate) marginal probability distribution of a variable.

Note:
Before this method is called, run() should have been called.

Reimplemented from dai::InfAlg.

Examples:
example.cpp.

Factor dai::BP::belief ( const VarSet vs  )  const [virtual]

Returns the (approximate) marginal probability distribution of a set of variables.

Note:
Before this method is called, run() should have been called.
Exceptions:
NOT_IMPLEMENTED if not implemented/supported.
BELIEF_NOT_AVAILABLE if the requested belief cannot be calculated with this algorithm.

Implements dai::InfAlg.

Factor dai::BP::beliefV ( size_t  i  )  const [virtual]

Returns the (approximate) marginal probability distribution of the variable with index i.

For some approximate inference algorithms, using beliefV() is preferred to belief() for performance reasons.

Note:
Before this method is called, run() should have been called.

Reimplemented from dai::InfAlg.

Factor dai::BP::beliefF ( size_t  I  )  const [virtual]

Returns the (approximate) marginal probability distribution of the variables on which factor I depends.

For some approximate inference algorithms, using beliefF() is preferred to belief() for performance reasons.

Note:
Before this method is called, run() should have been called.

Reimplemented from dai::InfAlg.

Examples:
example.cpp.

vector< Factor > dai::BP::beliefs (  )  const [virtual]

Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm.

Note:
Before this method is called, run() should have been called.

Implements dai::InfAlg.

Real dai::BP::logZ (  )  const [virtual]

Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph).

Note:
Before this method is called, run() should have been called.
Exceptions:
NOT_IMPLEMENTED if not implemented/supported

Implements dai::InfAlg.

Reimplemented in dai::FBP, and dai::TRWBP.

Examples:
example.cpp.

std::vector< size_t > dai::BP::findMaximum (  )  const [virtual]

Precondition:
Assumes that run() has been called and that props.inference == MAXPROD

Reimplemented from dai::InfAlg.

Examples:
example.cpp.

void dai::BP::init (  )  [virtual]

Initializes all data structures of the approximate inference algorithm.

Note:
This method should be called at least once before run() is called.

Implements dai::InfAlg.

Examples:
example.cpp.

void dai::BP::init ( const VarSet vs  )  [virtual]

Initializes all data structures corresponding to some set of variables.

This method can be used to do a partial initialization after a part of the factor graph has changed. Instead of initializing all data structures, it only initializes those involving the variables in vs.

Exceptions:
NOT_IMPLEMENTED if not implemented/supported

Implements dai::InfAlg.

Real dai::BP::run (  )  [virtual]

Runs the approximate inference algorithm.

Note:
Before run() is called the first time, init() should have been called.

Implements dai::InfAlg.

Examples:
example.cpp.

virtual Real dai::BP::maxDiff (  )  const [inline, virtual]

Returns maximum difference between single variable beliefs in the last iteration.

Exceptions:
NOT_IMPLEMENTED if not implemented/supported

Reimplemented from dai::InfAlg.

virtual size_t dai::BP::Iterations (  )  const [inline, virtual]

Returns number of iterations done (one iteration passes over the complete factorgraph).

Exceptions:
NOT_IMPLEMENTED if not implemented/supported

Reimplemented from dai::InfAlg.

virtual void dai::BP::setMaxIter ( size_t   )  [inline, virtual]

Sets maximum number of iterations (one iteration passes over the complete factorgraph).

Exceptions:
NOT_IMPLEMENTED if not implemented/supported

Reimplemented from dai::InfAlg.

void dai::BP::setProperties ( const PropertySet opts  )  [virtual]

Set parameters of this inference algorithm.

The parameters are set according to the PropertySet opts. The values can be stored either as std::string or as the type of the corresponding MF::props member.

Implements dai::InfAlg.

Reimplemented in dai::TRWBP.

PropertySet dai::BP::getProperties (  )  const [virtual]

Returns parameters of this inference algorithm converted into a PropertySet.

Implements dai::InfAlg.

Reimplemented in dai::TRWBP.

string dai::BP::printProperties (  )  const [virtual]

Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1,key2=val2,...,keyn=valn]".

Implements dai::InfAlg.

Reimplemented in dai::TRWBP.

const std::vector<std::pair<std::size_t, std::size_t> >& dai::BP::getSentMessages (  )  const [inline]

Returns history of which messages have been updated.

void dai::BP::clearSentMessages (  )  [inline]

Clears history of which messages have been updated.

const Prob& dai::BP::message ( size_t  i,
size_t  _I 
) const [inline, protected]

Returns constant reference to message from the _I 'th neighbor of variable i to variable i.

Prob& dai::BP::message ( size_t  i,
size_t  _I 
) [inline, protected]

Returns reference to message from the _I 'th neighbor of variable i to variable i.

const Prob& dai::BP::newMessage ( size_t  i,
size_t  _I 
) const [inline, protected]

Returns constant reference to updated message from the _I 'th neighbor of variable i to variable i.

Prob& dai::BP::newMessage ( size_t  i,
size_t  _I 
) [inline, protected]

Returns reference to updated message from the _I 'th neighbor of variable i to variable i.

const ind_t& dai::BP::index ( size_t  i,
size_t  _I 
) const [inline, protected]

Returns constant reference to cached index for the edge between variable i and its _I 'th neighbor.

ind_t& dai::BP::index ( size_t  i,
size_t  _I 
) [inline, protected]

Returns reference to cached index for the edge between variable i and its _I 'th neighbor.

const Real& dai::BP::residual ( size_t  i,
size_t  _I 
) const [inline, protected]

Returns constant reference to residual for the edge between variable i and its _I 'th neighbor.

Real& dai::BP::residual ( size_t  i,
size_t  _I 
) [inline, protected]

Returns reference to residual for the edge between variable i and its _I 'th neighbor.

Prob dai::BP::calcIncomingMessageProduct ( size_t  I,
bool  without_i,
size_t  i 
) const [protected, virtual]

Calculate the product of factor I and the incoming messages.

If without_i == true, the message coming from variable i is omitted from the product

Note:
This function is used by calcNewMessage() and calcBeliefF()

Reimplemented in dai::FBP, and dai::TRWBP.

void dai::BP::calcNewMessage ( size_t  i,
size_t  _I 
) [protected, virtual]

Calculate the updated message from the _I 'th neighbor of variable i to variable i.

Reimplemented in dai::FBP.

void dai::BP::updateMessage ( size_t  i,
size_t  _I 
) [protected]

Replace the "old" message from the _I 'th neighbor of variable i to variable i by the "new" (updated) message.

void dai::BP::updateResidual ( size_t  i,
size_t  _I,
Real  r 
) [protected]

Set the residual (difference between new and old message) for the edge between variable i and its _I 'th neighbor to r.

void dai::BP::findMaxResidual ( size_t &  i,
size_t &  _I 
) [protected]

Finds the edge which has the maximum residual (difference between new and old message).

void dai::BP::calcBeliefV ( size_t  i,
Prob p 
) const [protected, virtual]

Calculates unnormalized belief of variable i.

Reimplemented in dai::TRWBP.

virtual void dai::BP::calcBeliefF ( size_t  I,
Prob p 
) const [inline, protected, virtual]

Calculates unnormalized belief of factor I.

Reimplemented in dai::FBP, and dai::TRWBP.

void dai::BP::construct (  )  [protected, virtual]

Helper function for constructors.

Reimplemented in dai::FBP, and dai::TRWBP.


Member Data Documentation

std::vector<std::vector<EdgeProp> > dai::BP::_edges [protected]

Stores all edge properties.

std::vector<std::vector<LutType::iterator> > dai::BP::_edge2lut [protected]

Lookup table (only used for maximum-residual BP).

LutType dai::BP::_lut [protected]

Lookup table (only used for maximum-residual BP).

Real dai::BP::_maxdiff [protected]

Maximum difference between variable beliefs encountered so far.

size_t dai::BP::_iters [protected]

Number of iterations needed.

std::vector<std::pair<std::size_t, std::size_t> > dai::BP::_sentMessages [protected]

The history of message updates (only recorded if recordSentMessages is true).

std::vector<Factor> dai::BP::_oldBeliefsV [protected]

Stores variable beliefs of previous iteration.

std::vector<Factor> dai::BP::_oldBeliefsF [protected]

Stores factor beliefs of previous iteration.

std::vector<Edge> dai::BP::_updateSeq [protected]

Stores the update schedule.

const char * dai::BP::Name = "BP" [static]

Name of this inference algorithm.

Reimplemented in dai::FBP, and dai::TRWBP.

Specifies whether the history of message updates should be recorded.


The documentation for this class was generated from the following files:

Generated on Thu Aug 5 19:04:04 2010 for libDAI by  doxygen 1.5.5