1 /* Copyright (C) 2009 Frederik Eaton [frederik at ofb dot net]
3 This file is part of libDAI.
5 libDAI is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 libDAI is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with libDAI; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 /// \brief Defines class BP_dual
23 /// \todo Improve documentation
27 #ifndef __defined_libdai_bp_dual_h
28 #define __defined_libdai_bp_dual_h
31 #include <dai/daialg.h>
32 #include <dai/factorgraph.h>
39 /** Class to estimate "dual" versions of BP messages, and normalizers, given an InfAlg.
40 * These are computed from the variable and factor beliefs of the InfAlg.
41 * This class is used primarily by BBP.
47 struct _edges_t
: public std::vector
<std::vector
<T
> > {};
62 std::vector
<Real
> Zb1
;
65 std::vector
<Real
> Zb2
;
73 void RegenerateMessages();
74 void RegenerateBeliefs();
77 void CalcBeliefV(size_t i
);
78 void CalcBeliefF(size_t I
);
81 void calcNewM(size_t i
, size_t _I
);
82 void calcNewN(size_t i
, size_t _I
);
85 /// Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
86 /* A pointer to the the InfAlg object is
87 * stored, so the object must not be destroyed before the BP_dual
89 BP_dual(const InfAlg
*ia
) : _ia(ia
) {
93 const FactorGraph
& fg() const { return _ia
->fg(); }
95 /// msgM: factor -> var messages
96 DAI_ACCMUT(Prob
& msgM(size_t i
, size_t _I
), { return _msgs
.m
[i
][_I
]; });
97 /// msgN: var -> factor messages
98 DAI_ACCMUT(Prob
& msgN(size_t i
, size_t _I
), { return _msgs
.n
[i
][_I
]; });
99 /// Normalizer for msgM
100 DAI_ACCMUT(Real
& zM(size_t i
, size_t _I
), { return _msgs
.Zm
[i
][_I
]; });
101 /// Normalizer for msgN
102 DAI_ACCMUT(Real
& zN(size_t i
, size_t _I
), { return _msgs
.Zn
[i
][_I
]; });
105 Factor
beliefV(size_t i
) const { return Factor(_ia
->fg().var(i
), _beliefs
.b1
[i
]); }
107 Factor
beliefF(size_t I
) const { return Factor(_ia
->fg().factor(I
).vars(), _beliefs
.b2
[I
]); }
109 /// Normalizer for variable beliefs
110 Real
beliefVZ(size_t i
) const { return _beliefs
.Zb1
[i
]; }
111 /// Normalizer for factor beliefs
112 Real
beliefFZ(size_t I
) const { return _beliefs
.Zb2
[I
]; }
117 } // end of namespace dai