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 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
00100
00102
00104
00106
00107
00109
00110
00112
00114
00116
00117
00119
00121
00123
00125
00127
00129
00131
00133
00135
00136
00138
00139
00140
00141
00142
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
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 }
00249
00250
00251 #endif