14 #ifndef __defined_libdai_bp_h
15 #define __defined_libdai_bp_h
66 typedef std::vector<size_t>
ind_t;
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;
109 DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL);
149 BP() :
DAIAlgFG(), _edges(), _edge2lut(), _lut(), _maxdiff(0.0), _iters(0U), _sentMessages(), _oldBeliefsV(), _oldBeliefsF(), _updateSeq(), props(), recordSentMessages(false) {}
156 BP(
const FactorGraph &
fg,
const PropertySet &opts ) :
DAIAlgFG(fg), _edges(), _maxdiff(0.0), _iters(0U), _sentMessages(), _oldBeliefsV(), _oldBeliefsF(), _updateSeq(), props(), recordSentMessages(false) {
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;
170 DAIAlgFG::operator=( x );
173 for( LutType::iterator l = _lut.begin(); l != _lut.end(); ++l )
174 _edge2lut[l->second.first][l->second.second] = l;
192 virtual std::string
name()
const {
return "BP"; }
197 virtual std::vector<Factor>
beliefs()
const;
215 const std::vector<std::pair<size_t, size_t> >&
getSentMessages()
const {
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; }
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; }
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