Updated copyrights
[libdai.git] / include / dai / mf.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_mf_h
24 #define __defined_libdai_mf_h
25
26
27 #include <string>
28 #include <dai/daialg.h>
29 #include <dai/factorgraph.h>
30 #include <dai/properties.h>
31
32
33 namespace dai {
34
35
36 class MF : public DAIAlgFG {
37 private:
38 std::vector<Factor> _beliefs;
39 /// Maximum difference encountered so far
40 double _maxdiff;
41 /// Number of iterations needed
42 size_t _iters;
43
44 public:
45 struct Properties {
46 size_t verbose;
47 size_t maxiter;
48 double tol;
49 double damping;
50 } props;
51 static const char *Name;
52
53 public:
54 /// Default constructor
55 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
56
57 /// Construct from FactorGraph fg and PropertySet opts
58 MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
59 setProperties( opts );
60 construct();
61 }
62
63 /// Copy constructor
64 MF( const MF &x ) : DAIAlgFG(x), _beliefs(x._beliefs), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
65
66 /// Clone *this (virtual copy constructor)
67 virtual MF* clone() const { return new MF(*this); }
68
69 /// Create (virtual default constructor)
70 virtual MF* create() const { return new MF(); }
71
72 /// Assignment operator
73 MF& operator=( const MF &x ) {
74 if( this != &x ) {
75 DAIAlgFG::operator=( x );
76 _beliefs = x._beliefs;
77 _maxdiff = x._maxdiff;
78 _iters = x._iters;
79 props = x.props;
80 }
81 return *this;
82 }
83
84 /// Identifies itself for logging purposes
85 virtual std::string identify() const;
86
87 /// Get single node belief
88 virtual Factor belief( const Var &n ) const;
89
90 /// Get general belief
91 virtual Factor belief( const VarSet &ns ) const;
92
93 /// Get all beliefs
94 virtual std::vector<Factor> beliefs() const;
95
96 /// Get log partition sum
97 virtual Real logZ() const;
98
99 /// Clear messages and beliefs
100 virtual void init();
101
102 /// Clear messages and beliefs corresponding to the nodes in ns
103 virtual void init( const VarSet &ns );
104
105 /// The actual approximate inference algorithm
106 virtual double run();
107
108 /// Return maximum difference between single node beliefs in the last pass
109 virtual double maxDiff() const { return _maxdiff; }
110
111 /// Return number of passes over the factorgraph
112 virtual size_t Iterations() const { return _iters; }
113
114
115 void construct();
116
117 void setProperties( const PropertySet &opts );
118 PropertySet getProperties() const;
119 std::string printProperties() const;
120
121 Factor beliefV( size_t i ) const;
122 };
123
124
125 } // end of namespace dai
126
127
128 #endif