Updated copyrights
[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 #ifndef __defined_libdai_bp_h
24 #define __defined_libdai_bp_h
25
26
27 #include <string>
28 #include <dai/daialg.h>
29 #include <dai/factorgraph.h>
30 #include <dai/properties.h>
31 #include <dai/enum.h>
32
33
34 namespace dai {
35
36
37 class BP : public DAIAlgFG {
38 private:
39 typedef std::vector<size_t> ind_t;
40 struct EdgeProp {
41 ind_t index;
42 Prob message;
43 Prob newMessage;
44 double residual;
45 };
46 std::vector<std::vector<EdgeProp> > _edges;
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 logdomain;
58 double damping;
59 DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
60 UpdateType updates;
61 DAI_ENUM(InfType,SUMPROD,MAXPROD)
62 InfType inference;
63 } props;
64 static const char *Name;
65
66 public:
67 /// Default constructor
68 BP() : DAIAlgFG(), _edges(), _maxdiff(0.0), _iters(0U), props() {}
69
70 /// Construct from FactorGraph fg and PropertySet opts
71 BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _edges(), _maxdiff(0.0), _iters(0U), props() {
72 setProperties( opts );
73 construct();
74 }
75
76 /// Copy constructor
77 BP( const BP &x ) : DAIAlgFG(x), _edges(x._edges), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
78
79 /// Clone *this (virtual copy constructor)
80 virtual BP* clone() const { return new BP(*this); }
81
82 /// Create (virtual default constructor)
83 virtual BP* create() const { return new BP(); }
84
85 /// Assignment operator
86 BP& operator=( const BP &x ) {
87 if( this != &x ) {
88 DAIAlgFG::operator=( x );
89 _edges = x._edges;
90 _maxdiff = x._maxdiff;
91 _iters = x._iters;
92 props = x.props;
93 }
94 return *this;
95 }
96
97 /// Identifies itself for logging purposes
98 virtual std::string identify() const;
99
100 /// Get single node belief
101 virtual Factor belief( const Var &n ) const;
102
103 /// Get general belief
104 virtual Factor belief( const VarSet &ns ) const;
105
106 /// Get all beliefs
107 virtual std::vector<Factor> beliefs() const;
108
109 /// Get log partition sum
110 virtual Real logZ() const;
111
112 /// Clear messages and beliefs
113 virtual void init();
114
115 /// Clear messages and beliefs corresponding to the nodes in ns
116 virtual void init( const VarSet &ns );
117
118 /// The actual approximate inference algorithm
119 virtual double run();
120
121 /// Return maximum difference between single node beliefs in the last pass
122 virtual double maxDiff() const { return _maxdiff; }
123
124 /// Return number of passes over the factorgraph
125 virtual size_t Iterations() const { return _iters; }
126
127
128 Factor beliefV( size_t i ) const;
129 Factor beliefF( size_t I ) const;
130
131 private:
132 const Prob & message(size_t i, size_t _I) const { return _edges[i][_I].message; }
133 Prob & message(size_t i, size_t _I) { return _edges[i][_I].message; }
134 Prob & newMessage(size_t i, size_t _I) { return _edges[i][_I].newMessage; }
135 const Prob & newMessage(size_t i, size_t _I) const { return _edges[i][_I].newMessage; }
136 ind_t & index(size_t i, size_t _I) { return _edges[i][_I].index; }
137 const ind_t & index(size_t i, size_t _I) const { return _edges[i][_I].index; }
138 double & residual(size_t i, size_t _I) { return _edges[i][_I].residual; }
139 const double & residual(size_t i, size_t _I) const { return _edges[i][_I].residual; }
140
141 void calcNewMessage( size_t i, size_t _I );
142 void updateMessage( size_t i, size_t _I ) {
143 if( props.damping == 0.0 ) {
144 message(i,_I) = newMessage(i,_I);
145 residual(i,_I) = 0.0;
146 } else {
147 message(i,_I) = (message(i,_I) ^ props.damping) * (newMessage(i,_I) ^ (1.0 - props.damping));
148 residual(i,_I) = dist( newMessage(i,_I), message(i,_I), Prob::DISTLINF );
149 }
150 }
151 void findMaxResidual( size_t &i, size_t &_I );
152
153 void construct();
154 /// 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
155 void setProperties( const PropertySet &opts );
156 PropertySet getProperties() const;
157 std::string printProperties() const;
158 };
159
160
161 } // end of namespace dai
162
163
164 #endif