00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015
00016
00017 #ifndef __defined_libdai_bp_dual_h
00018 #define __defined_libdai_bp_dual_h
00019
00020
00021 #include <dai/daialg.h>
00022 #include <dai/factorgraph.h>
00023 #include <dai/enum.h>
00024
00025
00026 namespace dai {
00027
00028
00030
00037 class BP_dual {
00038 protected:
00040 template<class T>
00041 struct _edges_t : public std::vector<std::vector<T> > {};
00042
00044 struct messages {
00046 _edges_t<Prob> n;
00048 _edges_t<Real> Zn;
00050 _edges_t<Prob> m;
00052 _edges_t<Real> Zm;
00053 };
00055 messages _msgs;
00056
00058 struct beliefs {
00060 std::vector<Prob> b1;
00062 std::vector<Real> Zb1;
00064 std::vector<Prob> b2;
00066 std::vector<Real> Zb2;
00067 };
00069 beliefs _beliefs;
00070
00072 const InfAlg *_ia;
00073
00075 void init();
00077 void regenerateMessages();
00079 void regenerateBeliefs();
00080
00082 void calcMessages();
00084 void calcNewM(size_t i, size_t _I);
00086 void calcNewN(size_t i, size_t _I);
00087
00089 void calcBeliefs();
00091 void calcBeliefV(size_t i);
00093 void calcBeliefF(size_t I);
00094
00095 public:
00097
00100 BP_dual( const InfAlg *ia ) : _ia(ia) { init(); }
00101
00103 const FactorGraph& fg() const { return _ia->fg(); }
00104
00106 Prob & msgM( size_t i, size_t _I ) { return _msgs.m[i][_I]; }
00108 const Prob & msgM( size_t i, size_t _I ) const { return _msgs.m[i][_I]; }
00110 Prob & msgN( size_t i, size_t _I ) { return _msgs.n[i][_I]; }
00112 const Prob & msgN( size_t i, size_t _I ) const { return _msgs.n[i][_I]; }
00114 Real & zM( size_t i, size_t _I ) { return _msgs.Zm[i][_I]; }
00116 const Real & zM( size_t i, size_t _I ) const { return _msgs.Zm[i][_I]; }
00118 Real & zN( size_t i, size_t _I ) { return _msgs.Zn[i][_I]; }
00120 const Real & zN( size_t i, size_t _I ) const { return _msgs.Zn[i][_I]; }
00121
00123 Factor beliefV( size_t i ) const { return Factor( _ia->fg().var(i), _beliefs.b1[i] ); }
00125 Factor beliefF( size_t I ) const { return Factor( _ia->fg().factor(I).vars(), _beliefs.b2[I] ); }
00126
00128 Real beliefVZ( size_t i ) const { return _beliefs.Zb1[i]; }
00130 Real beliefFZ( size_t I ) const { return _beliefs.Zb2[I]; }
00131 };
00132
00133
00134 }
00135
00136
00137 #endif