Merged SVN head ...
[libdai.git] / include / dai / lc.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_lc_h
23 #define __defined_libdai_lc_h
24
25
26 #include <string>
27 #include <dai/daialg.h>
28 #include <dai/enum.h>
29 #include <dai/factorgraph.h>
30 #include <dai/properties.h>
31 #include <dai/exceptions.h>
32
33
34 namespace dai {
35
36
37 class LC : public DAIAlgFG {
38 protected:
39 std::vector<Factor> _pancakes; // used by all LC types (psi_I is stored in the pancake)
40 std::vector<Factor> _cavitydists; // used by all LC types to store the approximate cavity distribution
41 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$
42 std::vector<std::vector<Factor> > _phis;
43
44 /// Single variable beliefs
45 std::vector<Factor> _beliefs;
46
47 /// Maximum difference encountered so far
48 double _maxdiff;
49 /// Number of iterations needed
50 size_t _iters;
51
52 public:
53 struct Properties {
54 size_t verbose;
55 size_t maxiter;
56 double tol;
57 bool reinit;
58 double damping;
59 DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM)
60 CavityType cavity;
61 DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE)
62 UpdateType updates;
63 std::string cavainame; // FIXME: needs assignment operator?
64 PropertySet cavaiopts; // FIXME: needs assignment operator?
65 } props;
66 /// Name of this inference method
67 static const char *Name;
68
69 public:
70 /// Default constructor
71 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {}
72
73 /// Construct from FactorGraph fg and PropertySet opts
74 LC( const FactorGraph &fg, const PropertySet &opts );
75
76 /// Copy constructor
77 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) {}
78
79 /// Clone *this (virtual copy constructor)
80 virtual LC* clone() const { return new LC(*this); }
81
82 /// Create (virtual default constructor)
83 virtual LC* create() const { return new LC(); }
84
85 /// Assignment operator
86 LC& operator=( const LC &x ) {
87 if( this != &x ) {
88 DAIAlgFG::operator=( x );
89 _pancakes = x._pancakes;
90 _cavitydists = x._cavitydists;
91 _phis = x._phis;
92 _beliefs = x._beliefs;
93 _maxdiff = x._maxdiff;
94 _iters = x._iters;
95 props = x.props;
96 }
97 return *this;
98 }
99
100 /// Identifies itself for logging purposes
101 virtual std::string identify() const;
102
103 /// Get single node belief
104 virtual Factor belief( const Var &n ) const { return( _beliefs[findVar(n)] ); }
105
106 /// Get general belief
107 virtual Factor belief( const VarSet &/*ns*/ ) const {
108 DAI_THROW(NOT_IMPLEMENTED);
109 return Factor();
110 }
111
112 /// Get all beliefs
113 virtual std::vector<Factor> beliefs() const { return _beliefs; }
114
115 /// Get log partition sum
116 virtual Real logZ() const {
117 DAI_THROW(NOT_IMPLEMENTED);
118 return 0.0;
119 }
120
121 /// Clear messages and beliefs
122 virtual void init();
123
124 /// Clear messages and beliefs corresponding to the nodes in ns
125 virtual void init( const VarSet &/*ns*/ ) { init(); }
126
127 /// The actual approximate inference algorithm
128 virtual double run();
129
130 /// Return maximum difference between single node beliefs in the last pass
131 virtual double maxDiff() const { return _maxdiff; }
132
133 /// Return number of passes over the factorgraph
134 virtual size_t Iterations() const { return _iters; }
135
136 double CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
137 double InitCavityDists( const std::string &name, const PropertySet &opts );
138 long SetCavityDists( std::vector<Factor> &Q );
139
140 Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
141
142 void CalcBelief (size_t i);
143 const Factor &belief (size_t i) const { return _beliefs[i]; };
144 const Factor &pancake (size_t i) const { return _pancakes[i]; };
145 const Factor &cavitydist (size_t i) const { return _cavitydists[i]; };
146
147 void setProperties( const PropertySet &opts );
148 PropertySet getProperties() const;
149 std::string printProperties() const;
150 };
151
152
153 } // end of namespace dai
154
155
156 #endif