1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE 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
15 #ifndef __defined_libdai_bp_dual_h
16 #define __defined_libdai_bp_dual_h
19 #include <dai/dai_config.h>
23 #include <dai/daialg.h>
24 #include <dai/factorgraph.h>
31 /// Calculates both types of BP messages and their normalizers from an InfAlg.
32 /** BP_dual calculates "dual" versions of BP messages (both messages from factors
33 * to variables and messages from variables to factors), and normalizers, given an InfAlg.
34 * These are computed from the variable and factor beliefs of the InfAlg.
35 * This class is used primarily by BBP.
37 * \author Frederik Eaton
41 /// Convenience label for storing edge properties
43 struct _edges_t
: public std::vector
<std::vector
<T
> > {};
45 /// Groups together the data structures for storing the two types of messages and their normalizers
47 /// Unnormalized variable->factor messages
49 /// Normalizers of variable->factor messages
51 /// Unnormalized Factor->variable messages
53 /// Normalizers of factor->variable messages
56 /// Stores all messages
59 /// Groups together the data structures for storing the two types of beliefs and their normalizers
61 /// Unnormalized variable beliefs
63 /// Normalizers of variable beliefs
64 std::vector
<Real
> Zb1
;
65 /// Unnormalized factor beliefs
67 /// Normalizers of factor beliefs
68 std::vector
<Real
> Zb2
;
70 /// Stores all beliefs
73 /// Pointer to the InfAlg object
76 /// Does all necessary preprocessing
78 /// Allocates space for \a _msgs
79 void regenerateMessages();
80 /// Allocates space for \a _beliefs
81 void regenerateBeliefs();
83 /// Calculate all messages from InfAlg beliefs
85 /// Update factor->variable message (\a i -> \a I)
86 void calcNewM(size_t i
, size_t _I
);
87 /// Update variable->factor message (\a I -> \a i)
88 void calcNewN(size_t i
, size_t _I
);
90 /// Calculate all variable and factor beliefs from messages
92 /// Calculate belief of variable \a i
93 void calcBeliefV(size_t i
);
94 /// Calculate belief of factor \a I
95 void calcBeliefF(size_t I
);
98 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
99 /** \warning A pointer to the the InfAlg object is stored,
100 * so the object must not be destroyed before the BP_dual is destroyed.
102 BP_dual( const InfAlg
*ia
) : _ia(ia
) { init(); }
104 /// Returns the underlying FactorGraph
105 const FactorGraph
& fg() const { return _ia
->fg(); }
107 /// Returns reference to factor->variable message (\a I -> \a i)
108 Prob
& msgM( size_t i
, size_t _I
) { return _msgs
.m
[i
][_I
]; }
109 /// Returns constant reference to factor->variable message (\a I -> \a i)
110 const Prob
& msgM( size_t i
, size_t _I
) const { return _msgs
.m
[i
][_I
]; }
111 /// Returns reference to variable -> factor message (\a i -> \a I)
112 Prob
& msgN( size_t i
, size_t _I
) { return _msgs
.n
[i
][_I
]; }
113 /// Returns constant reference to variable -> factor message (\a i -> \a I)
114 const Prob
& msgN( size_t i
, size_t _I
) const { return _msgs
.n
[i
][_I
]; }
115 /// Returns reference to normalizer for factor->variable message (\a I -> \a i)
116 Real
& zM( size_t i
, size_t _I
) { return _msgs
.Zm
[i
][_I
]; }
117 /// Returns constant reference to normalizer for factor->variable message (\a I -> \a i)
118 const Real
& zM( size_t i
, size_t _I
) const { return _msgs
.Zm
[i
][_I
]; }
119 /// Returns reference to normalizer for variable -> factor message (\a i -> \a I)
120 Real
& zN( size_t i
, size_t _I
) { return _msgs
.Zn
[i
][_I
]; }
121 /// Returns constant reference to normalizer for variable -> factor message (\a i -> \a I)
122 const Real
& zN( size_t i
, size_t _I
) const { return _msgs
.Zn
[i
][_I
]; }
124 /// Returns belief of variable \a i
125 Factor
beliefV( size_t i
) const { return Factor( _ia
->fg().var(i
), _beliefs
.b1
[i
] ); }
126 /// Returns belief of factor \a I
127 Factor
beliefF( size_t I
) const { return Factor( _ia
->fg().factor(I
).vars(), _beliefs
.b2
[I
] ); }
129 /// Returns normalizer for belief of variable \a i
130 Real
beliefVZ( size_t i
) const { return _beliefs
.Zb1
[i
]; }
131 /// Returns normalizer for belief of factor \a I
132 Real
beliefFZ( size_t I
) const { return _beliefs
.Zb2
[I
]; }
136 } // end of namespace dai