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 <fstream>
18 #include <boost/shared_ptr.hpp>
19 
20 #include <dai/daialg.h>
21 #include <dai/bbp.h>
22 
23 
24 namespace dai {
25 
26 
28 
37 class CBP : public DAIAlgFG {
38  private:
40  std::vector<Factor> _beliefsV;
42  std::vector<Factor> _beliefsF;
45 
47  size_t _iters;
50 
54  size_t _num_leaves;
55 
57  boost::shared_ptr<std::ofstream> _clamp_ofstream;
58 
59 
60  public:
63 
65 
68  CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
69  props.set( opts );
70  construct();
71  }
72 
74 
75  virtual CBP* clone() const { return new CBP(*this); }
76  virtual CBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new CBP( fg, opts ); }
77  virtual std::string name() const { return "CBP"; }
78  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
79  virtual Factor belief( const VarSet & ) const { DAI_THROW(NOT_IMPLEMENTED); }
80  virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
81  virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
82  virtual std::vector<Factor> beliefs() const { return concat(_beliefsV, _beliefsF); }
83  virtual Real logZ() const { return _logZ; }
84  virtual void init() {};
85  virtual void init( const VarSet & ) {};
86  virtual Real run();
87  virtual Real maxDiff() const { return _maxdiff; }
88  virtual size_t Iterations() const { return _iters; }
89  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
90  virtual void setProperties( const PropertySet &opts ) { props.set( opts ); }
91  virtual PropertySet getProperties() const { return props.get(); }
92  virtual std::string printProperties() const { return props.toString(); }
94 
95  //----------------------------------------------------------------
96 
98  /* PROPERTIES(props,CBP) {
100  typedef BP::Properties::UpdateType UpdateType;
102  DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
104  DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
106  DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
107 
109  size_t verbose = 0;
110 
112  Real tol;
114  UpdateType updates;
116  size_t maxiter;
117 
119  Real rec_tol;
121  size_t max_levels = 10;
123  Real min_max_adj;
125  ChooseMethodType choose;
127  RecurseType recursion;
129  ClampType clamp;
131  PropertySet bbp_props;
133  BBPCostFunction bbp_cfn;
135  size_t rand_seed = 0;
136 
138  std::string clamp_outfile = "";
139  }
140  */
141 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
142  ./scripts/regenerate-properties include/dai/cbp.h src/cbp.cpp
143 */
144  struct Properties {
146  typedef BP::Properties::UpdateType UpdateType;
148  DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
150  DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
152  DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
154  size_t verbose;
160  size_t maxiter;
164  size_t max_levels;
168  ChooseMethodType choose;
170  RecurseType recursion;
172  ClampType clamp;
178  size_t rand_seed;
180  std::string clamp_outfile;
181 
183 
186  void set(const PropertySet &opts);
188  PropertySet get() const;
190  std::string toString() const;
191  } props;
192 /* }}} END OF GENERATED CODE */
193 
194  private:
196  void printDebugInfo();
197 
199 
203  void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
204  size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
205 
207 
214  virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
215 
217 
220  InfAlg* getInfAlg();
221 
223 
226  void setBeliefs( const std::vector<Factor> &bs, Real logZ );
227 
229  void construct();
230 };
231 
232 
234 
245 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut );
246 
247 
248 } // end of namespace dai
249 
250 
251 #endif