1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
10 /// \brief Defines class LC, which implements loop corrections for approximate inference
13 #ifndef __defined_libdai_lc_h
14 #define __defined_libdai_lc_h
17 #include <dai/dai_config.h>
22 #include <dai/daialg.h>
24 #include <dai/factorgraph.h>
25 #include <dai/properties.h>
26 #include <dai/exceptions.h>
32 /// Approximate inference algorithm "Loop Corrected Belief Propagation" [\ref MoK07]
33 class LC
: public DAIAlgFG
{
35 /// Stores for each variable the approximate cavity distribution multiplied with the omitted factors
36 std::vector
<Factor
> _pancakes
;
37 /// Stores for each variable the approximate cavity distribution
38 std::vector
<Factor
> _cavitydists
;
39 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$ in the paper
40 std::vector
<std::vector
<Factor
> > _phis
;
41 /// Single variable beliefs
42 std::vector
<Factor
> _beliefs
;
43 /// Maximum difference encountered so far
45 /// Number of iterations needed
51 /// Enumeration of possible ways to initialize the cavities
52 /** The following initialization methods are defined:
53 * - FULL calculates the marginal using calcMarginal()
54 * - PAIR calculates only second order interactions using calcPairBeliefs() with \a accurate == \c false
55 * - PAIR2 calculates only second order interactions using calcPairBeliefs() with \a accurate == \c true
56 * - UNIFORM uses a uniform distribution
58 DAI_ENUM(CavityType
,FULL
,PAIR
,PAIR2
,UNIFORM
);
60 /// Enumeration of different update schedules
61 /** The following update schedules are defined:
62 * - SEQFIX sequential fixed schedule
63 * - SEQRND sequential random schedule
65 DAI_ENUM(UpdateType
,SEQFIX
,SEQRND
);
67 /// Verbosity (amount of output sent to stderr)
70 /// Maximum number of iterations
73 /// Tolerance for convergence test
76 /// Complete or partial reinitialization of cavity graphs?
79 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
82 /// How to initialize the cavities
85 /// What update schedule to use
88 /// Name of the algorithm used to initialize the cavity distributions
89 std::string cavainame
;
91 /// Parameters for the algorithm used to initialize the cavity distributions
92 PropertySet cavaiopts
;
96 /// Default constructor
97 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {}
99 /// Construct from FactorGraph \a fg and PropertySet \a opts
100 /** \param fg Factor graph.
101 * \param opts Parameters @see Properties
103 LC( const FactorGraph
&fg
, const PropertySet
&opts
);
106 /// \name General InfAlg interface
108 virtual LC
* clone() const { return new LC(*this); }
109 virtual LC
* construct( const FactorGraph
&fg
, const PropertySet
&opts
) const { return new LC( fg
, opts
); }
110 virtual std::string
name() const { return "LC"; }
111 virtual Factor
belief( const Var
&v
) const { return beliefV( findVar( v
) ); }
112 virtual Factor
belief( const VarSet
&/*vs*/ ) const;
113 virtual Factor
beliefV( size_t i
) const { return _beliefs
[i
]; }
114 virtual std::vector
<Factor
> beliefs() const { return _beliefs
; }
115 virtual Real
logZ() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
117 virtual void init( const VarSet
&/*ns*/ ) { init(); }
119 virtual Real
maxDiff() const { return _maxdiff
; }
120 virtual size_t Iterations() const { return _iters
; }
121 virtual void setMaxIter( size_t maxiter
) { props
.maxiter
= maxiter
; }
122 virtual void setProperties( const PropertySet
&opts
);
123 virtual PropertySet
getProperties() const;
124 virtual std::string
printProperties() const;
127 /// \name Additional interface specific for LC
129 /// Approximates the cavity distribution of variable \a i, using the inference algorithm \a name with parameters \a opts
130 Real
CalcCavityDist( size_t i
, const std::string
&name
, const PropertySet
&opts
);
131 /// Approximates all cavity distributions using inference algorithm \a name with parameters \a opts
132 Real
InitCavityDists( const std::string
&name
, const PropertySet
&opts
);
133 /// Sets approximate cavity distributions to \a Q
134 long SetCavityDists( std::vector
<Factor
> &Q
);
135 /// Updates the belief of the Markov blanket of variable \a i based upon the information from its \a _I 'th neighboring factor
136 Factor
NewPancake (size_t i
, size_t _I
, bool & hasNaNs
);
137 /// Calculates the belief of variable \a i
138 void CalcBelief (size_t i
);
139 /// Returns the belief of the Markov blanket of variable \a i (including the variable itself)
140 const Factor
&pancake (size_t i
) const { return _pancakes
[i
]; };
141 /// Returns the approximate cavity distribution for variable \a i
142 const Factor
&cavitydist (size_t i
) const { return _cavitydists
[i
]; };
147 } // end of namespace dai