Merged TODO and FILEFORMAT into doxygen documentation, switched Makefile.win to GNU...
[libdai.git] / include / dai / bp.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 BP
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_bp_h
29 #define __defined_libdai_bp_h
30
31
32 #include <string>
33 #include <dai/daialg.h>
34 #include <dai/factorgraph.h>
35 #include <dai/properties.h>
36 #include <dai/enum.h>
37
38
39 namespace dai {
40
41
42 /// Approximate inference algorithm "(Loopy) Belief Propagation"
43 /** \todo Optimize BP_SEQMAX (it should use a better data structure than a vector for the residuals).
44 */
45 class BP : public DAIAlgFG {
46 private:
47 typedef std::vector<size_t> ind_t;
48 struct EdgeProp {
49 ind_t index;
50 Prob message;
51 Prob newMessage;
52 double residual;
53 };
54 std::vector<std::vector<EdgeProp> > _edges;
55 /// Maximum difference encountered so far
56 double _maxdiff;
57 /// Number of iterations needed
58 size_t _iters;
59
60 public:
61 /// Parameters of this inference algorithm
62 struct Properties {
63 /// Enumeration of possible update schedules
64 DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
65
66 /// Enumeration of inference variants
67 DAI_ENUM(InfType,SUMPROD,MAXPROD)
68
69 /// Verbosity
70 size_t verbose;
71
72 /// Maximum number of iterations
73 size_t maxiter;
74
75 /// Tolerance
76 double tol;
77
78 /// Do updates in logarithmic domain?
79 bool logdomain;
80
81 /// Damping constant
82 double damping;
83
84 /// Update schedule
85 UpdateType updates;
86
87 /// Type of inference: sum-product or max-product?
88 InfType inference;
89 } props;
90
91 /// Name of this inference algorithm
92 static const char *Name;
93
94 public:
95 /// Default constructor
96 BP() : DAIAlgFG(), _edges(), _maxdiff(0.0), _iters(0U), props() {}
97
98 /// Copy constructor
99 BP( const BP &x ) : DAIAlgFG(x), _edges(x._edges), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
100
101 /// Assignment operator
102 BP& operator=( const BP &x ) {
103 if( this != &x ) {
104 DAIAlgFG::operator=( x );
105 _edges = x._edges;
106 _maxdiff = x._maxdiff;
107 _iters = x._iters;
108 props = x.props;
109 }
110 return *this;
111 }
112
113 /// Construct from FactorGraph fg and PropertySet opts
114 BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _edges(), _maxdiff(0.0), _iters(0U), props() {
115 setProperties( opts );
116 construct();
117 }
118
119
120 /// @name General InfAlg interface
121 //@{
122 virtual BP* clone() const { return new BP(*this); }
123 virtual BP* create() const { return new BP(); }
124 virtual std::string identify() const;
125 virtual Factor belief( const Var &n ) const;
126 virtual Factor belief( const VarSet &ns ) const;
127 virtual std::vector<Factor> beliefs() const;
128 virtual Real logZ() const;
129 virtual void init();
130 virtual void init( const VarSet &ns );
131 virtual double run();
132 virtual double maxDiff() const { return _maxdiff; }
133 virtual size_t Iterations() const { return _iters; }
134 //@}
135
136
137 /// @name Additional interface specific for BP
138 //@{
139 Factor beliefV( size_t i ) const;
140 Factor beliefF( size_t I ) const;
141 //@}
142
143 private:
144 const Prob & message(size_t i, size_t _I) const { return _edges[i][_I].message; }
145 Prob & message(size_t i, size_t _I) { return _edges[i][_I].message; }
146 Prob & newMessage(size_t i, size_t _I) { return _edges[i][_I].newMessage; }
147 const Prob & newMessage(size_t i, size_t _I) const { return _edges[i][_I].newMessage; }
148 ind_t & index(size_t i, size_t _I) { return _edges[i][_I].index; }
149 const ind_t & index(size_t i, size_t _I) const { return _edges[i][_I].index; }
150 double & residual(size_t i, size_t _I) { return _edges[i][_I].residual; }
151 const double & residual(size_t i, size_t _I) const { return _edges[i][_I].residual; }
152
153 void calcNewMessage( size_t i, size_t _I );
154 void updateMessage( size_t i, size_t _I ) {
155 if( props.damping == 0.0 ) {
156 message(i,_I) = newMessage(i,_I);
157 residual(i,_I) = 0.0;
158 } else {
159 message(i,_I) = (message(i,_I) ^ props.damping) * (newMessage(i,_I) ^ (1.0 - props.damping));
160 residual(i,_I) = dist( newMessage(i,_I), message(i,_I), Prob::DISTLINF );
161 }
162 }
163 void findMaxResidual( size_t &i, size_t &_I );
164
165 void construct();
166 /// Set Props according to the PropertySet opts, where the values can be stored as std::strings or as the type of the corresponding Props member
167 void setProperties( const PropertySet &opts );
168 PropertySet getProperties() const;
169 std::string printProperties() const;
170 };
171
172
173 } // end of namespace dai
174
175
176 #endif