libDAI
bp_dual.h
Go to the documentation of this file.
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 
13 
14 
15 #ifndef __defined_libdai_bp_dual_h
16 #define __defined_libdai_bp_dual_h
17 
18 
19 #include <dai/dai_config.h>
20 #ifdef DAI_WITH_CBP
21 
22 
23 #include <dai/daialg.h>
24 #include <dai/factorgraph.h>
25 #include <dai/enum.h>
26 
27 
28 namespace dai {
29 
30 
32 
39 class BP_dual {
40  protected:
42  template<class T>
43  struct _edges_t : public std::vector<std::vector<T> > {};
44 
46  struct messages {
55  };
58 
60  struct beliefs {
62  std::vector<Prob> b1;
64  std::vector<Real> Zb1;
66  std::vector<Prob> b2;
68  std::vector<Real> Zb2;
69  };
72 
74  const InfAlg *_ia;
75 
77  void init();
79  void regenerateMessages();
81  void regenerateBeliefs();
82 
84  void calcMessages();
86  void calcNewM(size_t i, size_t _I);
88  void calcNewN(size_t i, size_t _I);
89 
91  void calcBeliefs();
93  void calcBeliefV(size_t i);
95  void calcBeliefF(size_t I);
96 
97  public:
99 
102  BP_dual( const InfAlg *ia ) : _ia(ia) { init(); }
103 
105  const FactorGraph& fg() const { return _ia->fg(); }
106 
108  Prob & msgM( size_t i, size_t _I ) { return _msgs.m[i][_I]; }
110  const Prob & msgM( size_t i, size_t _I ) const { return _msgs.m[i][_I]; }
112  Prob & msgN( size_t i, size_t _I ) { return _msgs.n[i][_I]; }
114  const Prob & msgN( size_t i, size_t _I ) const { return _msgs.n[i][_I]; }
116  Real & zM( size_t i, size_t _I ) { return _msgs.Zm[i][_I]; }
118  const Real & zM( size_t i, size_t _I ) const { return _msgs.Zm[i][_I]; }
120  Real & zN( size_t i, size_t _I ) { return _msgs.Zn[i][_I]; }
122  const Real & zN( size_t i, size_t _I ) const { return _msgs.Zn[i][_I]; }
123 
125  Factor beliefV( size_t i ) const { return Factor( _ia->fg().var(i), _beliefs.b1[i] ); }
127  Factor beliefF( size_t I ) const { return Factor( _ia->fg().factor(I).vars(), _beliefs.b2[I] ); }
128 
130  Real beliefVZ( size_t i ) const { return _beliefs.Zb1[i]; }
132  Real beliefFZ( size_t I ) const { return _beliefs.Zb2[I]; }
133 };
134 
135 
136 } // end of namespace dai
137 
138 
139 #endif
140 
141 
142 #endif
_edges_t< Prob > n
Unnormalized variable->factor messages.
Definition: bp_dual.h:48
_edges_t< Prob > m
Unnormalized Factor->variable messages.
Definition: bp_dual.h:52
TFactor< Real > Factor
Represents a factor with values of type dai::Real.
Definition: factor.h:640
std::vector< Prob > b2
Unnormalized factor beliefs.
Definition: bp_dual.h:66
_edges_t< Real > Zn
Normalizers of variable->factor messages.
Definition: bp_dual.h:50
const VarSet & vars() const
Returns constant reference to variable set (i.e., the variables on which the factor depends) ...
Definition: factor.h:139
const Prob & msgN(size_t i, size_t _I) const
Returns constant reference to variable -> factor message (i -> I)
Definition: bp_dual.h:114
Factor beliefF(size_t I) const
Returns belief of factor I.
Definition: bp_dual.h:127
Groups together the data structures for storing the two types of messages and their normalizers...
Definition: bp_dual.h:46
const Real & zM(size_t i, size_t _I) const
Returns constant reference to normalizer for factor->variable message (I -> i)
Definition: bp_dual.h:118
Factor beliefV(size_t i) const
Returns belief of variable i.
Definition: bp_dual.h:125
Represents a factor graph.
Definition: factorgraph.h:65
Prob & msgM(size_t i, size_t _I)
Returns reference to factor->variable message (I -> i)
Definition: bp_dual.h:108
std::vector< Real > Zb2
Normalizers of factor beliefs.
Definition: bp_dual.h:68
_edges_t< Real > Zm
Normalizers of factor->variable messages.
Definition: bp_dual.h:54
std::vector< Prob > b1
Unnormalized variable beliefs.
Definition: bp_dual.h:62
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
void calcMessages()
Calculate all messages from InfAlg beliefs.
Definition: bp_dual.cpp:68
const Var & var(size_t i) const
Returns constant reference the i 'th variable.
Definition: factorgraph.h:103
Calculates both types of BP messages and their normalizers from an InfAlg.
Definition: bp_dual.h:39
Prob & msgN(size_t i, size_t _I)
Returns reference to variable -> factor message (i -> I)
Definition: bp_dual.h:112
void calcNewM(size_t i, size_t _I)
Update factor->variable message (i -> I)
Definition: bp_dual.cpp:86
const Factor & factor(size_t I) const
Returns constant reference to I 'th factor.
Definition: factorgraph.h:112
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
Real beliefFZ(size_t I) const
Returns normalizer for belief of factor I.
Definition: bp_dual.h:132
BP_dual(const InfAlg *ia)
Construct BP_dual object from (converged) InfAlg object's beliefs and factors.
Definition: bp_dual.h:102
const Prob & msgM(size_t i, size_t _I) const
Returns constant reference to factor->variable message (I -> i)
Definition: bp_dual.h:110
messages _msgs
Stores all messages.
Definition: bp_dual.h:57
void regenerateMessages()
Allocates space for _msgs.
Definition: bp_dual.cpp:36
Groups together the data structures for storing the two types of beliefs and their normalizers...
Definition: bp_dual.h:60
void regenerateBeliefs()
Allocates space for _beliefs.
Definition: bp_dual.cpp:53
Real beliefVZ(size_t i) const
Returns normalizer for belief of variable i.
Definition: bp_dual.h:130
void calcNewN(size_t i, size_t _I)
Update variable->factor message (I -> i)
Definition: bp_dual.cpp:109
const InfAlg * _ia
Pointer to the InfAlg object.
Definition: bp_dual.h:74
InfAlg is an abstract base class, defining the common interface of all inference algorithms in libDAI...
Definition: daialg.h:35
const FactorGraph & fg() const
Returns the underlying FactorGraph.
Definition: bp_dual.h:105
void calcBeliefV(size_t i)
Calculate belief of variable i.
Definition: bp_dual.cpp:129
const Real & zN(size_t i, size_t _I) const
Returns constant reference to normalizer for variable -> factor message (i -> I)
Definition: bp_dual.h:122
void init()
Does all necessary preprocessing.
Definition: bp_dual.cpp:28
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
std::vector< Real > Zb1
Normalizers of variable beliefs.
Definition: bp_dual.h:64
void calcBeliefF(size_t I)
Calculate belief of factor I.
Definition: bp_dual.cpp:138
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
Allows the user to specify which algorithms will be built into libDAI.
beliefs _beliefs
Stores all beliefs.
Definition: bp_dual.h:71
Real & zN(size_t i, size_t _I)
Returns reference to normalizer for variable -> factor message (i -> I)
Definition: bp_dual.h:120
Convenience label for storing edge properties.
Definition: bp_dual.h:43
Real & zM(size_t i, size_t _I)
Returns reference to normalizer for factor->variable message (I -> i)
Definition: bp_dual.h:116
virtual FactorGraph & fg()=0
Returns reference to underlying FactorGraph.
void calcBeliefs()
Calculate all variable and factor beliefs from messages.
Definition: bp_dual.cpp:121