c5fca87cbfe55c0572f1fa8035c69459e3d1bacc
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
14 /// \todo Improve documentation
17 #ifndef __defined_libdai_mf_h
18 #define __defined_libdai_mf_h
21 #include <string>
22 #include <dai/daialg.h>
23 #include <dai/factorgraph.h>
24 #include <dai/properties.h>
27 namespace dai {
30 /// Approximate inference algorithm "Mean Field"
31 class MF : public DAIAlgFG {
32 private:
33 std::vector<Factor> _beliefs;
34 /// Maximum difference encountered so far
35 Real _maxdiff;
36 /// Number of iterations needed
37 size_t _iters;
39 public:
40 /// Parameters of this inference algorithm
41 struct Properties {
42 /// Verbosity
43 size_t verbose;
45 /// Maximum number of iterations
46 size_t maxiter;
48 /// Tolerance
49 Real tol;
51 /// Damping constant
52 Real damping;
53 } props;
55 /// Name of this inference algorithm
56 static const char *Name;
58 public:
59 /// Default constructor
60 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
62 /// Construct from FactorGraph fg and PropertySet opts
63 MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
64 setProperties( opts );
65 construct();
66 }
69 /// @name General InfAlg interface
70 //@{
71 virtual MF* clone() const { return new MF(*this); }
72 virtual std::string identify() const;
73 virtual Factor belief( const Var &n ) const;
74 virtual Factor belief( const VarSet &ns ) const;
75 virtual std::vector<Factor> beliefs() const;
76 virtual Real logZ() const;
77 virtual void init();
78 virtual void init( const VarSet &ns );
79 virtual Real run();
80 virtual Real maxDiff() const { return _maxdiff; }
81 virtual size_t Iterations() const { return _iters; }
82 //@}
85 /// @name Additional interface specific for MF
86 //@{
87 Factor beliefV( size_t i ) const;
88 //@}
90 private:
91 void construct();
92 void setProperties( const PropertySet &opts );
93 PropertySet getProperties() const;
94 std::string printProperties() const;
95 };
98 } // end of namespace dai
101 #endif