8357771c6c0cbda85424b8f1892833686f8d02e1
[libdai.git] / include / dai / bp_dual.h
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
9 /// \file
10 /// \brief Defines class BP_dual, which is used primarily by BBP.
11 /// \idea BP_dual replicates a large part of the functionality of BP; would it not be more efficient to adapt BP instead?
12 /// \author Frederik Eaton
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
27 /// Calculates both types of BP messages and their normalizers from an InfAlg.
28 /** BP_dual calculates "dual" versions of BP messages (both messages from factors
29 * to variables and messages from variables to factors), and normalizers, given an InfAlg.
30 * These are computed from the variable and factor beliefs of the InfAlg.
31 * This class is used primarily by BBP.
32 *
33 * \author Frederik Eaton
34 */
35 class BP_dual {
36 protected:
37 /// Convenience label for storing edge properties
38 template<class T>
39 struct _edges_t : public std::vector<std::vector<T> > {};
40
41 /// Groups together the data structures for storing the two types of messages and their normalizers
42 struct messages {
43 /// Unnormalized variable->factor messages
44 _edges_t<Prob> n;
45 /// Normalizers of variable->factor messages
46 _edges_t<Real> Zn;
47 /// Unnormalized Factor->variable messages
48 _edges_t<Prob> m;
49 /// Normalizers of factor->variable messages
50 _edges_t<Real> Zm;
51 };
52 /// Stores all messages
53 messages _msgs;
54
55 /// Groups together the data structures for storing the two types of beliefs and their normalizers
56 struct beliefs {
57 /// Unnormalized variable beliefs
58 std::vector<Prob> b1;
59 /// Normalizers of variable beliefs
60 std::vector<Real> Zb1;
61 /// Unnormalized factor beliefs
62 std::vector<Prob> b2;
63 /// Normalizers of factor beliefs
64 std::vector<Real> Zb2;
65 };
66 /// Stores all beliefs
67 beliefs _beliefs;
68
69 /// Pointer to the InfAlg object
70 const InfAlg *_ia;
71
72 /// Does all necessary preprocessing
73 void init();
74 /// Allocates space for \a _msgs
75 void regenerateMessages();
76 /// Allocates space for \a _beliefs
77 void regenerateBeliefs();
78
79 /// Calculate all messages from InfAlg beliefs
80 void calcMessages();
81 /// Update factor->variable message (\a i -> \a I)
82 void calcNewM(size_t i, size_t _I);
83 /// Update variable->factor message (\a I -> \a i)
84 void calcNewN(size_t i, size_t _I);
85
86 /// Calculate all variable and factor beliefs from messages
87 void calcBeliefs();
88 /// Calculate belief of variable \a i
89 void calcBeliefV(size_t i);
90 /// Calculate belief of factor \a I
91 void calcBeliefF(size_t I);
92
93 public:
94 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
95 /** \warning A pointer to the the InfAlg object is stored,
96 * so the object must not be destroyed before the BP_dual is destroyed.
97 */
98 BP_dual( const InfAlg *ia ) : _ia(ia) { init(); }
99
100 /// Returns the underlying FactorGraph
101 const FactorGraph& fg() const { return _ia->fg(); }
102
103 /// Returns reference to factor->variable message (\a I -> \a i)
104 Prob & msgM( size_t i, size_t _I ) { return _msgs.m[i][_I]; }
105 /// Returns constant reference to factor->variable message (\a I -> \a i)
106 const Prob & msgM( size_t i, size_t _I ) const { return _msgs.m[i][_I]; }
107 /// Returns reference to variable -> factor message (\a i -> \a I)
108 Prob & msgN( size_t i, size_t _I ) { return _msgs.n[i][_I]; }
109 /// Returns constant reference to variable -> factor message (\a i -> \a I)
110 const Prob & msgN( size_t i, size_t _I ) const { return _msgs.n[i][_I]; }
111 /// Returns reference to normalizer for factor->variable message (\a I -> \a i)
112 Real & zM( size_t i, size_t _I ) { return _msgs.Zm[i][_I]; }
113 /// Returns constant reference to normalizer for factor->variable message (\a I -> \a i)
114 const Real & zM( size_t i, size_t _I ) const { return _msgs.Zm[i][_I]; }
115 /// Returns reference to normalizer for variable -> factor message (\a i -> \a I)
116 Real & zN( size_t i, size_t _I ) { return _msgs.Zn[i][_I]; }
117 /// Returns constant reference to normalizer for variable -> factor message (\a i -> \a I)
118 const Real & zN( size_t i, size_t _I ) const { return _msgs.Zn[i][_I]; }
119
120 /// Returns belief of variable \a i
121 Factor beliefV( size_t i ) const { return Factor( _ia->fg().var(i), _beliefs.b1[i] ); }
122 /// Returns belief of factor \a I
123 Factor beliefF( size_t I ) const { return Factor( _ia->fg().factor(I).vars(), _beliefs.b2[I] ); }
124
125 /// Returns normalizer for belief of variable \a i
126 Real beliefVZ( size_t i ) const { return _beliefs.Zb1[i]; }
127 /// Returns normalizer for belief of factor \a I
128 Real beliefFZ( size_t I ) const { return _beliefs.Zb2[I]; }
129 };
130
131
132 } // end of namespace dai
133
134
135 #endif