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
00066 CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
00067 props.set( opts );
00068 construct();
00069 }
00070
00072 static const char *Name;
00073
00075
00076 virtual CBP* clone() const { return new CBP(*this); }
00077 virtual std::string identify() const { return std::string(Name) + props.toString(); }
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 setProperties( const PropertySet &opts ) { props.set( opts ); }
00090 virtual PropertySet getProperties() const { return props.get(); }
00091 virtual std::string printProperties() const { return props.toString(); }
00093
00094
00095
00097
00099
00101
00103
00105
00106
00108
00109
00111
00113
00115
00116
00118
00120
00122
00124
00126
00128
00130
00132
00134
00135
00137
00138
00139
00140
00141
00142
00143 struct Properties {
00145 typedef BP::Properties::UpdateType UpdateType;
00147 DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
00149 DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
00151 DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
00153 size_t verbose;
00155 Real tol;
00157 UpdateType updates;
00159 size_t maxiter;
00161 Real rec_tol;
00163 size_t max_levels;
00165 Real min_max_adj;
00167 ChooseMethodType choose;
00169 RecurseType recursion;
00171 ClampType clamp;
00173 PropertySet bbp_props;
00175 BBPCostFunction bbp_cfn;
00177 size_t rand_seed;
00179 std::string clamp_outfile;
00180
00182
00185 void set(const PropertySet &opts);
00187 PropertySet get() const;
00189 std::string toString() const;
00190 } props;
00191
00192
00193 private:
00195 void printDebugInfo();
00196
00198
00202 void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
00203 size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
00204
00206
00213 virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
00214
00216
00219 InfAlg* getInfAlg();
00220
00222
00225 void setBeliefs( const std::vector<Factor> &bs, Real logZ );
00226
00228 void construct();
00229 };
00230
00231
00233
00244 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut );
00245
00246
00247 }
00248
00249
00250 #endif