libDAI
bp_dual.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 
13 
14 
15 #ifndef __defined_libdai_bp_dual_h
16 #define __defined_libdai_bp_dual_h
17 
18 
19 #include <dai/daialg.h>
20 #include <dai/factorgraph.h>
21 #include <dai/enum.h>
22 
23 
24 namespace dai {
25 
26 
28 
35 class BP_dual {
36  protected:
38  template<class T>
39  struct _edges_t : public std::vector<std::vector<T> > {};
40 
42  struct messages {
51  };
54 
56  struct beliefs {
58  std::vector<Prob> b1;
60  std::vector<Real> Zb1;
62  std::vector<Prob> b2;
64  std::vector<Real> Zb2;
65  };
68 
70  const InfAlg *_ia;
71 
73  void init();
75  void regenerateMessages();
77  void regenerateBeliefs();
78 
80  void calcMessages();
82  void calcNewM(size_t i, size_t _I);
84  void calcNewN(size_t i, size_t _I);
85 
87  void calcBeliefs();
89  void calcBeliefV(size_t i);
91  void calcBeliefF(size_t I);
92 
93  public:
95 
98  BP_dual( const InfAlg *ia ) : _ia(ia) { init(); }
99 
101  const FactorGraph& fg() const { return _ia->fg(); }
102 
104  Prob & msgM( size_t i, size_t _I ) { return _msgs.m[i][_I]; }
106  const Prob & msgM( size_t i, size_t _I ) const { return _msgs.m[i][_I]; }
108  Prob & msgN( size_t i, size_t _I ) { return _msgs.n[i][_I]; }
110  const Prob & msgN( size_t i, size_t _I ) const { return _msgs.n[i][_I]; }
112  Real & zM( size_t i, size_t _I ) { return _msgs.Zm[i][_I]; }
114  const Real & zM( size_t i, size_t _I ) const { return _msgs.Zm[i][_I]; }
116  Real & zN( size_t i, size_t _I ) { return _msgs.Zn[i][_I]; }
118  const Real & zN( size_t i, size_t _I ) const { return _msgs.Zn[i][_I]; }
119 
121  Factor beliefV( size_t i ) const { return Factor( _ia->fg().var(i), _beliefs.b1[i] ); }
123  Factor beliefF( size_t I ) const { return Factor( _ia->fg().factor(I).vars(), _beliefs.b2[I] ); }
124 
126  Real beliefVZ( size_t i ) const { return _beliefs.Zb1[i]; }
128  Real beliefFZ( size_t I ) const { return _beliefs.Zb2[I]; }
129 };
130 
131 
132 } // end of namespace dai
133 
134 
135 #endif