Updated copyrights
[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 #ifndef __defined_libdai_lc_h
24 #define __defined_libdai_lc_h
25
26
27 #include <string>
28 #include <dai/daialg.h>
29 #include <dai/enum.h>
30 #include <dai/factorgraph.h>
31 #include <dai/properties.h>
32 #include <dai/exceptions.h>
33
34
35 namespace dai {
36
37
38 class LC : public DAIAlgFG {
39 private:
40 std::vector<Factor> _pancakes; // used by all LC types (psi_I is stored in the pancake)
41 std::vector<Factor> _cavitydists; // used by all LC types to store the approximate cavity distribution
42 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$
43 std::vector<std::vector<Factor> > _phis;
44
45 /// Single variable beliefs
46 std::vector<Factor> _beliefs;
47
48 /// Maximum difference encountered so far
49 double _maxdiff;
50 /// Number of iterations needed
51 size_t _iters;
52
53 public:
54 struct Properties {
55 size_t verbose;
56 size_t maxiter;
57 double tol;
58 bool reinit;
59 double damping;
60 DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM)
61 CavityType cavity;
62 DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE)
63 UpdateType updates;
64 std::string cavainame; // FIXME: needs assignment operator?
65 PropertySet cavaiopts; // FIXME: needs assignment operator?
66 } props;
67 /// Name of this inference method
68 static const char *Name;
69
70 public:
71 /// Default constructor
72 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {}
73
74 /// Construct from FactorGraph fg and PropertySet opts
75 LC( const FactorGraph &fg, const PropertySet &opts );
76
77 /// Copy constructor
78 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) {}
79
80 /// Clone *this (virtual copy constructor)
81 virtual LC* clone() const { return new LC(*this); }
82
83 /// Create (virtual default constructor)
84 virtual LC* create() const { return new LC(); }
85
86 /// Assignment operator
87 LC& operator=( const LC &x ) {
88 if( this != &x ) {
89 DAIAlgFG::operator=( x );
90 _pancakes = x._pancakes;
91 _cavitydists = x._cavitydists;
92 _phis = x._phis;
93 _beliefs = x._beliefs;
94 _maxdiff = x._maxdiff;
95 _iters = x._iters;
96 props = x.props;
97 }
98 return *this;
99 }
100
101 /// Identifies itself for logging purposes
102 virtual std::string identify() const;
103
104 /// Get single node belief
105 virtual Factor belief( const Var &n ) const { return( _beliefs[findVar(n)] ); }
106
107 /// Get general belief
108 virtual Factor belief( const VarSet &/*ns*/ ) const {
109 DAI_THROW(NOT_IMPLEMENTED);
110 return Factor();
111 }
112
113 /// Get all beliefs
114 virtual std::vector<Factor> beliefs() const { return _beliefs; }
115
116 /// Get log partition sum
117 virtual Real logZ() const {
118 DAI_THROW(NOT_IMPLEMENTED);
119 return 0.0;
120 }
121
122 /// Clear messages and beliefs
123 virtual void init();
124
125 /// Clear messages and beliefs corresponding to the nodes in ns
126 virtual void init( const VarSet &/*ns*/ ) { init(); }
127
128 /// The actual approximate inference algorithm
129 virtual double run();
130
131 /// Return maximum difference between single node beliefs in the last pass
132 virtual double maxDiff() const { return _maxdiff; }
133
134 /// Return number of passes over the factorgraph
135 virtual size_t Iterations() const { return _iters; }
136
137 double CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
138 double InitCavityDists( const std::string &name, const PropertySet &opts );
139 long SetCavityDists( std::vector<Factor> &Q );
140
141 Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
142
143 void CalcBelief (size_t i);
144 const Factor &belief (size_t i) const { return _beliefs[i]; };
145 const Factor &pancake (size_t i) const { return _pancakes[i]; };
146 const Factor &cavitydist (size_t i) const { return _cavitydists[i]; };
147
148 void setProperties( const PropertySet &opts );
149 PropertySet getProperties() const;
150 std::string printProperties() const;
151 };
152
153
154 } // end of namespace dai
155
156
157 #endif