Merged SVN head ...
[libdai.git] / include / dai / bp.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_bp_h
23 #define __defined_libdai_bp_h
24
25
26 #include <string>
27 #include <dai/daialg.h>
28 #include <dai/factorgraph.h>
29 #include <dai/properties.h>
30 #include <dai/enum.h>
31
32
33 namespace dai {
34
35
36 class BP : public DAIAlgFG {
37 private:
38 typedef std::vector<size_t> ind_t;
39 struct EdgeProp {
40 ind_t index;
41 Prob message;
42 Prob newMessage;
43 double residual;
44 };
45 std::vector<std::vector<EdgeProp> > _edges;
46 /// Maximum difference encountered so far
47 double _maxdiff;
48 /// Number of iterations needed
49 size_t _iters;
50
51 public:
52 struct Properties {
53 size_t verbose;
54 size_t maxiter;
55 double tol;
56 bool logdomain;
57 double damping;
58 DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
59 UpdateType updates;
60 } props;
61 static const char *Name;
62
63 public:
64 /// Default constructor
65 BP() : DAIAlgFG(), _edges(), _maxdiff(0.0), _iters(0U), props() {}
66
67 /// Construct from FactorGraph fg and PropertySet opts
68 BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _edges(), _maxdiff(0.0), _iters(0U), props() {
69 setProperties( opts );
70 construct();
71 }
72
73 /// Copy constructor
74 BP( const BP &x ) : DAIAlgFG(x), _edges(x._edges), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
75
76 /// Clone *this (virtual copy constructor)
77 virtual BP* clone() const { return new BP(*this); }
78
79 /// Create (virtual default constructor)
80 virtual BP* create() const { return new BP(); }
81
82 /// Assignment operator
83 BP& operator=( const BP &x ) {
84 if( this != &x ) {
85 DAIAlgFG::operator=( x );
86 _edges = x._edges;
87 _maxdiff = x._maxdiff;
88 _iters = x._iters;
89 props = x.props;
90 }
91 return *this;
92 }
93
94 /// Identifies itself for logging purposes
95 virtual std::string identify() const;
96
97 /// Get single node belief
98 virtual Factor belief( const Var &n ) const;
99
100 /// Get general belief
101 virtual Factor belief( const VarSet &ns ) const;
102
103 /// Get all beliefs
104 virtual std::vector<Factor> beliefs() const;
105
106 /// Get log partition sum
107 virtual Real logZ() const;
108
109 /// Clear messages and beliefs
110 virtual void init();
111
112 /// Clear messages and beliefs corresponding to the nodes in ns
113 virtual void init( const VarSet &ns );
114
115 /// The actual approximate inference algorithm
116 virtual double run();
117
118 /// Return maximum difference between single node beliefs in the last pass
119 virtual double maxDiff() const { return _maxdiff; }
120
121 /// Return number of passes over the factorgraph
122 virtual size_t Iterations() const { return _iters; }
123
124
125 Factor beliefV( size_t i ) const;
126 Factor beliefF( size_t I ) const;
127
128 private:
129 const Prob & message(size_t i, size_t _I) const { return _edges[i][_I].message; }
130 Prob & message(size_t i, size_t _I) { return _edges[i][_I].message; }
131 Prob & newMessage(size_t i, size_t _I) { return _edges[i][_I].newMessage; }
132 const Prob & newMessage(size_t i, size_t _I) const { return _edges[i][_I].newMessage; }
133 ind_t & index(size_t i, size_t _I) { return _edges[i][_I].index; }
134 const ind_t & index(size_t i, size_t _I) const { return _edges[i][_I].index; }
135 double & residual(size_t i, size_t _I) { return _edges[i][_I].residual; }
136 const double & residual(size_t i, size_t _I) const { return _edges[i][_I].residual; }
137
138 void calcNewMessage( size_t i, size_t _I );
139 void updateMessage( size_t i, size_t _I ) {
140 if( props.damping == 0.0 ) {
141 message(i,_I) = newMessage(i,_I);
142 residual(i,_I) = 0.0;
143 } else {
144 message(i,_I) = (message(i,_I) ^ props.damping) * (newMessage(i,_I) ^ (1.0 - props.damping));
145 residual(i,_I) = dist( newMessage(i,_I), message(i,_I), Prob::DISTLINF );
146 }
147 }
148 void findMaxResidual( size_t &i, size_t &_I );
149
150 void construct();
151 /// 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
152 void setProperties( const PropertySet &opts );
153 PropertySet getProperties() const;
154 std::string printProperties() const;
155 };
156
157
158 } // end of namespace dai
159
160
161 #endif