1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2009 Frederik Eaton [frederik at ofb dot net]
8 */
11 /// \file
12 /// \brief Defines class CBP, which implements Clamped Belief Propagation
15 #ifndef __defined_libdai_cbp_h
16 #define __defined_libdai_cbp_h
19 #include <fstream>
20 #include <boost/shared_ptr.hpp>
22 #include <dai/daialg.h>
23 #include <dai/bbp.h>
26 namespace dai {
29 /// Class for CBP (Clamped Belief Propagation) [\ref EaG09]
30 /** This approximate inference algorithm uses configurable heuristics to choose a variable
31 * \f$x_i \f$ and a state \f$x_i^* \f$. Inference is done with \f$x_i \f$ "clamped" to \f$x_i^* \f$
32 * (i.e., conditional on \f$x_i = x_i^* \f$), and also with the negation of this condition.
33 * Clamping is done recursively up to a fixed number of levels (other stopping criteria are
34 * also implemented, see the CBP::Properties::RecurseType property). The resulting approximate
35 * marginals are combined using estimates of the partition sum.
36 *
37 * \author Frederik Eaton
38 */
39 class CBP : public DAIAlgFG {
40 private:
41 /// Variable beliefs
42 std::vector<Factor> _beliefsV;
43 /// Factor beliefs
44 std::vector<Factor> _beliefsF;
45 /// Logarithm of partition sum
46 Real _logZ;
48 /// Numer of iterations needed
49 size_t _iters;
50 /// Maximum difference encountered so far
51 Real _maxdiff;
53 /// Number of clampings at each leaf node
54 Real _sum_level;
55 /// Number of leaves of recursion tree
56 size_t _num_leaves;
58 /// Output stream where information about the clampings is written
59 boost::shared_ptr<std::ofstream> _clamp_ofstream;
62 public:
63 /// Construct CBP object from FactorGraph \a fg and PropertySet \a opts
64 CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
65 props.set( opts );
66 construct();
67 }
69 /// Name of this inference algorithm
70 static const char *Name;
72 /// \name General InfAlg interface
73 //@{
74 virtual CBP* clone() const { return new CBP(*this); }
75 virtual std::string identify() const { return std::string(Name) + props.toString(); }
76 virtual Factor belief (const Var &n) const { return _beliefsV[findVar(n)]; }
77 virtual Factor belief (const VarSet &) const { DAI_THROW(NOT_IMPLEMENTED); }
78 virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
79 virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
80 virtual std::vector<Factor> beliefs() const { return concat(_beliefsV, _beliefsF); }
81 virtual Real logZ() const { return _logZ; }
82 virtual void init() {};
83 virtual void init( const VarSet & ) {};
84 virtual Real run();
85 virtual Real maxDiff() const { return _maxdiff; }
86 virtual size_t Iterations() const { return _iters; }
87 virtual void setProperties( const PropertySet &opts ) { props.set( opts ); }
88 virtual PropertySet getProperties() const { return props.get(); }
89 virtual std::string printProperties() const { return props.toString(); }
90 //@}
92 //----------------------------------------------------------------
94 /// Parameters of this inference algorithm
95 /* PROPERTIES(props,CBP) {
96 /// Enumeration of possible update schedules
97 typedef BP::Properties::UpdateType UpdateType;
98 /// Enumeration of possible methods for deciding when to stop recursing
99 DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
100 /// Enumeration of possible heuristics for choosing clamping variable
101 DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
102 /// Enumeration of possible clampings: variables or factors
103 DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
105 /// Verbosity
106 size_t verbose = 0;
108 /// Tolerance to use in BP
109 Real tol;
110 /// Update style for BP
112 /// Maximum number of iterations for BP
113 size_t maxiter;
115 /// Tolerance to use for controlling recursion depth (\a recurse is REC_LOGZ or REC_BDIFF)
116 Real rec_tol;
117 /// Maximum number of levels of recursion (\a recurse is REC_FIXED)
118 size_t max_levels = 10;
119 /// If choose==CHOOSE_BBP and maximum adjoint is less than this value, don't recurse
121 /// Heuristic for choosing clamping variable
122 ChooseMethodType choose;
123 /// Method for deciding when to stop recursing
124 RecurseType recursion;
125 /// Whether to clamp variables or factors
126 ClampType clamp;
127 /// Properties to pass to BBP
128 PropertySet bbp_props;
129 /// Cost function to use for BBP
130 BBPCostFunction bbp_cfn;
131 /// Random seed
132 size_t rand_seed = 0;
134 /// If non-empty, write clamping choices to this file
135 std::string clamp_outfile = "";
136 }
137 */
138 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
139 ./scripts/regenerate-properties include/dai/cbp.h src/cbp.cpp
140 */
141 struct Properties {
142 /// Enumeration of possible update schedules
143 typedef BP::Properties::UpdateType UpdateType;
144 /// Enumeration of possible methods for deciding when to stop recursing
145 DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
146 /// Enumeration of possible heuristics for choosing clamping variable
147 DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
148 /// Enumeration of possible clampings: variables or factors
149 DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
150 /// Verbosity
151 size_t verbose;
152 /// Tolerance to use in BP
153 Real tol;
154 /// Update style for BP
156 /// Maximum number of iterations for BP
157 size_t maxiter;
158 /// Tolerance to use for controlling recursion depth (\a recurse is REC_LOGZ or REC_BDIFF)
159 Real rec_tol;
160 /// Maximum number of levels of recursion (\a recurse is REC_FIXED)
161 size_t max_levels;
162 /// If choose==CHOOSE_BBP and maximum adjoint is less than this value, don't recurse
164 /// Heuristic for choosing clamping variable
165 ChooseMethodType choose;
166 /// Method for deciding when to stop recursing
167 RecurseType recursion;
168 /// Whether to clamp variables or factors
169 ClampType clamp;
170 /// Properties to pass to BBP
171 PropertySet bbp_props;
172 /// Cost function to use for BBP
173 BBPCostFunction bbp_cfn;
174 /// Random seed
175 size_t rand_seed;
176 /// If non-empty, write clamping choices to this file
177 std::string clamp_outfile;
179 /// Set members from PropertySet
180 void set(const PropertySet &opts);
181 /// Get members into PropertySet
182 PropertySet get() const;
183 /// Convert to a string which can be parsed as a PropertySet
184 std::string toString() const;
185 } props;
186 /* }}} END OF GENERATED CODE */
188 private:
189 /// Prints beliefs, variables and partition sum, in case of a debugging build
190 void printDebugInfo();
192 /// Called by run(), and by itself. Implements the main algorithm.
193 /** Chooses a variable to clamp, recurses, combines the partition sum
194 * and belief estimates of the children, and returns the improved
195 * estimates in \a lz_out and \a beliefs_out to its parent.
196 */
197 void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
198 size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
200 /// Choose the next variable to clamp.
201 /** Choose the next variable to clamp, given a converged InfAlg \a bp,
202 * and a vector of variables that are already clamped (\a
203 * clamped_vars_list). Returns the chosen variable in \a i, and
204 * the set of states in \a xis. If \a maxVarOut is non-NULL and
205 * \a props.choose == \c CHOOSE_BBP then it is used to store the
206 * adjoint of the chosen variable.
207 */
208 virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
210 /// Return the InfAlg to use at each step of the recursion.
211 /// \todo At present, CBP::getInfAlg() only returns a BP instance
212 InfAlg* getInfAlg();
214 /// Sets variable beliefs, factor beliefs and log partition sum
215 /** \param bs should be a concatenation of the variable beliefs followed by the factor beliefs
216 */
217 void setBeliefs( const std::vector<Factor> &bs, Real logZ );
219 /// Constructor helper function
220 void construct();
221 };
224 /// Find the best variable/factor to clamp using BBP.
225 /** Takes a converged inference algorithm as input, runs Gibbs and BP_dual, creates
226 * and runs a BBP object, finds the best variable/factor (the one with the maximum
227 * factor adjoint), and returns the corresponding (index,state) pair.
228 * \param in_bp inference algorithm (compatible with BP) that should have converged;
229 * \param clampingVar if \c true, finds best variable, otherwise, finds best factor;
230 * \param bbp_props BBP parameters to use;
231 * \param cfn BBP cost function to use;
232 * \param maxVarOut maximum adjoint value (only set if not NULL).
233 * \see BBP
234 * \relates CBP
235 */
236 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut );
239 } // end of namespace dai
242 #endif