Removed DAI_ACCMUT macro and improved documentation of include/dai/util.h
[libdai.git] / include / dai / bp_dual.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2009 Frederik Eaton [frederik at ofb dot net]
8 */
9
10
11 /// \file
12 /// \brief Defines class BP_dual
13 /// \todo This replicates a large part of the functionality of BP; would it not be shorter to adapt BP instead?
14 /// \todo Improve documentation
15
16
17 #ifndef __defined_libdai_bp_dual_h
18 #define __defined_libdai_bp_dual_h
19
20
21 #include <dai/daialg.h>
22 #include <dai/factorgraph.h>
23 #include <dai/enum.h>
24
25
26 namespace dai {
27
28
29 /// Calculates both types of BP messages and their normalizers from an InfAlg.
30 /** BP_dual calculates "dual" versions of BP messages (both messages from factors
31 * to variables and messages from variables to factors), and normalizers, given an InfAlg.
32 * These are computed from the variable and factor beliefs of the InfAlg.
33 * This class is used primarily by BBP.
34 */
35 class BP_dual {
36
37 protected:
38 /// Convenience label for storing edge properties
39 template<class T>
40 struct _edges_t : public std::vector<std::vector<T> > {};
41
42 /// Groups together the data structures for storing the two types of messages and their normalizers
43 struct messages {
44 /// Unnormalized variable->factor messages
45 _edges_t<Prob> n;
46 /// Normalizers of variable->factor messages
47 _edges_t<Real> Zn;
48 /// Unnormalized Factor->variable messages
49 _edges_t<Prob> m;
50 /// Normalizers of factor->variable messages
51 _edges_t<Real> Zm;
52 };
53 /// Stores all messages
54 messages _msgs;
55
56 /// Groups together the data structures for storing the two types of beliefs and their normalizers
57 struct beliefs {
58 /// Unnormalized variable beliefs
59 std::vector<Prob> b1;
60 /// Normalizers of variable beliefs
61 std::vector<Real> Zb1;
62 /// Unnormalized factor beliefs
63 std::vector<Prob> b2;
64 /// Normalizers of factor beliefs
65 std::vector<Real> Zb2;
66 };
67 /// Stores all beliefs
68 beliefs _beliefs;
69
70 /// Pointer to the InfAlg object
71 const InfAlg *_ia;
72
73 /// Does all necessary preprocessing
74 void init();
75 /// Allocates space for _msgs
76 void regenerateMessages();
77 /// Allocates space for _beliefs
78 void regenerateBeliefs();
79
80 /// Calculate all messages from InfAlg beliefs
81 void calcMessages();
82 /// Update factor->variable message (i->I)
83 void calcNewM(size_t i, size_t _I);
84 /// Update variable->factor message (I->i)
85 void calcNewN(size_t i, size_t _I);
86
87 /// Calculate all variable and factor beliefs from messages
88 void calcBeliefs();
89 /// Calculate variable belief
90 void calcBeliefV(size_t i);
91 /// Calculate factor belief
92 void calcBeliefF(size_t I);
93
94 public:
95 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
96 /* A pointer to the the InfAlg object is stored,
97 * so the object must not be destroyed before the BP_dual is destroyed.
98 */
99 BP_dual( const InfAlg *ia ) : _ia(ia) { init(); }
100
101 /// Returns the underlying FactorGraph
102 const FactorGraph& fg() const { return _ia->fg(); }
103
104 /// Returns reference to factor -> var message (I->i)
105 Prob & msgM( size_t i, size_t _I ) { return _msgs.m[i][_I]; }
106 /// Returns constant reference to factor -> var message (I->i)
107 const Prob & msgM( size_t i, size_t _I ) const { return _msgs.m[i][_I]; }
108 /// Returns reference to var -> factor message (i->I)
109 Prob & msgN( size_t i, size_t _I ) { return _msgs.n[i][_I]; }
110 /// Returns constant reference to var -> factor message (i->I)
111 const Prob & msgN( size_t i, size_t _I ) const { return _msgs.n[i][_I]; }
112 /// Returns reference to normalizer for msgM
113 Real & zM( size_t i, size_t _I ) { return _msgs.Zm[i][_I]; }
114 /// Returns constant reference to normalizer for msgM
115 const Real & zM( size_t i, size_t _I ) const { return _msgs.Zm[i][_I]; }
116 /// Returns reference to normalizer for msgN
117 Real & zN( size_t i, size_t _I ) { return _msgs.Zn[i][_I]; }
118 /// Returns constant reference to normalizer for msgN
119 const Real & zN( size_t i, size_t _I ) const { return _msgs.Zn[i][_I]; }
120
121 /// Returns variable belief
122 Factor beliefV( size_t i ) const { return Factor( _ia->fg().var(i), _beliefs.b1[i] ); }
123 /// Returns factor belief
124 Factor beliefF( size_t I ) const { return Factor( _ia->fg().factor(I).vars(), _beliefs.b2[I] ); }
125
126 /// Returns normalizer for variable belief
127 Real beliefVZ( size_t i ) const { return _beliefs.Zb1[i]; }
128 /// Returns normalizer for factor belief
129 Real beliefFZ( size_t I ) const { return _beliefs.Zb2[I]; }
130 };
131
132
133 } // end of namespace dai
134
135
136 #endif