Various cleanups
[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 /// Convenience label for storing edge properties
47 template<class T>
48 struct _edges_t : public std::vector<std::vector<T> > {};
49
50 /// Groups together the data structures for storing the two types of messages and their normalizers
51 struct messages {
52 _edges_t<Prob> n;
53 _edges_t<Real> Zn;
54 _edges_t<Prob> m;
55 _edges_t<Real> Zm;
56 };
57 messages _msgs;
58
59 /// Groups together the data structures for storing the two types of beliefs and their normalizers
60 struct beliefs {
61 // indexed by node
62 std::vector<Prob> b1;
63 std::vector<Real> Zb1;
64 // indexed by factor
65 std::vector<Prob> b2;
66 std::vector<Real> Zb2;
67 };
68 beliefs _beliefs;
69
70 const InfAlg *_ia;
71
72 void init();
73
74 void regenerateMessages();
75 void regenerateBeliefs();
76
77 void calcMessages();
78 void calcBeliefV(size_t i);
79 void calcBeliefF(size_t I);
80 void calcBeliefs();
81
82 void calcNewM(size_t i, size_t _I);
83 void calcNewN(size_t i, size_t _I);
84
85 public:
86 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
87 /* A pointer to the the InfAlg object is stored,
88 * so the object must not be destroyed before the BP_dual is destroyed.
89 */
90 BP_dual( const InfAlg *ia ) : _ia(ia) { init(); }
91
92 const FactorGraph& fg() const { return _ia->fg(); }
93
94 /// factor -> var message
95 DAI_ACCMUT(Prob & msgM(size_t i, size_t _I), { return _msgs.m[i][_I]; });
96 /// var -> factor message
97 DAI_ACCMUT(Prob & msgN(size_t i, size_t _I), { return _msgs.n[i][_I]; });
98 /// Normalizer for msgM
99 DAI_ACCMUT(Real & zM(size_t i, size_t _I), { return _msgs.Zm[i][_I]; });
100 /// Normalizer for msgN
101 DAI_ACCMUT(Real & zN(size_t i, size_t _I), { return _msgs.Zn[i][_I]; });
102
103 /// Variable belief
104 Factor beliefV(size_t i) const { return Factor(_ia->fg().var(i), _beliefs.b1[i]); }
105 /// Factor belief
106 Factor beliefF(size_t I) const { return Factor(_ia->fg().factor(I).vars(), _beliefs.b2[I]); }
107
108 /// Normalizer for variable belief
109 Real beliefVZ(size_t i) const { return _beliefs.Zb1[i]; }
110 /// Normalizer for factor belief
111 Real beliefFZ(size_t I) const { return _beliefs.Zb2[I]; }
112
113 };
114
115
116 } // end of namespace dai
117
118
119 #endif