libDAI
|
00001 /* This file is part of libDAI - http://www.libdai.org/ 00002 * 00003 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved. 00004 * 00005 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 00006 */ 00007 00008 00013 00014 00015 #ifndef __defined_libdai_bp_dual_h 00016 #define __defined_libdai_bp_dual_h 00017 00018 00019 #include <dai/daialg.h> 00020 #include <dai/factorgraph.h> 00021 #include <dai/enum.h> 00022 00023 00024 namespace dai { 00025 00026 00028 00035 class BP_dual { 00036 protected: 00038 template<class T> 00039 struct _edges_t : public std::vector<std::vector<T> > {}; 00040 00042 struct messages { 00044 _edges_t<Prob> n; 00046 _edges_t<Real> Zn; 00048 _edges_t<Prob> m; 00050 _edges_t<Real> Zm; 00051 }; 00053 messages _msgs; 00054 00056 struct beliefs { 00058 std::vector<Prob> b1; 00060 std::vector<Real> Zb1; 00062 std::vector<Prob> b2; 00064 std::vector<Real> Zb2; 00065 }; 00067 beliefs _beliefs; 00068 00070 const InfAlg *_ia; 00071 00073 void init(); 00075 void regenerateMessages(); 00077 void regenerateBeliefs(); 00078 00080 void calcMessages(); 00082 void calcNewM(size_t i, size_t _I); 00084 void calcNewN(size_t i, size_t _I); 00085 00087 void calcBeliefs(); 00089 void calcBeliefV(size_t i); 00091 void calcBeliefF(size_t I); 00092 00093 public: 00095 00098 BP_dual( const InfAlg *ia ) : _ia(ia) { init(); } 00099 00101 const FactorGraph& fg() const { return _ia->fg(); } 00102 00104 Prob & msgM( size_t i, size_t _I ) { return _msgs.m[i][_I]; } 00106 const Prob & msgM( size_t i, size_t _I ) const { return _msgs.m[i][_I]; } 00108 Prob & msgN( size_t i, size_t _I ) { return _msgs.n[i][_I]; } 00110 const Prob & msgN( size_t i, size_t _I ) const { return _msgs.n[i][_I]; } 00112 Real & zM( size_t i, size_t _I ) { return _msgs.Zm[i][_I]; } 00114 const Real & zM( size_t i, size_t _I ) const { return _msgs.Zm[i][_I]; } 00116 Real & zN( size_t i, size_t _I ) { return _msgs.Zn[i][_I]; } 00118 const Real & zN( size_t i, size_t _I ) const { return _msgs.Zn[i][_I]; } 00119 00121 Factor beliefV( size_t i ) const { return Factor( _ia->fg().var(i), _beliefs.b1[i] ); } 00123 Factor beliefF( size_t I ) const { return Factor( _ia->fg().factor(I).vars(), _beliefs.b2[I] ); } 00124 00126 Real beliefVZ( size_t i ) const { return _beliefs.Zb1[i]; } 00128 Real beliefFZ( size_t I ) const { return _beliefs.Zb2[I]; } 00129 }; 00130 00131 00132 } // end of namespace dai 00133 00134 00135 #endif