Merged bp.h and bp.cpp from 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 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 /// Return number of passes over the factorgraph
95 size_t Iterations() const { return _iters; }
96
97 /// Return maximum difference between single node beliefs for two consecutive iterations
98 double maxDiff() const { return _maxdiff; }
99
100 /// Identifies itself for logging purposes
101 std::string identify() const;
102
103 /// Get single node belief
104 Factor belief( const Var &n ) const;
105
106 /// Get general belief
107 Factor belief( const VarSet &n ) const;
108
109 /// Get all beliefs
110 std::vector<Factor> beliefs() const;
111
112 /// Get log partition sum
113 Real logZ() const;
114
115 /// Clear messages and beliefs
116 void init();
117
118 /// Clear messages and beliefs corresponding to the nodes in ns
119 virtual void init( const VarSet &ns );
120
121 /// The actual approximate inference algorithm
122 double run();
123
124 Factor beliefV( size_t i ) const;
125 Factor beliefF( size_t I ) const;
126
127 private:
128 const Prob & message(size_t i, size_t _I) const { return _edges[i][_I].message; }
129 Prob & message(size_t i, size_t _I) { return _edges[i][_I].message; }
130 Prob & newMessage(size_t i, size_t _I) { return _edges[i][_I].newMessage; }
131 const Prob & newMessage(size_t i, size_t _I) const { return _edges[i][_I].newMessage; }
132 ind_t & index(size_t i, size_t _I) { return _edges[i][_I].index; }
133 const ind_t & index(size_t i, size_t _I) const { return _edges[i][_I].index; }
134 double & residual(size_t i, size_t _I) { return _edges[i][_I].residual; }
135 const double & residual(size_t i, size_t _I) const { return _edges[i][_I].residual; }
136
137 void calcNewMessage( size_t i, size_t _I );
138 void updateMessage( size_t i, size_t _I ) {
139 if( props.damping == 0.0 )
140 message(i,_I) = newMessage(i,_I);
141 else
142 message(i,_I) = (message(i,_I) ^ props.damping) * (newMessage(i,_I) ^ (1.0 - props.damping));
143 }
144 void findMaxResidual( size_t &i, size_t &_I );
145
146 /// 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
147 void construct();
148 void setProperties( const PropertySet &opts );
149 PropertySet getProperties() const;
150 std::string printProperties() const;
151 };
152
153
154 } // end of namespace dai
155
156
157 #endif