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