1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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]
9 */
12 /// \file
13 /// \brief Defines class MF which implements the Mean Field algorithm
16 #ifndef __defined_libdai_mf_h
17 #define __defined_libdai_mf_h
20 #include <string>
21 #include <dai/daialg.h>
22 #include <dai/factorgraph.h>
23 #include <dai/properties.h>
26 namespace dai {
29 /// Approximate inference algorithm "Mean Field"
30 /** The Mean Field algorithm iteratively calculates approximations of
31 * single variable marginals (beliefs). The update equation for
32 * a single belief \f$b_i\f$ is given by:
33 * \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]
34 * These update equations are performed for all variables until convergence.
35 */
36 class MF : public DAIAlgFG {
37 private:
38 /// Current approximations of single variable marginals
39 std::vector<Factor> _beliefs;
40 /// Maximum difference encountered so far
41 Real _maxdiff;
42 /// Number of iterations needed
43 size_t _iters;
45 public:
46 /// Parameters of this inference algorithm
47 struct Properties {
48 /// Verbosity
49 size_t verbose;
51 /// Maximum number of iterations
52 size_t maxiter;
54 /// Tolerance
55 Real tol;
57 /// Damping constant
58 Real damping;
59 } props;
61 /// Name of this inference algorithm
62 static const char *Name;
64 public:
65 /// \name Constructors/destructors
66 //@{
67 /// Default constructor
68 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
70 /// Construct from FactorGraph \a fg and PropertySet \a opts
71 MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
72 setProperties( opts );
73 construct();
74 }
75 //@}
77 /// \name General InfAlg interface
78 //@{
79 virtual MF* clone() const { return new MF(*this); }
80 virtual std::string identify() const;
81 virtual Factor belief( const Var &n ) const;
82 virtual Factor belief( const VarSet &ns ) const;
83 virtual Factor beliefV( size_t i ) const;
84 virtual std::vector<Factor> beliefs() const;
85 virtual Real logZ() const;
86 virtual void init();
87 virtual void init( const VarSet &ns );
88 virtual Real run();
89 virtual Real maxDiff() const { return _maxdiff; }
90 virtual size_t Iterations() const { return _iters; }
91 //@}
93 /// \name Managing parameters (which are stored in MF::props)
94 //@{
95 /// Set parameters of this inference algorithm.
96 /** The parameters are set according to \a opts.
97 * The values can be stored either as std::string or as the type of the corresponding MF::props member.
98 */
99 void setProperties( const PropertySet &opts );
100 /// Returns parameters of this inference algorithm converted into a PropertySet.
101 PropertySet getProperties() const;
102 /// Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1,key2=val2,...,keyn=valn]".
103 std::string printProperties() const;
104 //@}
106 private:
107 /// Helper function for constructors
108 void construct();
110 /// Calculates an updated belief of variable \a i
111 Factor calcNewBelief( size_t i );
112 };
115 } // end of namespace dai
118 #endif