8357771c6c0cbda85424b8f1892833686f8d02e1
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/daialg.h>
20 #include <dai/factorgraph.h>
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.
33 * \author Frederik Eaton
37 /// Convenience label for storing edge properties
39 struct _edges_t
: public std::vector
<std::vector
<T
> > {};
41 /// Groups together the data structures for storing the two types of messages and their normalizers
43 /// Unnormalized variable->factor messages
45 /// Normalizers of variable->factor messages
47 /// Unnormalized Factor->variable messages
49 /// Normalizers of factor->variable messages
52 /// Stores all messages
55 /// Groups together the data structures for storing the two types of beliefs and their normalizers
57 /// Unnormalized variable beliefs
59 /// Normalizers of variable beliefs
60 std::vector
<Real
> Zb1
;
61 /// Unnormalized factor beliefs
63 /// Normalizers of factor beliefs
64 std::vector
<Real
> Zb2
;
66 /// Stores all beliefs
69 /// Pointer to the InfAlg object
72 /// Does all necessary preprocessing
74 /// Allocates space for \a _msgs
75 void regenerateMessages();
76 /// Allocates space for \a _beliefs
77 void regenerateBeliefs();
79 /// Calculate all messages from InfAlg beliefs
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
);
86 /// Calculate all variable and factor beliefs from messages
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
);
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.
98 BP_dual( const InfAlg
*ia
) : _ia(ia
) { init(); }
100 /// Returns the underlying FactorGraph
101 const FactorGraph
& fg() const { return _ia
->fg(); }
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
]; }
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
] ); }
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
]; }
132 } // end of namespace dai