libDAI
include/dai/cbp.h
Go to the documentation of this file.
00001 /*  This file is part of libDAI - http://www.libdai.org/
00002  *
00003  *  Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
00004  *
00005  *  Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
00006  */
00007 
00008 
00011 
00012 
00013 #ifndef __defined_libdai_cbp_h
00014 #define __defined_libdai_cbp_h
00015 
00016 
00017 #include <fstream>
00018 #include <boost/shared_ptr.hpp>
00019 
00020 #include <dai/daialg.h>
00021 #include <dai/bbp.h>
00022 
00023 
00024 namespace dai {
00025 
00026 
00028 
00037 class CBP : public DAIAlgFG {
00038     private:
00040         std::vector<Factor> _beliefsV;
00042         std::vector<Factor> _beliefsF;
00044         Real _logZ;
00045 
00047         size_t _iters;
00049         Real _maxdiff;
00050 
00052         Real _sum_level;
00054         size_t _num_leaves;
00055 
00057         boost::shared_ptr<std::ofstream> _clamp_ofstream;
00058 
00059 
00060     public:
00062         CBP() : DAIAlgFG(), _beliefsV(), _beliefsF(), _logZ(0.0), _iters(0), _maxdiff(0.0), _sum_level(0.0), _num_leaves(0), _clamp_ofstream() {}
00063 
00065 
00068         CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
00069             props.set( opts );
00070             construct();
00071         }
00072 
00074 
00075         virtual CBP* clone() const { return new CBP(*this); }
00076         virtual CBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new CBP( fg, opts ); }
00077         virtual std::string name() const { return "CBP"; }
00078         virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
00079         virtual Factor belief( const VarSet & ) const { DAI_THROW(NOT_IMPLEMENTED); }
00080         virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
00081         virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
00082         virtual std::vector<Factor> beliefs() const { return concat(_beliefsV, _beliefsF); }
00083         virtual Real logZ() const { return _logZ; }
00084         virtual void init() {};
00085         virtual void init( const VarSet & ) {};
00086         virtual Real run();
00087         virtual Real maxDiff() const { return _maxdiff; }
00088         virtual size_t Iterations() const { return _iters; }
00089         virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
00090         virtual void setProperties( const PropertySet &opts ) { props.set( opts ); }
00091         virtual PropertySet getProperties() const { return props.get(); }
00092         virtual std::string printProperties() const { return props.toString(); }
00094 
00095         //----------------------------------------------------------------
00096 
00098         /* PROPERTIES(props,CBP) {
00100             typedef BP::Properties::UpdateType UpdateType;
00102             DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
00104             DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
00106             DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
00107 
00109             size_t verbose = 0;
00110 
00112             Real tol;
00114             UpdateType updates;
00116             size_t maxiter;
00117 
00119             Real rec_tol;
00121             size_t max_levels = 10;
00123             Real min_max_adj;
00125             ChooseMethodType choose;
00127             RecurseType recursion;
00129             ClampType clamp;
00131             PropertySet bbp_props;
00133             BBPCostFunction bbp_cfn;
00135             size_t rand_seed = 0;
00136 
00138             std::string clamp_outfile = "";
00139         }
00140         */
00141 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
00142     ./scripts/regenerate-properties include/dai/cbp.h src/cbp.cpp
00143 */
00144         struct Properties {
00146             typedef BP::Properties::UpdateType UpdateType;
00148             DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
00150             DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
00152             DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
00154             size_t verbose;
00156             Real tol;
00158             UpdateType updates;
00160             size_t maxiter;
00162             Real rec_tol;
00164             size_t max_levels;
00166             Real min_max_adj;
00168             ChooseMethodType choose;
00170             RecurseType recursion;
00172             ClampType clamp;
00174             PropertySet bbp_props;
00176             BBPCostFunction bbp_cfn;
00178             size_t rand_seed;
00180             std::string clamp_outfile;
00181 
00183 
00186             void set(const PropertySet &opts);
00188             PropertySet get() const;
00190             std::string toString() const;
00191         } props;
00192 /* }}} END OF GENERATED CODE */
00193 
00194     private:
00196         void printDebugInfo();
00197 
00199 
00203         void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
00204                          size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
00205 
00207 
00214         virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
00215 
00217 
00220         InfAlg* getInfAlg();
00221 
00223 
00226         void setBeliefs( const std::vector<Factor> &bs, Real logZ );
00227 
00229         void construct();
00230 };
00231 
00232 
00234 
00245 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut );
00246 
00247 
00248 } // end of namespace dai
00249 
00250 
00251 #endif