1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 This file is part of libDAI.
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 /// \brief Defines class LC
25 /// \todo Improve documentation
28 #ifndef __defined_libdai_lc_h
29 #define __defined_libdai_lc_h
33 #include <dai/daialg.h>
35 #include <dai/factorgraph.h>
36 #include <dai/properties.h>
37 #include <dai/exceptions.h>
43 /// Approximate inference algorithm "Loop Corrected Belief Propagation" by Mooij and Kappen
44 class LC
: public DAIAlgFG
{
46 std::vector
<Factor
> _pancakes
; // used by all LC types (psi_I is stored in the pancake)
47 std::vector
<Factor
> _cavitydists
; // used by all LC types to store the approximate cavity distribution
48 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$
49 std::vector
<std::vector
<Factor
> > _phis
;
51 /// Single variable beliefs
52 std::vector
<Factor
> _beliefs
;
54 /// Maximum difference encountered so far
56 /// Number of iterations needed
60 /// Parameters of this inference algorithm
62 /// Enumeration of possible ways to initialize the cavities
63 DAI_ENUM(CavityType
,FULL
,PAIR
,PAIR2
,UNIFORM
)
65 /// Enumeration of different update schedules
66 DAI_ENUM(UpdateType
,SEQFIX
,SEQRND
,NONE
)
71 /// Maximum number of iterations
77 /// Complete or partial reinit of cavity graphs?
83 /// How to initialize the cavities
86 /// What update schedule to use
89 /// Name of the algorithm used to initialize the cavity distributions
90 std::string cavainame
; // FIXME: needs assignment operator?
92 /// Parameters for the algorithm used to initialize the cavity distributions
93 PropertySet cavaiopts
; // FIXME: needs assignment operator?
96 /// Name of this inference algorithm
97 static const char *Name
;
100 /// Default constructor
101 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {}
103 /// Construct from FactorGraph fg and PropertySet opts
104 LC( const FactorGraph
&fg
, const PropertySet
&opts
);
107 /// @name General InfAlg interface
109 virtual LC
* clone() const { return new LC(*this); }
110 virtual std::string
identify() const;
111 virtual Factor
belief( const Var
&n
) const { return( _beliefs
[findVar(n
)] ); }
112 virtual Factor
belief( const VarSet
&/*ns*/ ) const { DAI_THROW(NOT_IMPLEMENTED
); return Factor(); }
113 virtual std::vector
<Factor
> beliefs() const { return _beliefs
; }
114 virtual Real
logZ() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
116 virtual void init( const VarSet
&/*ns*/ ) { init(); }
117 virtual double run();
118 virtual double maxDiff() const { return _maxdiff
; }
119 virtual size_t Iterations() const { return _iters
; }
123 /// @name Additional interface specific for LC
125 double CalcCavityDist( size_t i
, const std::string
&name
, const PropertySet
&opts
);
126 double InitCavityDists( const std::string
&name
, const PropertySet
&opts
);
127 long SetCavityDists( std::vector
<Factor
> &Q
);
129 Factor
NewPancake (size_t i
, size_t _I
, bool & hasNaNs
);
131 void CalcBelief (size_t i
);
132 const Factor
&belief (size_t i
) const { return _beliefs
[i
]; };
133 const Factor
&pancake (size_t i
) const { return _pancakes
[i
]; };
134 const Factor
&cavitydist (size_t i
) const { return _cavitydists
[i
]; };
138 void setProperties( const PropertySet
&opts
);
139 PropertySet
getProperties() const;
140 std::string
printProperties() const;
144 } // end of namespace dai