Replaced Name members by name() virtual functions (fixing a bug in matlab/dai.cpp)
[libdai.git] / include / dai / mf.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines class MF which implements the Mean Field algorithm
14
15
16 #ifndef __defined_libdai_mf_h
17 #define __defined_libdai_mf_h
18
19
20 #include <string>
21 #include <dai/enum.h>
22 #include <dai/daialg.h>
23 #include <dai/factorgraph.h>
24 #include <dai/properties.h>
25
26
27 namespace dai {
28
29
30 /// Approximate inference algorithm "Mean Field"
31 /** The Mean Field algorithm iteratively calculates approximations of
32 * single variable marginals (beliefs). The update equation for
33 * a single belief \f$b_i\f$ is given by:
34 * \f[ b_i^{\mathrm{new}}(x_i) \propto \prod_{I\in N_i} \exp \left( \sum_{x_{N_I \setminus \{i\}}} \log f_I(x_I) \prod_{j \in N_I \setminus \{i\}} b_j(x_j) \right) \f]
35 * for naive mean field and by
36 * \f[ b_i^{\mathrm{new}}(x_i) \propto \prod_{I\in N_i} \left( \sum_{x_{N_I \setminus \{i\}}} f_I(x_I) \prod_{j \in N_I \setminus \{i\}} b_j(x_j) \right) \f]
37 * for hard-spin mean field.
38 * These update equations are performed for all variables until convergence.
39 */
40 class MF : public DAIAlgFG {
41 private:
42 /// Current approximations of single variable marginals
43 std::vector<Factor> _beliefs;
44 /// Maximum difference encountered so far
45 Real _maxdiff;
46 /// Number of iterations needed
47 size_t _iters;
48
49 public:
50 /// Parameters for MF
51 struct Properties {
52 /// Enumeration of possible message initializations
53 DAI_ENUM(InitType,UNIFORM,RANDOM);
54
55 /// Enumeration of possible update types
56 DAI_ENUM(UpdateType,NAIVE,HARDSPIN);
57
58 /// Verbosity (amount of output sent to stderr)
59 size_t verbose;
60
61 /// Maximum number of iterations
62 size_t maxiter;
63
64 /// Tolerance for convergence test
65 Real tol;
66
67 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
68 Real damping;
69
70 /// How to initialize the messages/beliefs
71 InitType init;
72
73 /// How to update the messages/beliefs
74 UpdateType updates;
75 } props;
76
77 public:
78 /// \name Constructors/destructors
79 //@{
80 /// Default constructor
81 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
82
83 /// Construct from FactorGraph \a fg and PropertySet \a opts
84 /** \param fg Factor graph.
85 * \param opts Parameters @see Properties
86 */
87 MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
88 setProperties( opts );
89 construct();
90 }
91 //@}
92
93 /// \name General InfAlg interface
94 //@{
95 virtual MF* clone() const { return new MF(*this); }
96 virtual std::string name() const { return "MF"; }
97 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
98 virtual Factor belief( const VarSet &vs ) const;
99 virtual Factor beliefV( size_t i ) const;
100 virtual std::vector<Factor> beliefs() const;
101 virtual Real logZ() const;
102 virtual void init();
103 virtual void init( const VarSet &ns );
104 virtual Real run();
105 virtual Real maxDiff() const { return _maxdiff; }
106 virtual size_t Iterations() const { return _iters; }
107 virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
108 virtual void setProperties( const PropertySet &opts );
109 virtual PropertySet getProperties() const;
110 virtual std::string printProperties() const;
111 //@}
112
113 private:
114 /// Helper function for constructors
115 void construct();
116
117 /// Calculates an updated belief of variable \a i
118 Factor calcNewBelief( size_t i );
119 };
120
121
122 } // end of namespace dai
123
124
125 #endif