libDAI
bbp.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 
12 
13 
14 #ifndef ___defined_libdai_bbp_h
15 #define ___defined_libdai_bbp_h
16 
17 
18 #include <dai/dai_config.h>
19 #ifdef DAI_WITH_CBP
20 
21 
22 #include <vector>
23 #include <utility>
24 
25 #include <dai/prob.h>
26 #include <dai/daialg.h>
27 #include <dai/factorgraph.h>
28 #include <dai/enum.h>
29 #include <dai/bp_dual.h>
30 
31 
32 namespace dai {
33 
34 
36 
38 DAI_ENUM(BBPCostFunctionBase,CFN_GIBBS_B,CFN_GIBBS_B2,CFN_GIBBS_EXP,CFN_GIBBS_B_FACTOR,CFN_GIBBS_B2_FACTOR,CFN_GIBBS_EXP_FACTOR,CFN_VAR_ENT,CFN_FACTOR_ENT,CFN_BETHE_ENT);
39 
40 
42 class BBPCostFunction : public BBPCostFunctionBase {
43  public:
45  BBPCostFunction() : BBPCostFunctionBase() {}
46 
48  BBPCostFunction( const BBPCostFunctionBase &x ) : BBPCostFunctionBase(x) {}
49 
51  bool needGibbsState() const;
52 
54  Real evaluate( const InfAlg &ia, const std::vector<size_t> *stateP ) const;
55 
57  BBPCostFunction& operator=( const BBPCostFunctionBase &x ) {
58  BBPCostFunctionBase::operator=( x );
59  return *this;
60  }
61 };
62 
63 
65 
67 class BBP {
68  private:
70 
74  const FactorGraph *_fg;
76  const InfAlg *_ia;
78 
80 
81  std::vector<Prob> _adj_psi_V;
84  std::vector<Prob> _adj_psi_F;
86  std::vector<std::vector<Prob> > _adj_n;
88  std::vector<std::vector<Prob> > _adj_m;
90  std::vector<Prob> _adj_b_V;
92  std::vector<Prob> _adj_b_F;
94 
96 
97  std::vector<Prob> _init_adj_psi_V;
100  std::vector<Prob> _init_adj_psi_F;
101 
103  std::vector<std::vector<Prob> > _adj_n_unnorm;
105  std::vector<std::vector<Prob> > _adj_m_unnorm;
107  std::vector<std::vector<Prob> > _new_adj_n;
109  std::vector<std::vector<Prob> > _new_adj_m;
111  std::vector<Prob> _adj_b_V_unnorm;
113  std::vector<Prob> _adj_b_F_unnorm;
114 
116  std::vector<std::vector<Prob > > _Tmsg;
118  std::vector<std::vector<Prob > > _Umsg;
120  std::vector<std::vector<std::vector<Prob > > > _Smsg;
122  std::vector<std::vector<std::vector<Prob > > > _Rmsg;
123 
125  size_t _iters;
127 
129 
130  typedef std::vector<size_t> _ind_t;
133  std::vector<std::vector<_ind_t> > _indices;
135 
137  void RegenerateInds();
139  const _ind_t& _index(size_t i, size_t _I) const { return _indices[i][_I]; }
141 
143 
144  void RegenerateT();
147  void RegenerateU();
149  void RegenerateS();
151  void RegenerateR();
153  void RegenerateInputs();
155 
157  void RegeneratePsiAdjoints();
159 
163 
169  void Regenerate();
171 
173 
174  Prob & T(size_t i, size_t _I) { return _Tmsg[i][_I]; }
177  const Prob & T(size_t i, size_t _I) const { return _Tmsg[i][_I]; }
179  Prob & U(size_t I, size_t _i) { return _Umsg[I][_i]; }
181  const Prob & U(size_t I, size_t _i) const { return _Umsg[I][_i]; }
183  Prob & S(size_t i, size_t _I, size_t _j) { return _Smsg[i][_I][_j]; }
185  const Prob & S(size_t i, size_t _I, size_t _j) const { return _Smsg[i][_I][_j]; }
187  Prob & R(size_t I, size_t _i, size_t _J) { return _Rmsg[I][_i][_J]; }
189  const Prob & R(size_t I, size_t _i, size_t _J) const { return _Rmsg[I][_i][_J]; }
190 
192  Prob& adj_n(size_t i, size_t _I) { return _adj_n[i][_I]; }
194  const Prob& adj_n(size_t i, size_t _I) const { return _adj_n[i][_I]; }
196  Prob& adj_m(size_t i, size_t _I) { return _adj_m[i][_I]; }
198  const Prob& adj_m(size_t i, size_t _I) const { return _adj_m[i][_I]; }
200 
202 
203 
207  void calcNewN( size_t i, size_t _I );
209 
212  void calcNewM( size_t i, size_t _I );
214  void calcUnnormMsgN( size_t i, size_t _I );
216  void calcUnnormMsgM( size_t i, size_t _I );
218  void upMsgN( size_t i, size_t _I );
220  void upMsgM( size_t i, size_t _I );
222  void doParUpdate();
224 
226 
227  void incrSeqMsgM( size_t i, size_t _I, const Prob& p );
229  // DISABLED BECAUSE IT IS BUGGY:
230  // void updateSeqMsgM( size_t i, size_t _I );
232  void setSeqMsgM( size_t i, size_t _I, const Prob &p );
234  void sendSeqMsgN( size_t i, size_t _I, const Prob &f );
236  void sendSeqMsgM( size_t i, size_t _I );
238 
240 
242  Prob unnormAdjoint( const Prob &w, Real Z_w, const Prob &adj_w );
243 
245  Real getUnMsgMag();
247  void getMsgMags( Real &s, Real &new_s );
249  void getArgmaxMsgM( size_t &i, size_t &_I, Real &mag );
251  Real getMaxMsgM();
252 
254  Real getTotalMsgM();
258  Real getTotalMsgN();
259 
261  std::vector<Prob> getZeroAdjF( const FactorGraph &fg );
263  std::vector<Prob> getZeroAdjV( const FactorGraph &fg );
264 
265  public:
267 
268 
272  BBP( const InfAlg *ia, const PropertySet &opts ) : _bp_dual(ia), _fg(&(ia->fg())), _ia(ia) {
273  props.set(opts);
274  }
276 
278 
279  void init( const std::vector<Prob> &adj_b_V, const std::vector<Prob> &adj_b_F, const std::vector<Prob> &adj_psi_V, const std::vector<Prob> &adj_psi_F ) {
281  _adj_b_V = adj_b_V;
282  _adj_b_F = adj_b_F;
283  _init_adj_psi_V = adj_psi_V;
284  _init_adj_psi_F = adj_psi_F;
285  Regenerate();
286  }
287 
289  void init( const std::vector<Prob> &adj_b_V, const std::vector<Prob> &adj_b_F ) {
290  init( adj_b_V, adj_b_F, getZeroAdjV(*_fg), getZeroAdjF(*_fg) );
291  }
292 
294  void init_V( const std::vector<Prob> &adj_b_V ) {
295  init( adj_b_V, getZeroAdjF(*_fg) );
296  }
297 
299  void init_F( const std::vector<Prob> &adj_b_F ) {
300  init( getZeroAdjV(*_fg), adj_b_F );
301  }
302 
304 
308  void initCostFnAdj( const BBPCostFunction &cfn, const std::vector<size_t> *stateP );
310 
312 
313  void run();
316 
318 
319  Prob& adj_psi_V(size_t i) { return _adj_psi_V[i]; }
322  const Prob& adj_psi_V(size_t i) const { return _adj_psi_V[i]; }
324  Prob& adj_psi_F(size_t I) { return _adj_psi_F[I]; }
326  const Prob& adj_psi_F(size_t I) const { return _adj_psi_F[I]; }
328  Prob& adj_b_V(size_t i) { return _adj_b_V[i]; }
330  const Prob& adj_b_V(size_t i) const { return _adj_b_V[i]; }
332  Prob& adj_b_F(size_t I) { return _adj_b_F[I]; }
334  const Prob& adj_b_F(size_t I) const { return _adj_b_F[I]; }
336  size_t Iterations() { return _iters; }
338 
339  public:
341  /* PROPERTIES(props,BBP) {
349  DAI_ENUM(UpdateType,SEQ_FIX,SEQ_MAX,SEQ_BP_REV,SEQ_BP_FWD,PAR);
350 
352  size_t verbose = 0;
353 
355  size_t maxiter;
356 
359  Real tol;
360 
362  Real damping;
363 
365  UpdateType updates;
366 
367  // DISABLED BECAUSE IT IS BUGGY:
368  // bool clean_updates;
369  }
370  */
371 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
372  ./scripts/regenerate-properties include/dai/bbp.h src/bbp.cpp
373 */
374  struct Properties {
376 
383  DAI_ENUM(UpdateType,SEQ_FIX,SEQ_MAX,SEQ_BP_REV,SEQ_BP_FWD,PAR);
385  size_t verbose;
387  size_t maxiter;
389 
395  UpdateType updates;
396 
398 
401  void set(const PropertySet &opts);
403  PropertySet get() const;
405  std::string toString() const;
406  } props;
407 /* }}} END OF GENERATED CODE */
408 };
409 
410 
412 
420 Real numericBBPTest( const InfAlg &bp, const std::vector<size_t> *state, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real h );
421 
422 
423 } // end of namespace dai
424 
425 
426 #endif
427 
428 
429 #endif
const Prob & R(size_t I, size_t _i, size_t _J) const
Returns constant reference to R value; see eqn. (44) in [EaG09].
Definition: bbp.h:189
std::vector< Prob > _adj_b_F_unnorm
Unnormalized factor belief adjoints.
Definition: bbp.h:113
std::vector< Prob > _adj_psi_F
Factor adjoints.
Definition: bbp.h:84
std::vector< std::vector< _ind_t > > _indices
Cached indices (indexed [i][_I])
Definition: bbp.h:133
Prob & T(size_t i, size_t _I)
Returns reference to T value; see eqn. (41) in [EaG09].
Definition: bbp.h:175
std::vector< std::vector< std::vector< Prob > > > _Smsg
_Smsg[i][_I]_j in [EaG09])
Definition: bbp.h:120
DAI_ENUM(UpdateType, SEQ_FIX, SEQ_MAX, SEQ_BP_REV, SEQ_BP_FWD, PAR)
Enumeration of possible update schedules.
std::vector< Prob > _init_adj_psi_V
Initial variable factor adjoints.
Definition: bbp.h:98
void init_V(const std::vector< Prob > &adj_b_V)
Initializes variable belief adjoints adj_b_V (and sets factor belief adjoints and initial factor adjo...
Definition: bbp.h:294
Real damping
Damping constant (0 for none); damping = 1 - lambda where lambda is the damping constant used in [EaG...
Definition: bbp.h:393
const Prob & adj_b_V(size_t i) const
Returns constant reference to variable belief adjoint.
Definition: bbp.h:330
std::vector< Prob > getZeroAdjV(const FactorGraph &fg)
Returns a vector of Probs (filled with zeroes) with state spaces corresponding to the variables in th...
Definition: bbp.cpp:743
std::string toString() const
Convert to a string which can be parsed as a PropertySet.
Definition: bbp.cpp:1183
void init_F(const std::vector< Prob > &adj_b_F)
Initializes factor belief adjoints adj_b_F (and sets variable belief adjoints and initial factor adjo...
Definition: bbp.h:299
std::vector< std::vector< std::vector< Prob > > > _Rmsg
_Rmsg[I][_i]_J in [EaG09])
Definition: bbp.h:122
void calcNewN(size_t i, size_t _I)
Calculates new variable->factor message adjoint.
Definition: bbp.cpp:405
Prob & adj_psi_F(size_t I)
Returns reference to factor adjoint.
Definition: bbp.h:324
void upMsgM(size_t i, size_t _I)
Updates (un)normalized factor->variable message adjoints.
Definition: bbp.cpp:461
void init(const std::vector< Prob > &adj_b_V, const std::vector< Prob > &adj_b_F)
Initializes from given belief adjoints adj_b_V and adj_b_F (setting initial factor adjoints to zero) ...
Definition: bbp.h:289
std::vector< std::vector< Prob > > _adj_n_unnorm
Unnormalized variable->factor message adjoint (indexed [i][_I])
Definition: bbp.h:103
Implements BBP (Back-Belief-Propagation) [EaG09].
Definition: bbp.h:67
Real getTotalMsgM()
Calculates sum of L1 norms of all normalized factor->variable message adjoints.
Definition: bbp.cpp:707
void set(const PropertySet &opts)
Set members from PropertySet.
Definition: bbp.cpp:1140
bool needGibbsState() const
Returns whether this cost function depends on having a Gibbs state.
Definition: bbp.cpp:40
Represents a factor graph.
Definition: factorgraph.h:65
Prob unnormAdjoint(const Prob &w, Real Z_w, const Prob &adj_w)
Computes the adjoint of the unnormed probability vector from the normalizer and the adjoint of the no...
Definition: bbp.cpp:623
size_t maxiter
Maximum number of iterations.
Definition: bbp.h:387
BBP(const InfAlg *ia, const PropertySet &opts)
Construct BBP object from a InfAlg ia and a PropertySet opts.
Definition: bbp.h:272
std::vector< Prob > _init_adj_psi_F
Initial factor adjoints.
Definition: bbp.h:100
Real getMaxMsgM()
Returns magnitude of the largest (in L1-norm) normalized factor->variable message adjoint...
Definition: bbp.cpp:699
size_t _iters
Number of iterations done.
Definition: bbp.h:125
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
void init(const std::vector< Prob > &adj_b_V, const std::vector< Prob > &adj_b_F, const std::vector< Prob > &adj_psi_V, const std::vector< Prob > &adj_psi_F)
Initializes from given belief adjoints adj_b_V, adj_b_F and initial factor adjoints adj_psi_V...
Definition: bbp.h:280
BBPCostFunction()
Default constructor.
Definition: bbp.h:45
void initCostFnAdj(const BBPCostFunction &cfn, const std::vector< size_t > *stateP)
Initializes with adjoints calculated from cost function cfn, and state stateP.
Definition: bbp.cpp:752
Defines TProb<> and Prob classes which represent (probability) vectors (e.g., probability distributio...
void RegenerateSeqMessageAdjoints()
Initialise members for message adjoints for sequential algorithm.
Definition: bbp.cpp:344
void getArgmaxMsgM(size_t &i, size_t &_I, Real &mag)
Returns indices and magnitude of the largest normalized factor->variable message adjoint.
Definition: bbp.cpp:683
UpdateType updates
Update schedule.
Definition: bbp.h:395
size_t Iterations()
Return number of iterations done so far.
Definition: bbp.h:336
void RegenerateT()
Calculate T values; see eqn. (41) in [EaG09].
Definition: bbp.cpp:167
void doParUpdate()
Do one parallel update of all message adjoints.
Definition: bbp.cpp:467
Calculates both types of BP messages and their normalizers from an InfAlg.
Definition: bp_dual.h:39
const Prob & adj_m(size_t i, size_t _I) const
Returns constant reference to factor->variable message adjoint.
Definition: bbp.h:198
void RegenerateR()
Calculate R values; see eqn. (44) in [EaG09].
Definition: bbp.cpp:232
Real getUnMsgMag()
Calculates averaged L1 norm of unnormalized message adjoints.
Definition: bbp.cpp:636
void calcNewM(size_t i, size_t _I)
Calculates new factor->variable message adjoint.
Definition: bbp.cpp:426
std::vector< std::vector< Prob > > _new_adj_m
Updated normalized factor->variable message adjoint (indexed [i][_I])
Definition: bbp.h:109
const InfAlg * _ia
Pointer to the approximate inference algorithm (currently, only BP objects are supported) ...
Definition: bbp.h:76
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
void sendSeqMsgM(size_t i, size_t _I)
Implements routine Send-m in Figure 5 in [EaG09].
Definition: bbp.cpp:558
std::vector< std::vector< Prob > > _adj_n
Variable->factor message adjoints (indexed [i][_I])
Definition: bbp.h:86
void incrSeqMsgM(size_t i, size_t _I, const Prob &p)
Helper function for sendSeqMsgM(): increases factor->variable message adjoint by p and calculates the...
Definition: bbp.cpp:481
std::vector< std::vector< Prob > > _adj_m
Factor->variable message adjoints (indexed [i][_I])
Definition: bbp.h:88
BBPCostFunction & operator=(const BBPCostFunctionBase &x)
Assignment operator.
Definition: bbp.h:57
std::vector< std::vector< Prob > > _new_adj_n
Updated normalized variable->factor message adjoint (indexed [i][_I])
Definition: bbp.h:107
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: bbp.h:385
Real getTotalNewMsgM()
Calculates sum of L1 norms of all updated normalized factor->variable message adjoints.
Definition: bbp.cpp:716
void RegenerateParMessageAdjoints()
Initialise members for message adjoints for parallel algorithm.
Definition: bbp.cpp:294
void RegenerateS()
Calculate S values; see eqn. (43) in [EaG09].
Definition: bbp.cpp:204
void Regenerate()
Called by init, recalculates intermediate values.
Definition: bbp.cpp:389
Prob & S(size_t i, size_t _I, size_t _j)
Returns reference to S value; see eqn. (43) in [EaG09].
Definition: bbp.h:183
Prob & adj_b_F(size_t I)
Returns reference to factor belief adjoint.
Definition: bbp.h:332
Prob & adj_n(size_t i, size_t _I)
Returns reference to variable->factor message adjoint.
Definition: bbp.h:192
void RegeneratePsiAdjoints()
Initialise members for factor adjoints.
Definition: bbp.cpp:265
void setSeqMsgM(size_t i, size_t _I, const Prob &p)
Sets normalized factor->variable message adjoint and calculates the corresponding unnormalized adjoin...
Definition: bbp.cpp:517
std::vector< std::vector< Prob > > _Umsg
_Umsg[I]_i in [EaG09])
Definition: bbp.h:118
void RegenerateU()
Calculate U values; see eqn. (42) in [EaG09].
Definition: bbp.cpp:183
std::vector< Prob > _adj_psi_V
Variable factor adjoints.
Definition: bbp.h:82
DAI_ENUM(LDPCType, SMALL, GROUP, RANDOM)
Possible LDPC structures.
void upMsgN(size_t i, size_t _I)
Updates (un)normalized variable->factor message adjoints.
Definition: bbp.cpp:455
const _ind_t & _index(size_t i, size_t _I) const
Returns an index from the cache.
Definition: bbp.h:139
std::vector< Prob > _adj_b_F
Normalized factor belief adjoints.
Definition: bbp.h:92
Prob & U(size_t I, size_t _i)
Returns reference to U value; see eqn. (42) in [EaG09].
Definition: bbp.h:179
InfAlg is an abstract base class, defining the common interface of all inference algorithms in libDAI...
Definition: daialg.h:35
const Prob & U(size_t I, size_t _i) const
Returns constant reference to U value; see eqn. (42) in [EaG09].
Definition: bbp.h:181
Prob & adj_b_V(size_t i)
Returns reference to variable belief adjoint.
Definition: bbp.h:328
Parameters for BBP.
Definition: bbp.h:374
const Prob & S(size_t i, size_t _I, size_t _j) const
Returns constant reference to S value; see eqn. (43) in [EaG09].
Definition: bbp.h:185
std::vector< Prob > _adj_b_V_unnorm
Unnormalized variable belief adjoints.
Definition: bbp.h:111
std::vector< std::vector< Prob > > _adj_m_unnorm
Unnormalized factor->variable message adjoint (indexed [i][_I])
Definition: bbp.h:105
BBPCostFunction(const BBPCostFunctionBase &x)
Construct from BBPCostFunctionBase x.
Definition: bbp.h:48
void calcUnnormMsgN(size_t i, size_t _I)
Calculates unnormalized variable->factor message adjoint from the normalized one. ...
Definition: bbp.cpp:445
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
void RegenerateInputs()
Calculate _adj_b_V_unnorm and _adj_b_F_unnorm from _adj_b_V and _adj_b_F.
Definition: bbp.cpp:253
void RegenerateInds()
Prepares index cache _indices.
Definition: bbp.cpp:148
std::vector< std::vector< Prob > > _Tmsg
_Tmsg[i]_I in [EaG09])
Definition: bbp.h:116
std::vector< Prob > getZeroAdjF(const FactorGraph &fg)
Returns a vector of Probs (filled with zeroes) with state spaces corresponding to the factors in the ...
Definition: bbp.cpp:734
Namespace for libDAI.
Definition: alldai.cpp:16
Real getTotalMsgN()
Calculates sum of L1 norms of all normalized variable->factor message adjoints.
Definition: bbp.cpp:725
void run()
Perform iterative updates until change is less than given tolerance.
Definition: bbp.cpp:903
const Prob & adj_b_F(size_t I) const
Returns constant reference to factor belief adjoint.
Definition: bbp.h:334
Prob & adj_m(size_t i, size_t _I)
Returns reference to factor->variable message adjoint.
Definition: bbp.h:196
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
void calcUnnormMsgM(size_t i, size_t _I)
Calculates unnormalized factor->variable message adjoint from the normalized one. ...
Definition: bbp.cpp:450
Allows the user to specify which algorithms will be built into libDAI.
const Prob & T(size_t i, size_t _I) const
Returns constant reference to T value; see eqn. (41) in [EaG09].
Definition: bbp.h:177
Prob & adj_psi_V(size_t i)
Returns reference to variable factor adjoint.
Definition: bbp.h:320
std::vector< size_t > _ind_t
Index type.
Definition: bbp.h:131
std::vector< Prob > _adj_b_V
Normalized variable belief adjoints.
Definition: bbp.h:90
void getMsgMags(Real &s, Real &new_s)
Calculates averaged L1 norms of current and new normalized message adjoints.
Definition: bbp.cpp:649
Real evaluate(const InfAlg &ia, const std::vector< size_t > *stateP) const
Evaluates cost function in state stateP using the information in inference algorithm ia...
Definition: bbp.cpp:55
const Prob & adj_n(size_t i, size_t _I) const
Returns constant reference to variable->factor message adjoint.
Definition: bbp.h:194
Real tol
Tolerance for convergence test.
Definition: bbp.h:391
BP_dual _bp_dual
Stores a BP_dual helper object.
Definition: bbp.h:72
Prob & R(size_t I, size_t _i, size_t _J)
Returns reference to R value; see eqn. (44) in [EaG09].
Definition: bbp.h:187
void sendSeqMsgN(size_t i, size_t _I, const Prob &f)
Implements routine Send-n in Figure 5 in [EaG09].
Definition: bbp.cpp:523
Predefined cost functions that can be used with BBP.
Definition: bbp.h:42
const FactorGraph * _fg
Pointer to the factor graph.
Definition: bbp.h:74
const Prob & adj_psi_V(size_t i) const
Returns constant reference to variable factor adjoint.
Definition: bbp.h:322
const Prob & adj_psi_F(size_t I) const
Returns constant reference to factor adjoint.
Definition: bbp.h:326
Defines class BP_dual, which is used primarily by BBP.