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 <string>
18 #include <dai/enum.h>
19 #include <dai/daialg.h>
20 #include <dai/factorgraph.h>
21 #include <dai/properties.h>
22 
23 
24 namespace dai {
25 
26 
28 
37 class MF : public DAIAlgFG {
38  private:
40  std::vector<Factor> _beliefs;
44  size_t _iters;
45 
46  public:
48  struct Properties {
50  DAI_ENUM(InitType,UNIFORM,RANDOM);
51 
53  DAI_ENUM(UpdateType,NAIVE,HARDSPIN);
54 
56  size_t verbose;
57 
59  size_t maxiter;
60 
63 
66 
68  InitType init;
69 
71  UpdateType updates;
72  } props;
73 
74  public:
76 
77 
78  MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
79 
81 
84  MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
85  setProperties( opts );
86  construct();
87  }
89 
91 
92  virtual MF* clone() const { return new MF(*this); }
93  virtual MF* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new MF( fg, opts ); }
94  virtual std::string name() const { return "MF"; }
95  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
96  virtual Factor belief( const VarSet &vs ) const;
97  virtual Factor beliefV( size_t i ) const;
98  virtual std::vector<Factor> beliefs() const;
99  virtual Real logZ() const;
100  virtual void init();
101  virtual void init( const VarSet &ns );
102  virtual Real run();
103  virtual Real maxDiff() const { return _maxdiff; }
104  virtual size_t Iterations() const { return _iters; }
105  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
106  virtual void setProperties( const PropertySet &opts );
107  virtual PropertySet getProperties() const;
108  virtual std::string printProperties() const;
110 
111  private:
113  void construct();
114 
116  Factor calcNewBelief( size_t i );
117 };
118 
119 
120 } // end of namespace dai
121 
122 
123 #endif