00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00015
00016
00017 #ifndef __defined_libdai_mr_h
00018 #define __defined_libdai_mr_h
00019
00020
00021 #include <vector>
00022 #include <string>
00023 #include <dai/factorgraph.h>
00024 #include <dai/daialg.h>
00025 #include <dai/enum.h>
00026 #include <dai/properties.h>
00027 #include <dai/exceptions.h>
00028 #include <dai/graph.h>
00029 #include <boost/dynamic_bitset.hpp>
00030
00031
00032 namespace dai {
00033
00034
00036
00038 class MR : public DAIAlgFG {
00039 private:
00041 bool supported;
00042
00044 GraphAL G;
00045
00047 std::vector<std::vector<Real> > tJ;
00049 std::vector<Real> theta;
00050
00052 std::vector<std::vector<Real> > M;
00054 std::vector<std::vector<std::vector<Real> > > cors;
00055
00057 typedef boost::dynamic_bitset<> sub_nb;
00058
00060 std::vector<Real> Mag;
00061
00063 Real _maxdiff;
00064
00066 size_t _iters;
00067
00068 public:
00070 struct Properties {
00072
00076 DAI_ENUM(UpdateType,FULL,LINEAR);
00077
00079
00084 DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT);
00085
00087 size_t verbose;
00088
00090 Real tol;
00091
00093 UpdateType updates;
00094
00096 InitType inits;
00097 } props;
00098
00100 static const char *Name;
00101
00102 public:
00104 MR() : DAIAlgFG(), supported(), G(), tJ(), theta(), M(), cors(), Mag(), _maxdiff(), _iters(), props() {}
00105
00107
00112 MR( const FactorGraph &fg, const PropertySet &opts );
00113
00114
00116
00117 virtual MR* clone() const { return new MR(*this); }
00118 virtual std::string identify() const;
00119 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
00120 virtual Factor belief( const VarSet & ) const;
00121 virtual Factor beliefV( size_t i ) const;
00122 virtual std::vector<Factor> beliefs() const;
00123 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
00124 virtual void init() {}
00125 virtual void init( const VarSet & ) { DAI_THROW(NOT_IMPLEMENTED); }
00126 virtual Real run();
00127 virtual Real maxDiff() const { return _maxdiff; }
00128 virtual size_t Iterations() const { return _iters; }
00129 virtual void setProperties( const PropertySet &opts );
00130 virtual PropertySet getProperties() const;
00131 virtual std::string printProperties() const;
00133
00134 private:
00136 Real calcCavityCorrelations();
00137
00139 void propagateCavityFields();
00140
00142 void calcMagnetizations();
00143
00145
00148 Real _tJ(size_t i, sub_nb A);
00149
00151 Real Omega(size_t i, size_t _j, size_t _l);
00152
00154
00157 Real T(size_t i, sub_nb A);
00158
00160 Real T(size_t i, size_t _j);
00161
00163 Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
00164
00166 Real Gamma(size_t i, size_t _l1, size_t _l2);
00167
00169
00177 Real appM(size_t i, sub_nb A);
00178
00180
00185 void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd);
00186 };
00187
00188
00189 }
00190
00191
00192 #endif