1 #ifndef ____defined_libdai_cbp_h__
2 #define ____defined_libdai_cbp_h__
4 #include <dai/daialg.h>
11 /* Class for "clamped belief propagation".
13 'chooseNextClamp' can be overridden (e.g. in BPC); this version
14 randomly chooses variables to clamp.
20 vector
<T
> concat(const vector
<T
>& u
, const vector
<T
>& v
) {
22 w
.reserve(u
.size()+v
.size());
23 for(size_t i
=0; i
<u
.size(); i
++)
25 for(size_t i
=0; i
<v
.size(); i
++)
30 class CBP
: public DAIAlgFG
{
31 vector
<Factor
> _beliefs1
;
32 vector
<Factor
> _beliefs2
;
40 size_t maxClampLevel() { return props
.max_levels
; }
41 double minMaxAdj() { return props
.min_max_adj
; }
42 double recTol() { return props
.rec_tol
; }
45 bbp_cfn_t
BBP_cost_function() {
49 void printDebugInfo();
51 void runRecurse(BP_dual
&bp_dual
,
53 vector
<size_t> clamped_vars_list
,
54 set
<size_t> clamped_vars
,
57 Real
&lz_out
, vector
<Factor
>& beliefs_out
);
67 typedef BP::Properties::UpdateType UpdateType
;
68 DAI_ENUM(RecurseType
,REC_FIXED
,REC_LOGZ
,REC_BDIFF
);
69 DAI_ENUM(ChooseMethodType
,CHOOSE_RANDOM
,CHOOSE_BBP
,CHOOSE_BP_ALL
);
70 DAI_ENUM(ClampType
,CLAMP_VAR
,CLAMP_FACTOR
);
80 ChooseMethodType choose
;
81 RecurseType recursion
;
87 Properties::ChooseMethodType
ChooseMethod() { return props
.choose
; }
88 Properties::RecurseType
Recursion() { return props
.recursion
; }
89 Properties::ClampType
Clamping() { return props
.clamp
; }
91 //----------------------------------------------------------------
93 // construct CBP object from FactorGraph
94 CBP(const FactorGraph
&fg
, const PropertySet
&opts
) : DAIAlgFG(fg
) {
99 static const char *Name
;
100 /// List of property names
101 static const char *PropertyList
[];
103 /// @name General InfAlg interface
105 virtual CBP
* clone() const { return new CBP(*this); }
106 // virtual CBP* create() const { return new CBP(); }
107 virtual CBP
* create() const { throw "Unimplemented"; }
108 virtual std::string
identify() const { return string(Name
) + printProperties(); }
109 virtual Factor
belief (const Var
&n
) const { return _beliefs1
[findVar(n
)]; }
110 virtual Factor
belief (const VarSet
&) const { assert(0); }
111 virtual vector
<Factor
> beliefs() const { return concat(_beliefs1
, _beliefs2
); }
112 virtual Real
logZ() const { return _logZ
; }
113 virtual void init() {};
114 virtual void init( const VarSet
& ) {};
115 virtual double run();
116 virtual double maxDiff() const { return _maxdiff
; }
117 virtual size_t Iterations() const { return _iters
; }
121 //----------------------------------------------------------------
123 virtual bool chooseNextClampVar(BP_dual
& bp
,
124 vector
<size_t> &clamped_vars_list
,
125 set
<size_t> &clamped_vars
,
126 size_t &i
, vector
<size_t> &xis
, Real
*maxVarOut
);
128 //----------------------------------------------------------------
130 void setBeliefsFrom(const BP
& b
);
131 void setBeliefs(const vector
<Factor
>& bs
, double logZ
) {
133 _beliefs1
.clear(); _beliefs1
.reserve(nrVars());
134 _beliefs2
.clear(); _beliefs2
.reserve(nrFactors());
135 for(i
=0; i
<nrVars(); i
++) _beliefs1
.push_back(bs
[i
]);
136 for(; i
<nrVars()+nrFactors(); i
++) _beliefs2
.push_back(bs
[i
]);
139 Factor
belief1 (size_t i
) const { return _beliefs1
[i
]; }
140 Factor
belief2 (size_t I
) const { return _beliefs2
[I
]; }
142 //----------------------------------------------------------------
146 void setProperties( const PropertySet
&opts
);
147 PropertySet
getProperties() const;
148 std::string
printProperties() const;
151 pair
<size_t, size_t> bbpFindClampVar(BP_dual
&in_bp_dual
, bool clampVar
, size_t numClamped
, bbp_cfn_t cfn
, const PropertySet
&props
, Real
*maxVarOut
);