00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00013
00014
00015 #ifndef __defined_libdai_cbp_h
00016 #define __defined_libdai_cbp_h
00017
00018
00019 #include <fstream>
00020 #include <boost/shared_ptr.hpp>
00021
00022 #include <dai/daialg.h>
00023 #include <dai/bbp.h>
00024
00025
00026 namespace dai {
00027
00028
00030
00039 class CBP : public DAIAlgFG {
00040 private:
00042 std::vector<Factor> _beliefsV;
00044 std::vector<Factor> _beliefsF;
00046 Real _logZ;
00047
00049 size_t _iters;
00051 Real _maxdiff;
00052
00054 Real _sum_level;
00056 size_t _num_leaves;
00057
00059 boost::shared_ptr<std::ofstream> _clamp_ofstream;
00060
00061
00062 public:
00064
00067 CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
00068 props.set( opts );
00069 construct();
00070 }
00071
00073 static const char *Name;
00074
00076
00077 virtual CBP* clone() const { return new CBP(*this); }
00078 virtual std::string identify() const { return std::string(Name) + props.toString(); }
00079 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
00080 virtual Factor belief( const VarSet & ) const { DAI_THROW(NOT_IMPLEMENTED); }
00081 virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
00082 virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
00083 virtual std::vector<Factor> beliefs() const { return concat(_beliefsV, _beliefsF); }
00084 virtual Real logZ() const { return _logZ; }
00085 virtual void init() {};
00086 virtual void init( const VarSet & ) {};
00087 virtual Real run();
00088 virtual Real maxDiff() const { return _maxdiff; }
00089 virtual size_t Iterations() const { return _iters; }
00090 virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
00091 virtual void setProperties( const PropertySet &opts ) { props.set( opts ); }
00092 virtual PropertySet getProperties() const { return props.get(); }
00093 virtual std::string printProperties() const { return props.toString(); }
00095
00096
00097
00099
00101
00103
00105
00107
00108
00110
00111
00113
00115
00117
00118
00120
00122
00124
00126
00128
00130
00132
00134
00136
00137
00139
00140
00141
00142
00143
00144
00145 struct Properties {
00147 typedef BP::Properties::UpdateType UpdateType;
00149 DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
00151 DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
00153 DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
00155 size_t verbose;
00157 Real tol;
00159 UpdateType updates;
00161 size_t maxiter;
00163 Real rec_tol;
00165 size_t max_levels;
00167 Real min_max_adj;
00169 ChooseMethodType choose;
00171 RecurseType recursion;
00173 ClampType clamp;
00175 PropertySet bbp_props;
00177 BBPCostFunction bbp_cfn;
00179 size_t rand_seed;
00181 std::string clamp_outfile;
00182
00184
00187 void set(const PropertySet &opts);
00189 PropertySet get() const;
00191 std::string toString() const;
00192 } props;
00193
00194
00195 private:
00197 void printDebugInfo();
00198
00200
00204 void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
00205 size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
00206
00208
00215 virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
00216
00218
00221 InfAlg* getInfAlg();
00222
00224
00227 void setBeliefs( const std::vector<Factor> &bs, Real logZ );
00228
00230 void construct();
00231 };
00232
00233
00235
00246 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut );
00247
00248
00249 }
00250
00251
00252 #endif