Merge branch 'joris'
[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 /// \file
24 /// \brief Defines class MF
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_mf_h
29 #define __defined_libdai_mf_h
30
31
32 #include <string>
33 #include <dai/daialg.h>
34 #include <dai/factorgraph.h>
35 #include <dai/properties.h>
36
37
38 namespace dai {
39
40
41 /// Approximate inference algorithm "Mean Field"
42 class MF : public DAIAlgFG {
43 private:
44 std::vector<Factor> _beliefs;
45 /// Maximum difference encountered so far
46 double _maxdiff;
47 /// Number of iterations needed
48 size_t _iters;
49
50 public:
51 /// Parameters of this inference algorithm
52 struct Properties {
53 /// Verbosity
54 size_t verbose;
55
56 /// Maximum number of iterations
57 size_t maxiter;
58
59 /// Tolerance
60 double tol;
61
62 /// Damping constant
63 double damping;
64 } props;
65
66 /// Name of this inference algorithm
67 static const char *Name;
68
69 public:
70 /// Default constructor
71 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
72
73 /// Copy constructor
74 MF( const MF &x ) : DAIAlgFG(x), _beliefs(x._beliefs), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
75
76 /// Assignment operator
77 MF& operator=( const MF &x ) {
78 if( this != &x ) {
79 DAIAlgFG::operator=( x );
80 _beliefs = x._beliefs;
81 _maxdiff = x._maxdiff;
82 _iters = x._iters;
83 props = x.props;
84 }
85 return *this;
86 }
87
88 /// Construct from FactorGraph fg and PropertySet opts
89 MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
90 setProperties( opts );
91 construct();
92 }
93
94
95 /// @name General InfAlg interface
96 //@{
97 virtual MF* clone() const { return new MF(*this); }
98 virtual MF* create() const { return new MF(); }
99 virtual std::string identify() const;
100 virtual Factor belief( const Var &n ) const;
101 virtual Factor belief( const VarSet &ns ) const;
102 virtual std::vector<Factor> beliefs() const;
103 virtual Real logZ() const;
104 virtual void init();
105 virtual void init( const VarSet &ns );
106 virtual double run();
107 virtual double maxDiff() const { return _maxdiff; }
108 virtual size_t Iterations() const { return _iters; }
109 //@}
110
111
112 /// @name Additional interface specific for MF
113 //@{
114 Factor beliefV( size_t i ) const;
115 //@}
116
117 private:
118 void construct();
119 void setProperties( const PropertySet &opts );
120 PropertySet getProperties() const;
121 std::string printProperties() const;
122 };
123
124
125 } // end of namespace dai
126
127
128 #endif