Significant improvement of documentation
[libdai.git] / include / dai / lc.h
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
4
5 This file is part of libDAI.
6
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.
11
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.
16
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
20 */
21
22
23 /// \file
24 /// \brief Defines class LC
25
26
27 #ifndef __defined_libdai_lc_h
28 #define __defined_libdai_lc_h
29
30
31 #include <string>
32 #include <dai/daialg.h>
33 #include <dai/enum.h>
34 #include <dai/factorgraph.h>
35 #include <dai/properties.h>
36 #include <dai/exceptions.h>
37
38
39 namespace dai {
40
41
42 /// Approximate inference algorithm "Loop Corrected Belief Propagation" by Mooij and Kappen
43 class LC : public DAIAlgFG {
44 private:
45 std::vector<Factor> _pancakes; // used by all LC types (psi_I is stored in the pancake)
46 std::vector<Factor> _cavitydists; // used by all LC types to store the approximate cavity distribution
47 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$
48 std::vector<std::vector<Factor> > _phis;
49
50 /// Single variable beliefs
51 std::vector<Factor> _beliefs;
52
53 /// Maximum difference encountered so far
54 double _maxdiff;
55 /// Number of iterations needed
56 size_t _iters;
57
58 public:
59 /// Parameters of this inference algorithm
60 struct Properties {
61 /// Enumeration of possible ways to initialize the cavities
62 DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM)
63
64 /// Enumeration of different update schedules
65 DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE)
66
67 /// Verbosity
68 size_t verbose;
69
70 /// Maximum number of iterations
71 size_t maxiter;
72
73 /// Tolerance
74 double tol;
75
76 /// Complete or partial reinit of cavity graphs?
77 bool reinit;
78
79 /// Damping constant
80 double damping;
81
82 /// How to initialize the cavities
83 CavityType cavity;
84
85 /// What update schedule to use
86 UpdateType updates;
87
88 /// Name of the algorithm used to initialize the cavity distributions
89 std::string cavainame; // FIXME: needs assignment operator?
90
91 /// Parameters for the algorithm used to initialize the cavity distributions
92 PropertySet cavaiopts; // FIXME: needs assignment operator?
93 } props;
94
95 /// Name of this inference algorithm
96 static const char *Name;
97
98 public:
99 /// Default constructor
100 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {}
101
102 /// Copy constructor
103 LC( const LC &x ) : DAIAlgFG(x), _pancakes(x._pancakes), _cavitydists(x._cavitydists), _phis(x._phis), _beliefs(x._beliefs), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
104
105 /// Assignment operator
106 LC& operator=( const LC &x ) {
107 if( this != &x ) {
108 DAIAlgFG::operator=( x );
109 _pancakes = x._pancakes;
110 _cavitydists = x._cavitydists;
111 _phis = x._phis;
112 _beliefs = x._beliefs;
113 _maxdiff = x._maxdiff;
114 _iters = x._iters;
115 props = x.props;
116 }
117 return *this;
118 }
119
120 /// Construct from FactorGraph fg and PropertySet opts
121 LC( const FactorGraph &fg, const PropertySet &opts );
122
123
124 /// @name General InfAlg interface
125 //@{
126 virtual LC* clone() const { return new LC(*this); }
127 virtual LC* create() const { return new LC(); }
128 virtual std::string identify() const;
129 virtual Factor belief( const Var &n ) const { return( _beliefs[findVar(n)] ); }
130 virtual Factor belief( const VarSet &/*ns*/ ) const { DAI_THROW(NOT_IMPLEMENTED); return Factor(); }
131 virtual std::vector<Factor> beliefs() const { return _beliefs; }
132 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
133 virtual void init();
134 virtual void init( const VarSet &/*ns*/ ) { init(); }
135 virtual double run();
136 virtual double maxDiff() const { return _maxdiff; }
137 virtual size_t Iterations() const { return _iters; }
138 //@}
139
140
141 /// @name Additional interface specific for LC
142 //@{
143 double CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
144 double InitCavityDists( const std::string &name, const PropertySet &opts );
145 long SetCavityDists( std::vector<Factor> &Q );
146
147 Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
148
149 void CalcBelief (size_t i);
150 const Factor &belief (size_t i) const { return _beliefs[i]; };
151 const Factor &pancake (size_t i) const { return _pancakes[i]; };
152 const Factor &cavitydist (size_t i) const { return _cavitydists[i]; };
153 //@}
154
155 private:
156 void setProperties( const PropertySet &opts );
157 PropertySet getProperties() const;
158 std::string printProperties() const;
159 };
160
161
162 } // end of namespace dai
163
164
165 #endif