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