libDAI
mf.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_mf_h
14 #define __defined_libdai_mf_h
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_MF
19 
20 
21 #include <string>
22 #include <dai/enum.h>
23 #include <dai/daialg.h>
24 #include <dai/factorgraph.h>
25 #include <dai/properties.h>
26 
27 
28 namespace dai {
29 
30 
32 
41 class MF : public DAIAlgFG {
42  private:
44  std::vector<Factor> _beliefs;
48  size_t _iters;
49 
50  public:
52  struct Properties {
54  DAI_ENUM(InitType,UNIFORM,RANDOM);
55 
57  DAI_ENUM(UpdateType,NAIVE,HARDSPIN);
58 
60  size_t verbose;
61 
63  size_t maxiter;
64 
67 
70 
72  InitType init;
73 
75  UpdateType updates;
76  } props;
77 
78  public:
80 
81  MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
83 
85 
88  MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
89  setProperties( opts );
90  construct();
91  }
93 
95 
96  virtual MF* clone() const { return new MF(*this); }
97  virtual MF* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new MF( fg, opts ); }
98  virtual std::string name() const { return "MF"; }
99  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
100  virtual Factor belief( const VarSet &vs ) const;
101  virtual Factor beliefV( size_t i ) 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 Real run();
107  virtual Real maxDiff() const { return _maxdiff; }
108  virtual size_t Iterations() const { return _iters; }
109  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
110  virtual void setProperties( const PropertySet &opts );
111  virtual PropertySet getProperties() const;
112  virtual std::string printProperties() const;
114 
115  private:
117  void construct();
118 
120  Factor calcNewBelief( size_t i );
121 };
122 
123 
124 } // end of namespace dai
125 
126 
127 #endif
128 
129 
130 #endif
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: mf.cpp:198
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: mf.h:107
void construct()
Helper function for constructors.
Definition: mf.cpp:77
Real damping
Damping constant (0.0 means no damping, 1.0 is maximum damping)
Definition: mf.h:69
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: mf.cpp:52
Approximate inference algorithm "Mean Field".
Definition: mf.h:41
Represents a factor graph.
Definition: factorgraph.h:65
FactorGraph & fg()
Returns reference to underlying FactorGraph.
Definition: daialg.h:221
InitType init
How to initialize the messages/beliefs.
Definition: mf.h:72
virtual void setProperties(const PropertySet &opts)
Set parameters of this inference algorithm.
Definition: mf.cpp:27
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
std::vector< Factor > _beliefs
Current approximations of single variable marginals.
Definition: mf.h:44
virtual Real run()
Runs the approximate inference algorithm.
Definition: mf.cpp:117
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: mf.cpp:64
virtual MF * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: mf.h:96
virtual std::vector< Factor > beliefs() const
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
Definition: mf.cpp:190
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: mf.h:108
Represents a set of variables.
Definition: varset.h:94
Factor calcNewBelief(size_t i)
Calculates an updated belief of variable i.
Definition: mf.cpp:96
DAI_ENUM(InitType, UNIFORM, RANDOM)
Enumeration of possible message initializations.
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: mf.h:60
virtual Factor beliefV(size_t i) const
Returns the (approximate) marginal probability distribution of the variable with index i...
Definition: mf.cpp:173
virtual MF * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: mf.h:97
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: mf.cpp:86
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
MF()
Default constructor.
Definition: mf.h:82
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
Real tol
Tolerance for convergence test.
Definition: mf.h:66
Allows the user to specify which algorithms will be built into libDAI.
size_t maxiter
Maximum number of iterations.
Definition: mf.h:63
Parameters for MF.
Definition: mf.h:52
size_t _iters
Number of iterations needed.
Definition: mf.h:48
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
UpdateType updates
How to update the messages/beliefs.
Definition: mf.h:75
virtual Factor belief(const Var &v) const
Returns the (approximate) marginal probability distribution of a variable.
Definition: mf.h:99
MF(const FactorGraph &fg, const PropertySet &opts)
Construct from FactorGraph fg and PropertySet opts.
Definition: mf.h:88
virtual void setMaxIter(size_t maxiter)
Sets maximum number of iterations (one iteration passes over the complete factorgraph).
Definition: mf.h:109
virtual std::string name() const
Returns the name of the algorithm.
Definition: mf.h:98
Real _maxdiff
Maximum difference encountered so far.
Definition: mf.h:46