Merged mf.* from SVN head (which implements damping)...
[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 protected:
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
47 public:
48 struct Properties {
49 size_t verbose;
50 size_t maxiter;
51 double tol;
52 bool logdomain;
53 DAI_ENUM(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
54 UpdateType updates;
55 } props;
56 double maxdiff;
57
58 public:
59 /// Default constructor
60 BP() : DAIAlgFG(), edges(), props(), maxdiff(0.0) {};
61 /// Copy constructor
62 BP( const BP & x ) : DAIAlgFG(x), edges(x.edges), props(x.props), maxdiff(x.maxdiff) {};
63 /// Clone *this (virtual copy constructor)
64 BP* clone() const { return new BP(*this); }
65 /// Create (virtual constructor)
66 virtual BP* create() const { return new BP; }
67 /// Construct from FactorGraph fg and PropertySet opts
68 BP( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), edges(), props(), maxdiff(0.0) {
69 setProperties( opts );
70 construct();
71 }
72 /// Assignment operator
73 BP& operator=( const BP & x ) {
74 if( this != &x ) {
75 DAIAlgFG::operator=( x );
76 edges = x.edges;
77 props = x.props;
78 maxdiff = x.maxdiff;
79 }
80 return *this;
81 }
82
83 static const char *Name;
84
85 Prob & message(size_t i, size_t _I) { return edges[i][_I].message; }
86 const Prob & message(size_t i, size_t _I) const { return edges[i][_I].message; }
87 Prob & newMessage(size_t i, size_t _I) { return edges[i][_I].newMessage; }
88 const Prob & newMessage(size_t i, size_t _I) const { return edges[i][_I].newMessage; }
89 ind_t & index(size_t i, size_t _I) { return edges[i][_I].index; }
90 const ind_t & index(size_t i, size_t _I) const { return edges[i][_I].index; }
91 double & residual(size_t i, size_t _I) { return edges[i][_I].residual; }
92 const double & residual(size_t i, size_t _I) const { return edges[i][_I].residual; }
93
94 std::string identify() const;
95 void construct();
96 void init();
97 /// Clear messages and beliefs corresponding to the nodes in ns
98 virtual void init( const VarSet &ns );
99 double run();
100
101 void findMaxResidual( size_t &i, size_t &_I );
102 void calcNewMessage( size_t i, size_t _I );
103 Factor beliefV (size_t i) const;
104 Factor beliefF (size_t I) const;
105 Factor belief (const Var &n) const;
106 Factor belief (const VarSet &n) const;
107 std::vector<Factor> beliefs() const;
108 Real logZ() const;
109
110 void restoreFactors( const VarSet &ns ) { FactorGraph::restoreFactors(ns); init(ns); }
111
112 /// 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
113 void setProperties( const PropertySet &opts );
114 PropertySet getProperties() const;
115 std::string printProperties() const;
116 double maxDiff() const { return maxdiff; }
117 };
118
119
120 } // end of namespace dai
121
122
123 #endif