libDAI
mr.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_mr_h
14 #define __defined_libdai_mr_h
15 
16 
17 #include <vector>
18 #include <string>
19 #include <dai/factorgraph.h>
20 #include <dai/daialg.h>
21 #include <dai/enum.h>
22 #include <dai/properties.h>
23 #include <dai/exceptions.h>
24 #include <dai/graph.h>
25 #include <boost/dynamic_bitset.hpp>
26 
27 
28 namespace dai {
29 
30 
32 
34 class MR : public DAIAlgFG {
35  private:
37  bool supported;
38 
41 
43  std::vector<std::vector<Real> > tJ;
45  std::vector<Real> theta;
46 
48  std::vector<std::vector<Real> > M;
50  std::vector<std::vector<std::vector<Real> > > cors;
51 
53  typedef boost::dynamic_bitset<> sub_nb;
54 
56  std::vector<Real> Mag;
57 
60 
62  size_t _iters;
63 
64  public:
66  struct Properties {
68 
72  DAI_ENUM(UpdateType,FULL,LINEAR);
73 
75 
80  DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT);
81 
83  size_t verbose;
84 
87 
89  UpdateType updates;
90 
92  InitType inits;
93  } props;
94 
95  public:
97  MR() : DAIAlgFG(), supported(), G(), tJ(), theta(), M(), cors(), Mag(), _maxdiff(), _iters(), props() {}
98 
100 
105  MR( const FactorGraph &fg, const PropertySet &opts );
106 
107 
109 
110  virtual MR* clone() const { return new MR(*this); }
111  virtual MR* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new MR( fg, opts ); }
112  virtual std::string name() const { return "MR"; }
113  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
114  virtual Factor belief( const VarSet &/*vs*/ ) const;
115  virtual Factor beliefV( size_t i ) const;
116  virtual std::vector<Factor> beliefs() const;
117  virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
118  virtual void init() {}
119  virtual void init( const VarSet &/*ns*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
120  virtual Real run();
121  virtual Real maxDiff() const { return _maxdiff; }
122  virtual size_t Iterations() const { return _iters; }
123  virtual void setProperties( const PropertySet &opts );
124  virtual PropertySet getProperties() const;
125  virtual std::string printProperties() const;
127 
128  private:
131 
133  void propagateCavityFields();
134 
136  void calcMagnetizations();
137 
139 
142  Real _tJ(size_t i, sub_nb A);
143 
145  Real Omega(size_t i, size_t _j, size_t _l);
146 
148 
151  Real T(size_t i, sub_nb A);
152 
154  Real T(size_t i, size_t _j);
155 
157  Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
158 
160  Real Gamma(size_t i, size_t _l1, size_t _l2);
161 
163 
171  Real appM(size_t i, sub_nb A);
172 
174 
179  void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd);
180 };
181 
182 
183 } // end of namespace dai
184 
185 
186 #endif