libDAI
cbp.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 
11 
12 
13 #ifndef __defined_libdai_cbp_h
14 #define __defined_libdai_cbp_h
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_CBP
19 
20 
21 #include <fstream>
22 #include <boost/shared_ptr.hpp>
23 
24 #include <dai/daialg.h>
25 #include <dai/bbp.h>
26 
27 
28 namespace dai {
29 
30 
32 
41 class CBP : public DAIAlgFG {
42  private:
44  std::vector<Factor> _beliefsV;
46  std::vector<Factor> _beliefsF;
49 
51  size_t _iters;
54 
58  size_t _num_leaves;
59 
61  boost::shared_ptr<std::ofstream> _clamp_ofstream;
62 
63 
64  public:
66  CBP() : DAIAlgFG(), _beliefsV(), _beliefsF(), _logZ(0.0), _iters(0), _maxdiff(0.0), _sum_level(0.0), _num_leaves(0), _clamp_ofstream() {}
67 
69 
72  CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
73  props.set( opts );
74  construct();
75  }
76 
78 
79  virtual CBP* clone() const { return new CBP(*this); }
80  virtual CBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new CBP( fg, opts ); }
81  virtual std::string name() const { return "CBP"; }
82  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
83  virtual Factor belief( const VarSet & ) const { DAI_THROW(NOT_IMPLEMENTED); }
84  virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
85  virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
86  virtual std::vector<Factor> beliefs() const { return concat(_beliefsV, _beliefsF); }
87  virtual Real logZ() const { return _logZ; }
88  virtual void init() {};
89  virtual void init( const VarSet & ) {};
90  virtual Real run();
91  virtual Real maxDiff() const { return _maxdiff; }
92  virtual size_t Iterations() const { return _iters; }
93  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
94  virtual void setProperties( const PropertySet &opts ) { props.set( opts ); }
95  virtual PropertySet getProperties() const { return props.get(); }
96  virtual std::string printProperties() const { return props.toString(); }
98 
99  //----------------------------------------------------------------
100 
102  /* PROPERTIES(props,CBP) {
104  typedef BP::Properties::UpdateType UpdateType;
106  DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
108  DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
110  DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
111 
113  size_t verbose = 0;
114 
116  Real tol;
118  UpdateType updates;
120  size_t maxiter;
121 
123  Real rec_tol;
125  size_t max_levels = 10;
127  Real min_max_adj;
129  ChooseMethodType choose;
131  RecurseType recursion;
133  ClampType clamp;
135  PropertySet bbp_props;
137  BBPCostFunction bbp_cfn;
139  size_t rand_seed = 0;
140 
142  std::string clamp_outfile = "";
143  }
144  */
145 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
146  ./scripts/regenerate-properties include/dai/cbp.h src/cbp.cpp
147 */
148  struct Properties {
150  typedef BP::Properties::UpdateType UpdateType;
152  DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
154  DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
156  DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
158  size_t verbose;
162  UpdateType updates;
164  size_t maxiter;
168  size_t max_levels;
172  ChooseMethodType choose;
174  RecurseType recursion;
176  ClampType clamp;
182  size_t rand_seed;
184  std::string clamp_outfile;
185 
187 
190  void set(const PropertySet &opts);
192  PropertySet get() const;
194  std::string toString() const;
195  } props;
196 /* }}} END OF GENERATED CODE */
197 
198  private:
200  void printDebugInfo();
201 
203 
207  void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
208  size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
209 
211 
218  virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
219 
221 
224  InfAlg* getInfAlg();
225 
227 
230  void setBeliefs( const std::vector<Factor> &bs, Real logZ );
231 
233  void construct();
234 };
235 
236 
238 
249 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut );
250 
251 
252 } // end of namespace dai
253 
254 
255 #endif
256 
257 
258 #endif
Defines class BBP, which implements Back-Belief-Propagation.
DAI_ENUM(RecurseType, REC_FIXED, REC_LOGZ, REC_BDIFF)
Enumeration of possible methods for deciding when to stop recursing.
virtual bool chooseNextClampVar(InfAlg *bp, std::vector< size_t > &clamped_vars_list, size_t &i, std::vector< size_t > &xis, Real *maxVarOut)
Choose the next variable to clamp.
Definition: cbp.cpp:281
CBP(const FactorGraph &fg, const PropertySet &opts)
Construct CBP object from FactorGraph fg and PropertySet opts.
Definition: cbp.h:72
boost::shared_ptr< std::ofstream > _clamp_ofstream
Output stream where information about the clampings is written.
Definition: cbp.h:61
size_t _num_leaves
Number of leaves of recursion tree.
Definition: cbp.h:58
BP::Properties::UpdateType UpdateType
Enumeration of possible update schedules.
Definition: cbp.h:150
Parameters for CBP.
Definition: cbp.h:148
virtual CBP * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: cbp.h:80
Real _sum_level
Number of clampings at each leaf node.
Definition: cbp.h:56
Class for CBP (Conditioned Belief Propagation) [EaG09].
Definition: cbp.h:41
virtual Factor belief(const VarSet &) const
Returns the (approximate) marginal probability distribution of a set of variables.
Definition: cbp.h:83
Represents a factor graph.
Definition: factorgraph.h:65
Real _maxdiff
Maximum difference encountered so far.
Definition: cbp.h:53
std::string clamp_outfile
If non-empty, write clamping choices to this file.
Definition: cbp.h:184
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: cbp.h:92
FactorGraph & fg()
Returns reference to underlying FactorGraph.
Definition: daialg.h:221
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
Real min_max_adj
If choose==CHOOSE_BBP and maximum adjoint is less than this value, don't recurse. ...
Definition: cbp.h:170
virtual void setProperties(const PropertySet &opts)
Set parameters of this inference algorithm.
Definition: cbp.h:94
Real _logZ
Logarithm of partition sum.
Definition: cbp.h:48
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: cbp.h:88
UpdateType updates
Update style for BP.
Definition: cbp.h:162
virtual Factor beliefV(size_t i) const
Returns the (approximate) marginal probability distribution of the variable with index i...
Definition: cbp.h:84
BBPCostFunction bbp_cfn
Cost function to use for BBP.
Definition: cbp.h:180
size_t rand_seed
Random seed.
Definition: cbp.h:182
std::vector< Factor > _beliefsF
Factor beliefs.
Definition: cbp.h:46
Real tol
Tolerance for BP convergence test.
Definition: cbp.h:160
Represents a set of variables.
Definition: varset.h:94
virtual Real run()
Runs the approximate inference algorithm.
Definition: cbp.cpp:131
void runRecurse(InfAlg *bp, Real orig_logZ, std::vector< size_t > clamped_vars_list, size_t &num_leaves, size_t &choose_count, Real &sum_level, Real &lz_out, std::vector< Factor > &beliefs_out)
Called by run(), and by itself. Implements the main algorithm.
Definition: cbp.cpp:168
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: cbp.h:158
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: cbp.h:87
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: cbp.h:91
size_t maxiter
Maximum number of iterations for BP.
Definition: cbp.h:164
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: cbp.h:95
RecurseType recursion
Method for deciding when to stop recursing.
Definition: cbp.h:174
std::string toString() const
Convert to a string which can be parsed as a PropertySet.
Definition: cbp.cpp:616
ClampType clamp
Whether to clamp variables or factors.
Definition: cbp.h:176
virtual Factor belief(const Var &v) const
Returns the (approximate) marginal probability distribution of a variable.
Definition: cbp.h:82
PropertySet bbp_props
Properties to pass to BBP.
Definition: cbp.h:178
Real rec_tol
Tolerance used for controlling recursion depth (recurse is REC_LOGZ or REC_BDIFF) ...
Definition: cbp.h:166
std::vector< Factor > _beliefsV
Variable beliefs.
Definition: cbp.h:44
std::vector< T > concat(const std::vector< T > &u, const std::vector< T > &v)
Concatenates two vectors.
Definition: util.h:231
void printDebugInfo()
Prints beliefs, variables and partition sum, in case of a debugging build.
Definition: cbp.cpp:447
InfAlg is an abstract base class, defining the common interface of all inference algorithms in libDAI...
Definition: daialg.h:35
void setBeliefs(const std::vector< Factor > &bs, Real logZ)
Sets variable beliefs, factor beliefs and log partition sum to the specified values.
Definition: cbp.cpp:76
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
virtual std::vector< Factor > beliefs() const
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
Definition: cbp.h:86
size_t max_levels
Maximum number of levels of recursion (recurse is REC_FIXED)
Definition: cbp.h:168
virtual void setMaxIter(size_t maxiter)
Sets maximum number of iterations (one iteration passes over the complete factorgraph).
Definition: cbp.h:93
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
size_t _iters
Numer of iterations needed.
Definition: cbp.h:51
ChooseMethodType choose
Heuristic for choosing clamping variable.
Definition: cbp.h:172
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
virtual Factor beliefF(size_t I) const
Returns the (approximate) marginal probability distribution of the variables on which factor I depend...
Definition: cbp.h:85
virtual std::string name() const
Returns the name of the algorithm.
Definition: cbp.h:81
Allows the user to specify which algorithms will be built into libDAI.
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: cbp.h:96
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
InfAlg * getInfAlg()
Return the InfAlg to use at each step of the recursion.
Definition: cbp.cpp:153
CBP()
Default constructor.
Definition: cbp.h:66
virtual CBP * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: cbp.h:79
PropertySet get() const
Get members into PropertySet.
Definition: cbp.cpp:598
Predefined cost functions that can be used with BBP.
Definition: bbp.h:42
void set(const PropertySet &opts)
Set members from PropertySet.
Definition: cbp.cpp:522
void construct()
Constructor helper function.
Definition: cbp.cpp:90
virtual void init(const VarSet &)
Initializes all data structures corresponding to some set of variables.
Definition: cbp.h:89