libDAI
bp.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
12 
13 
14 #ifndef __defined_libdai_bp_h
15 #define __defined_libdai_bp_h
16 
17 
18 #include <dai/dai_config.h>
19 #ifdef DAI_WITH_BP
20 
21 
22 #include <string>
23 #include <dai/daialg.h>
24 #include <dai/factorgraph.h>
25 #include <dai/properties.h>
26 #include <dai/enum.h>
27 
28 
29 namespace dai {
30 
31 
33 
63 class BP : public DAIAlgFG {
64  protected:
66  typedef std::vector<size_t> ind_t;
68  struct EdgeProp {
70  ind_t index;
77  };
79  std::vector<std::vector<EdgeProp> > _edges;
81  typedef std::multimap<Real, std::pair<size_t, size_t> > LutType;
83  std::vector<std::vector<LutType::iterator> > _edge2lut;
85  LutType _lut;
89  size_t _iters;
91  std::vector<std::pair<size_t, size_t> > _sentMessages;
93  std::vector<Factor> _oldBeliefsV;
95  std::vector<Factor> _oldBeliefsF;
97  std::vector<Edge> _updateSeq;
98 
99  public:
101  struct Properties {
103 
109  DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL);
110 
112 
116  DAI_ENUM(InfType,SUMPROD,MAXPROD);
117 
119  size_t verbose;
120 
122  size_t maxiter;
123 
125  double maxtime;
126 
129 
131  bool logdomain;
132 
135 
137  UpdateType updates;
138 
140  InfType inference;
141  } props;
142 
145 
146  public:
148 
149  BP() : DAIAlgFG(), _edges(), _edge2lut(), _lut(), _maxdiff(0.0), _iters(0U), _sentMessages(), _oldBeliefsV(), _oldBeliefsF(), _updateSeq(), props(), recordSentMessages(false) {}
151 
153 
156  BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _edges(), _maxdiff(0.0), _iters(0U), _sentMessages(), _oldBeliefsV(), _oldBeliefsF(), _updateSeq(), props(), recordSentMessages(false) {
157  setProperties( opts );
158  construct();
159  }
160 
162  BP( const BP &x ) : DAIAlgFG(x), _edges(x._edges), _edge2lut(x._edge2lut), _lut(x._lut), _maxdiff(x._maxdiff), _iters(x._iters), _sentMessages(x._sentMessages), _oldBeliefsV(x._oldBeliefsV), _oldBeliefsF(x._oldBeliefsF), _updateSeq(x._updateSeq), props(x.props), recordSentMessages(x.recordSentMessages) {
163  for( LutType::iterator l = _lut.begin(); l != _lut.end(); ++l )
164  _edge2lut[l->second.first][l->second.second] = l;
165  }
166 
168  BP& operator=( const BP &x ) {
169  if( this != &x ) {
170  DAIAlgFG::operator=( x );
171  _edges = x._edges;
172  _lut = x._lut;
173  for( LutType::iterator l = _lut.begin(); l != _lut.end(); ++l )
174  _edge2lut[l->second.first][l->second.second] = l;
175  _maxdiff = x._maxdiff;
176  _iters = x._iters;
177  _sentMessages = x._sentMessages;
178  _oldBeliefsV = x._oldBeliefsV;
179  _oldBeliefsF = x._oldBeliefsF;
180  _updateSeq = x._updateSeq;
181  props = x.props;
182  recordSentMessages = x.recordSentMessages;
183  }
184  return *this;
185  }
187 
189 
190  virtual BP* clone() const { return new BP(*this); }
191  virtual BP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new BP( fg, opts ); }
192  virtual std::string name() const { return "BP"; }
193  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
194  virtual Factor belief( const VarSet &vs ) const;
195  virtual Factor beliefV( size_t i ) const;
196  virtual Factor beliefF( size_t I ) const;
197  virtual std::vector<Factor> beliefs() const;
198  virtual Real logZ() const;
201  std::vector<size_t> findMaximum() const { return dai::findMaximum( *this ); }
202  virtual void init();
203  virtual void init( const VarSet &ns );
204  virtual Real run();
205  virtual Real maxDiff() const { return _maxdiff; }
206  virtual size_t Iterations() const { return _iters; }
207  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
208  virtual void setProperties( const PropertySet &opts );
209  virtual PropertySet getProperties() const;
210  virtual std::string printProperties() const;
212 
214 
215  const std::vector<std::pair<size_t, size_t> >& getSentMessages() const {
217  return _sentMessages;
218  }
219 
221  void clearSentMessages() { _sentMessages.clear(); }
223 
224  protected:
226  const Prob & message(size_t i, size_t _I) const { return _edges[i][_I].message; }
228  Prob & message(size_t i, size_t _I) { return _edges[i][_I].message; }
230  const Prob & newMessage(size_t i, size_t _I) const { return _edges[i][_I].newMessage; }
232  Prob & newMessage(size_t i, size_t _I) { return _edges[i][_I].newMessage; }
234  const ind_t & index(size_t i, size_t _I) const { return _edges[i][_I].index; }
236  ind_t & index(size_t i, size_t _I) { return _edges[i][_I].index; }
238  const Real & residual(size_t i, size_t _I) const { return _edges[i][_I].residual; }
240  Real & residual(size_t i, size_t _I) { return _edges[i][_I].residual; }
241 
243 
246  virtual Prob calcIncomingMessageProduct( size_t I, bool without_i, size_t i ) const;
248  virtual void calcNewMessage( size_t i, size_t _I );
250  void updateMessage( size_t i, size_t _I );
252  void updateResidual( size_t i, size_t _I, Real r );
254  void findMaxResidual( size_t &i, size_t &_I );
256  virtual void calcBeliefV( size_t i, Prob &p ) const;
258  virtual void calcBeliefF( size_t I, Prob &p ) const {
259  p = calcIncomingMessageProduct( I, false, 0 );
260  }
261 
263  virtual void construct();
264 };
265 
266 
267 } // end of namespace dai
268 
269 
270 #endif
271 
272 
273 #endif
Real damping
Damping constant (0.0 means no damping, 1.0 is maximum damping)
Definition: bp.h:134
size_t maxiter
Maximum number of iterations.
Definition: bp.h:122
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: bp.cpp:145
InfType inference
Inference variant.
Definition: bp.h:140
void clearSentMessages()
Clears history of which messages have been updated.
Definition: bp.h:221
virtual void construct()
Helper function for constructors.
Definition: bp.cpp:94
virtual BP * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: bp.h:191
double maxtime
Maximum time (in seconds)
Definition: bp.h:125
std::vector< Factor > _oldBeliefsV
Stores variable beliefs of previous iteration.
Definition: bp.h:93
std::vector< Edge > _updateSeq
Stores the update schedule.
Definition: bp.h:97
BP & operator=(const BP &x)
Assignment operator.
Definition: bp.h:168
Represents a factor graph.
Definition: factorgraph.h:65
ind_t & index(size_t i, size_t _I)
Returns reference to cached index for the edge between variable i and its _I 'th neighbor.
Definition: bp.h:236
Real _maxdiff
Maximum difference between variable beliefs encountered so far.
Definition: bp.h:87
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)...
Definition: bp.cpp:449
Type used for storing edge properties.
Definition: bp.h:68
virtual void calcBeliefV(size_t i, Prob &p) const
Calculates unnormalized belief of variable i.
Definition: bp.cpp:359
UpdateType updates
Message update schedule.
Definition: bp.h:137
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: bp.cpp:79
FactorGraph & fg()
Returns reference to underlying FactorGraph.
Definition: daialg.h:221
Real tol
Tolerance for convergence test.
Definition: bp.h:128
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
Prob & message(size_t i, size_t _I)
Returns reference to message from the _I 'th neighbor of variable i to variable i.
Definition: bp.h:228
std::vector< std::vector< LutType::iterator > > _edge2lut
Lookup table (only used for maximum-residual BP)
Definition: bp.h:83
virtual Prob calcIncomingMessageProduct(size_t I, bool without_i, size_t i) const
Calculate the product of factor I and the incoming messages.
Definition: bp.cpp:168
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: bp.cpp:65
virtual Factor beliefF(size_t I) const
Returns the (approximate) marginal probability distribution of the variables on which factor I depend...
Definition: bp.cpp:383
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: bp.h:205
std::vector< size_t > ind_t
Type used for index cache.
Definition: bp.h:66
std::vector< std::pair< size_t, size_t > > _sentMessages
The history of message updates (only recorded if recordSentMessages is true)
Definition: bp.h:91
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: bp.h:119
virtual void setMaxIter(size_t maxiter)
Sets maximum number of iterations (one iteration passes over the complete factorgraph).
Definition: bp.h:207
bool recordSentMessages
Specifies whether the history of message updates should be recorded.
Definition: bp.h:144
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
Represents a set of variables.
Definition: varset.h:94
Parameters for BP.
Definition: bp.h:101
size_t _iters
Number of iterations needed.
Definition: bp.h:89
virtual std::string name() const
Returns the name of the algorithm.
Definition: bp.h:192
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 ...
Definition: bp.cpp:467
std::multimap< Real, std::pair< size_t, size_t > > LutType
Type of lookup table (only used for maximum-residual BP)
Definition: bp.h:81
virtual void setProperties(const PropertySet &opts)
Definition: bp.cpp:33
std::vector< size_t > findMaximum(const InfAlg &obj)
Calculates the joint state of all variables that has maximum probability, according to the inference ...
Definition: daialg.cpp:211
const Prob & message(size_t i, size_t _I) const
Returns constant reference to message from the _I 'th neighbor of variable i to variable i...
Definition: bp.h:226
BP()
Default constructor.
Definition: bp.h:150
Real & residual(size_t i, size_t _I)
Returns reference to residual for the edge between variable i and its _I 'th neighbor.
Definition: bp.h:240
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
const Real & residual(size_t i, size_t _I) const
Returns constant reference to residual for the edge between variable i and its _I 'th neighbor...
Definition: bp.h:238
Approximate inference algorithm "(Loopy) Belief Propagation".
Definition: bp.h:63
BP(const FactorGraph &fg, const PropertySet &opts)
Construct from FactorGraph fg and PropertySet opts.
Definition: bp.h:156
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
virtual Factor beliefV(size_t i) const
Returns the (approximate) marginal probability distribution of the variable with index i...
Definition: bp.cpp:369
virtual std::vector< Factor > beliefs() const
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
Definition: bp.cpp:397
virtual void calcBeliefF(size_t I, Prob &p) const
Calculates unnormalized belief of factor I.
Definition: bp.h:258
virtual Real run()
Runs the approximate inference algorithm.
Definition: bp.cpp:265
Real residual
Residual for this edge.
Definition: bp.h:76
Prob message
Old message living on this edge.
Definition: bp.h:72
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
std::vector< std::vector< EdgeProp > > _edges
Stores all edge properties.
Definition: bp.h:79
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
virtual Factor belief(const Var &v) const
Returns the (approximate) marginal probability distribution of a variable.
Definition: bp.h:193
ind_t index
Index cached for this edge.
Definition: bp.h:70
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
BP(const BP &x)
Copy constructor.
Definition: bp.h:162
std::vector< Factor > _oldBeliefsF
Stores factor beliefs of previous iteration.
Definition: bp.h:95
Allows the user to specify which algorithms will be built into libDAI.
std::vector< size_t > findMaximum() const
Definition: bp.h:201
Prob & newMessage(size_t i, size_t _I)
Returns reference to updated message from the _I 'th neighbor of variable i to variable i...
Definition: bp.h:232
void findMaxResidual(size_t &i, size_t &_I)
Finds the edge which has the maximum residual (difference between new and old message) ...
Definition: bp.cpp:159
DAI_ENUM(UpdateType, SEQFIX, SEQRND, SEQMAX, PARALL)
Enumeration of possible update schedules.
const Prob & newMessage(size_t i, size_t _I) const
Returns constant reference to updated message from the _I 'th neighbor of variable i to variable i...
Definition: bp.h:230
bool logdomain
Whether updates should be done in logarithmic domain or not.
Definition: bp.h:131
virtual void calcNewMessage(size_t i, size_t _I)
Calculate the updated message from the _I 'th neighbor of variable i to variable i.
Definition: bp.cpp:211
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
virtual BP * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: bp.h:190
Prob newMessage
New message living on this edge.
Definition: bp.h:74
LutType _lut
Lookup table (only used for maximum-residual BP)
Definition: bp.h:85
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: bp.h:206
const std::vector< std::pair< size_t, size_t > > & getSentMessages() const
Returns history of which messages have been updated.
Definition: bp.h:216
const ind_t & index(size_t i, size_t _I) const
Returns constant reference to cached index for the edge between variable i and its _I 'th neighbor...
Definition: bp.h:234
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: bp.cpp:424