5d3eb8129145c010ed22e068c47ad98e10174afa
[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 class BP : public DAIAlgFG {
44 private:
45 typedef std::vector<size_t> ind_t;
46 struct EdgeProp {
47 ind_t index;
48 Prob message;
49 Prob newMessage;
50 double residual;
51 };
52 std::vector<std::vector<EdgeProp> > _edges;
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 update schedules
62 DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
63
64 /// Enumeration of inference variants
65 DAI_ENUM(InfType,SUMPROD,MAXPROD)
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 /// Do updates in logarithmic domain?
77 bool logdomain;
78
79 /// Damping constant
80 double damping;
81
82 /// Update schedule
83 UpdateType updates;
84
85 /// Type of inference: sum-product or max-product?
86 InfType inference;
87 } props;
88
89 /// Name of this inference algorithm
90 static const char *Name;
91
92 public:
93 /// Default constructor
94 BP() : DAIAlgFG(), _edges(), _maxdiff(0.0), _iters(0U), props() {}
95
96 /// Copy constructor
97 BP( const BP &x ) : DAIAlgFG(x), _edges(x._edges), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
98
99 /// Assignment operator
100 BP& operator=( const BP &x ) {
101 if( this != &x ) {
102 DAIAlgFG::operator=( x );
103 _edges = x._edges;
104 _maxdiff = x._maxdiff;
105 _iters = x._iters;
106 props = x.props;
107 }
108 return *this;
109 }
110
111 /// Construct from FactorGraph fg and PropertySet opts
112 BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _edges(), _maxdiff(0.0), _iters(0U), props() {
113 setProperties( opts );
114 construct();
115 }
116
117
118 /// @name General InfAlg interface
119 //@{
120 virtual BP* clone() const { return new BP(*this); }
121 virtual BP* create() const { return new BP(); }
122 virtual std::string identify() const;
123 virtual Factor belief( const Var &n ) const;
124 virtual Factor belief( const VarSet &ns ) const;
125 virtual std::vector<Factor> beliefs() const;
126 virtual Real logZ() const;
127 virtual void init();
128 virtual void init( const VarSet &ns );
129 virtual double run();
130 virtual double maxDiff() const { return _maxdiff; }
131 virtual size_t Iterations() const { return _iters; }
132 //@}
133
134
135 /// @name Additional interface specific for BP
136 //@{
137 Factor beliefV( size_t i ) const;
138 Factor beliefF( size_t I ) const;
139 //@}
140
141 private:
142 const Prob & message(size_t i, size_t _I) const { return _edges[i][_I].message; }
143 Prob & message(size_t i, size_t _I) { return _edges[i][_I].message; }
144 Prob & newMessage(size_t i, size_t _I) { return _edges[i][_I].newMessage; }
145 const Prob & newMessage(size_t i, size_t _I) const { return _edges[i][_I].newMessage; }
146 ind_t & index(size_t i, size_t _I) { return _edges[i][_I].index; }
147 const ind_t & index(size_t i, size_t _I) const { return _edges[i][_I].index; }
148 double & residual(size_t i, size_t _I) { return _edges[i][_I].residual; }
149 const double & residual(size_t i, size_t _I) const { return _edges[i][_I].residual; }
150
151 void calcNewMessage( size_t i, size_t _I );
152 void updateMessage( size_t i, size_t _I ) {
153 if( props.damping == 0.0 ) {
154 message(i,_I) = newMessage(i,_I);
155 residual(i,_I) = 0.0;
156 } else {
157 message(i,_I) = (message(i,_I) ^ props.damping) * (newMessage(i,_I) ^ (1.0 - props.damping));
158 residual(i,_I) = dist( newMessage(i,_I), message(i,_I), Prob::DISTLINF );
159 }
160 }
161 void findMaxResidual( size_t &i, size_t &_I );
162
163 void construct();
164 /// 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
165 void setProperties( const PropertySet &opts );
166 PropertySet getProperties() const;
167 std::string printProperties() const;
168 };
169
170
171 } // end of namespace dai
172
173
174 #endif