a2cc41b4a0acbb78846520d1ea04f212e838c5f5
[libdai.git] / include / dai / bp_dual.h
1 /* Copyright (C) 2009 Frederik Eaton [frederik at ofb dot net]
2
3 This file is part of libDAI.
4
5 libDAI is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 libDAI is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libDAI; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20
21 /// \file
22 /// \brief Defines class BP_dual
23 /// \todo Improve documentation
24 /// \todo Clean up
25
26
27 #ifndef __defined_libdai_bp_dual_h
28 #define __defined_libdai_bp_dual_h
29
30
31 #include <dai/daialg.h>
32 #include <dai/factorgraph.h>
33 #include <dai/enum.h>
34
35
36 namespace dai {
37
38
39 /** Class to estimate "dual" versions of BP messages, and normalizers, given an InfAlg.
40 * These are computed from the variable and factor beliefs of the InfAlg.
41 * This class is used primarily by BBP.
42 */
43 class BP_dual {
44
45 protected:
46 template<class T>
47 struct _edges_t : public std::vector<std::vector<T> > {};
48
49 struct messages {
50 // messages:
51 _edges_t<Prob> n;
52 _edges_t<Real> Zn;
53 _edges_t<Prob> m;
54 _edges_t<Real> Zm;
55 };
56 messages _msgs;
57
58 struct beliefs {
59 // beliefs:
60 // indexed by node
61 std::vector<Prob> b1;
62 std::vector<Real> Zb1;
63 // indexed by factor
64 std::vector<Prob> b2;
65 std::vector<Real> Zb2;
66 };
67 beliefs _beliefs;
68
69 const InfAlg *_ia;
70
71 void Init();
72
73 void RegenerateMessages();
74 void RegenerateBeliefs();
75
76 void CalcMessages();
77 void CalcBeliefV(size_t i);
78 void CalcBeliefF(size_t I);
79 void CalcBeliefs();
80
81 void calcNewM(size_t i, size_t _I);
82 void calcNewN(size_t i, size_t _I);
83 public:
84
85 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
86 /* A pointer to the the InfAlg object is
87 * stored, so the object must not be destroyed before the BP_dual
88 */
89 BP_dual(const InfAlg *ia) : _ia(ia) {
90 Init();
91 }
92
93 const FactorGraph& fg() const { return _ia->fg(); }
94
95 /// msgM: factor -> var messages
96 DAI_ACCMUT(Prob & msgM(size_t i, size_t _I), { return _msgs.m[i][_I]; });
97 /// msgN: var -> factor messages
98 DAI_ACCMUT(Prob & msgN(size_t i, size_t _I), { return _msgs.n[i][_I]; });
99 /// Normalizer for msgM
100 DAI_ACCMUT(Real & zM(size_t i, size_t _I), { return _msgs.Zm[i][_I]; });
101 /// Normalizer for msgN
102 DAI_ACCMUT(Real & zN(size_t i, size_t _I), { return _msgs.Zn[i][_I]; });
103
104 /// Variable beliefs
105 Factor beliefV(size_t i) const { return Factor(_ia->fg().var(i), _beliefs.b1[i]); }
106 /// Factor beliefs
107 Factor beliefF(size_t I) const { return Factor(_ia->fg().factor(I).vars(), _beliefs.b2[I]); }
108
109 /// Normalizer for variable beliefs
110 Real beliefVZ(size_t i) const { return _beliefs.Zb1[i]; }
111 /// Normalizer for factor beliefs
112 Real beliefFZ(size_t I) const { return _beliefs.Zb2[I]; }
113
114 };
115
116
117 } // end of namespace dai
118
119
120 #endif