1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2009 Frederik Eaton [frederik at ofb dot net]
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?
16 #ifndef __defined_libdai_bp_dual_h
17 #define __defined_libdai_bp_dual_h
20 #include <dai/daialg.h>
21 #include <dai/factorgraph.h>
28 /// Calculates both types of BP messages and their normalizers from an InfAlg.
29 /** BP_dual calculates "dual" versions of BP messages (both messages from factors
30 * to variables and messages from variables to factors), and normalizers, given an InfAlg.
31 * These are computed from the variable and factor beliefs of the InfAlg.
32 * This class is used primarily by BBP.
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 _msgs
75 void regenerateMessages();
76 /// Allocates space for _beliefs
77 void regenerateBeliefs();
79 /// Calculate all messages from InfAlg beliefs
81 /// Update factor->variable message (i->I)
82 void calcNewM(size_t i
, size_t _I
);
83 /// Update variable->factor message (I->i)
84 void calcNewN(size_t i
, size_t _I
);
86 /// Calculate all variable and factor beliefs from messages
88 /// Calculate variable belief
89 void calcBeliefV(size_t i
);
90 /// Calculate factor belief
91 void calcBeliefF(size_t I
);
94 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
95 /* 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 factor -> var message (I->i)
104 DAI_ACCMUT(Prob
& msgM( size_t i
, size_t _I
), { return _msgs
.m
[i
][_I
]; });
105 /// Returns var -> factor message (i->I)
106 DAI_ACCMUT(Prob
& msgN( size_t i
, size_t _I
), { return _msgs
.n
[i
][_I
]; });
107 /// Returns normalizer for msgM
108 DAI_ACCMUT(Real
& zM( size_t i
, size_t _I
), { return _msgs
.Zm
[i
][_I
]; });
109 /// Returns normalizer for msgN
110 DAI_ACCMUT(Real
& zN( size_t i
, size_t _I
), { return _msgs
.Zn
[i
][_I
]; });
112 /// Returns variable belief
113 Factor
beliefV( size_t i
) const { return Factor( _ia
->fg().var(i
), _beliefs
.b1
[i
] ); }
114 /// Returns factor belief
115 Factor
beliefF( size_t I
) const { return Factor( _ia
->fg().factor(I
).vars(), _beliefs
.b2
[I
] ); }
117 /// Returns normalizer for variable belief
118 Real
beliefVZ( size_t i
) const { return _beliefs
.Zb1
[i
]; }
119 /// Returns normalizer for factor belief
120 Real
beliefFZ( size_t I
) const { return _beliefs
.Zb2
[I
]; }
124 } // end of namespace dai