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 <boost/dynamic_bitset.hpp>
00029
00030
00031 namespace dai {
00032
00033
00035
00038 class MR : public DAIAlgFG {
00039 private:
00041 bool supported;
00043 std::vector<size_t> con;
00045 std::vector<std::vector<size_t> > nb;
00047 std::vector<std::vector<Real> > tJ;
00049 std::vector<Real> theta;
00051 std::vector<std::vector<Real> > M;
00053 std::vector<std::vector<size_t> > kindex;
00055 std::vector<std::vector<std::vector<Real> > > cors;
00057 static const size_t kmax = 31;
00059 typedef boost::dynamic_bitset<> sub_nb;
00061 size_t N;
00063 std::vector<Real> Mag;
00065 Real _maxdiff;
00067 size_t _iters;
00068
00069 public:
00071 struct Properties {
00073
00077 DAI_ENUM(UpdateType,FULL,LINEAR);
00078
00080
00085 DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT);
00086
00088 size_t verbose;
00089
00091 Real tol;
00092
00094 UpdateType updates;
00095
00097 InitType inits;
00098 } props;
00099
00101 static const char *Name;
00102
00103 public:
00105 MR() : DAIAlgFG(), supported(), con(), nb(), tJ(), theta(), M(), kindex(), cors(), N(), Mag(), _maxdiff(), _iters(), props() {}
00106
00108
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 { DAI_THROW(NOT_IMPLEMENTED); return Factor(); }
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 sign(Real a) { return (a >= 0) ? 1.0 : -1.0; }
00137
00139 void init(size_t Nin, Real *_w, Real *_th);
00140
00142 void makekindex();
00143
00145 Real init_cor();
00146
00148 Real init_cor_resp();
00149
00151 void solvemcav();
00152
00154 void solveM();
00155
00157
00160 Real _tJ(size_t i, sub_nb A);
00161
00163 Real Omega(size_t i, size_t _j, size_t _l);
00164
00166
00169 Real T(size_t i, sub_nb A);
00170
00172 Real T(size_t i, size_t _j);
00173
00175 Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
00176
00178 Real Gamma(size_t i, size_t _l1, size_t _l2);
00179
00181
00189 Real appM(size_t i, sub_nb A);
00190
00192
00197 void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd);
00198 };
00199
00200
00201 }
00202
00203
00204 #endif