Improved documentation of include/dai/exactinf.h
[libdai.git] / include / dai / cbp.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
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 */
9
10
11 /// \file
12 /// \brief Defines class CBP [\ref EaG09]
13 /// \todo Improve documentation
14
15
16 #ifndef __defined_libdai_cbp_h
17 #define __defined_libdai_cbp_h
18
19
20 #include <fstream>
21 #include <boost/shared_ptr.hpp>
22
23 #include <dai/daialg.h>
24 #include <dai/bbp.h>
25
26
27 namespace dai {
28
29
30 /// Find a variable to clamp using BBP (goes with maximum adjoint)
31 /// \see BBP
32 std::pair<size_t, size_t> bbpFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, bbp_cfn_t cfn, Real *maxVarOut );
33
34
35 /// Class for CBP (Clamped Belief Propagation)
36 /** This algorithm uses configurable heuristics to choose a variable
37 * x_i and a state x_i*. Inference is done with x_i "clamped" to x_i*
38 * (i.e., conditional on x_i == x_i*), and also with the negation of this
39 * condition. Clamping is done recursively up to a fixed number of
40 * levels (other stopping criteria are also implemented, see
41 * \a recursion property). The resulting approximate marginals are
42 * combined using logZ estimates.
43 */
44 class CBP : public DAIAlgFG {
45 private:
46 /// Variable beliefs
47 std::vector<Factor> _beliefsV;
48 /// Factor beliefs
49 std::vector<Factor> _beliefsF;
50 /// Log-partition sum
51 Real _logZ;
52
53 /// Counts number of clampings at each leaf node
54 Real _sum_level;
55
56 /// Number of leaves of recursion tree
57 size_t _num_leaves;
58
59 /// Output stream where information about the clampings is written
60 boost::shared_ptr<std::ofstream> _clamp_ofstream;
61
62 /// Returns BBP cost function used
63 bbp_cfn_t BBP_cost_function() { return props.bbp_cfn; }
64
65 /// Prints beliefs, variables and partition sum, in case of a debugging build
66 void printDebugInfo();
67
68 /// Called by 'run', and by itself. Implements the main algorithm.
69 /** Chooses a variable to clamp, recurses, combines the logZ and
70 * beliefs estimates of the children, and returns the improved
71 * estimates in \a lz_out and \a beliefs_out to its parent
72 */
73 void runRecurse( InfAlg *bp, Real orig_logZ, std::vector<size_t> clamped_vars_list, size_t &num_leaves,
74 size_t &choose_count, Real &sum_level, Real &lz_out, std::vector<Factor> &beliefs_out );
75
76 /// Choose the next variable to clamp
77 /** Choose the next variable to clamp, given a converged InfAlg (\a bp),
78 * and a vector of variables that are already clamped (\a
79 * clamped_vars_list). Returns the chosen variable in \a i, and
80 * the set of states in \a xis. If \a maxVarOut is non-NULL and
81 * props.choose==CHOOSE_BBP then it is used to store the
82 * adjoint of the chosen variable
83 */
84 virtual bool chooseNextClampVar( InfAlg* bp, std::vector<size_t> &clamped_vars_list, size_t &i, std::vector<size_t> &xis, Real *maxVarOut );
85
86 /// Return the InfAlg to use at each step of the recursion.
87 /// \todo At present, only returns a BP instance
88 InfAlg* getInfAlg();
89
90 /// Numer of iterations needed
91 size_t _iters;
92 /// Maximum difference encountered so far
93 Real _maxdiff;
94
95 /// Sets variable beliefs, factor beliefs and logZ
96 /** \param bs should be a concatenation of the variable beliefs followed by the factor beliefs
97 */
98 void setBeliefs( const std::vector<Factor> &bs, Real logZ );
99
100 /// Constructor helper function
101 void construct();
102
103 public:
104 /// Construct CBP object from FactorGraph fg and PropertySet opts
105 CBP( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg) {
106 props.set( opts );
107 construct();
108 }
109
110 /// Name of this inference algorithm
111 static const char *Name;
112
113 /// \name General InfAlg interface
114 //@{
115 virtual CBP* clone() const { return new CBP(*this); }
116 virtual std::string identify() const { return std::string(Name) + props.toString(); }
117 virtual Factor belief (const Var &n) const { return _beliefsV[findVar(n)]; }
118 virtual Factor belief (const VarSet &) const { DAI_THROW(NOT_IMPLEMENTED); }
119 virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
120 virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
121 virtual std::vector<Factor> beliefs() const { return concat(_beliefsV, _beliefsF); }
122 virtual Real logZ() const { return _logZ; }
123 virtual void init() {};
124 virtual void init( const VarSet & ) {};
125 virtual Real run();
126 virtual Real maxDiff() const { return _maxdiff; }
127 virtual size_t Iterations() const { return _iters; }
128 //@}
129
130 //----------------------------------------------------------------
131
132 /// Parameters of this inference algorithm
133 /* PROPERTIES(props,CBP) {
134 /// Enumeration of possible update schedules
135 typedef BP::Properties::UpdateType UpdateType;
136 /// Enumeration of possible methods for deciding when to stop recursing
137 DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
138 /// Enumeration of possible heuristics for choosing clamping variable
139 DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
140 /// Enumeration of possible clampings: variables or factors
141 DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
142
143 /// Verbosity
144 size_t verbose = 0;
145
146 /// Tolerance to use in BP
147 Real tol;
148 /// Update style for BP
149 UpdateType updates;
150 /// Maximum number of iterations for BP
151 size_t maxiter;
152
153 /// Tolerance to use for controlling recursion depth (\a recurse is REC_LOGZ or REC_BDIFF)
154 Real rec_tol;
155 /// Maximum number of levels of recursion (\a recurse is REC_FIXED)
156 size_t max_levels = 10;
157 /// If choose==CHOOSE_BBP and maximum adjoint is less than this value, don't recurse
158 Real min_max_adj;
159 /// Heuristic for choosing clamping variable
160 ChooseMethodType choose;
161 /// Method for deciding when to stop recursing
162 RecurseType recursion;
163 /// Whether to clamp variables or factors
164 ClampType clamp;
165 /// Properties to pass to BBP
166 PropertySet bbp_props;
167 /// Cost function to use for BBP
168 bbp_cfn_t bbp_cfn;
169 /// Random seed
170 size_t rand_seed = 0;
171
172 /// If non-empty, write clamping choices to this file
173 std::string clamp_outfile = "";
174 }
175 */
176 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
177 ./scripts/regenerate-properties include/dai/cbp.h src/cbp.cpp
178 */
179 struct Properties {
180 /// Enumeration of possible update schedules
181 typedef BP::Properties::UpdateType UpdateType;
182 /// Enumeration of possible methods for deciding when to stop recursing
183 DAI_ENUM(RecurseType,REC_FIXED,REC_LOGZ,REC_BDIFF);
184 /// Enumeration of possible heuristics for choosing clamping variable
185 DAI_ENUM(ChooseMethodType,CHOOSE_RANDOM,CHOOSE_MAXENT,CHOOSE_BBP,CHOOSE_BP_L1,CHOOSE_BP_CFN);
186 /// Enumeration of possible clampings: variables or factors
187 DAI_ENUM(ClampType,CLAMP_VAR,CLAMP_FACTOR);
188 /// Verbosity
189 size_t verbose;
190 /// Tolerance to use in BP
191 Real tol;
192 /// Update style for BP
193 UpdateType updates;
194 /// Maximum number of iterations for BP
195 size_t maxiter;
196 /// Tolerance to use for controlling recursion depth (\a recurse is REC_LOGZ or REC_BDIFF)
197 Real rec_tol;
198 /// Maximum number of levels of recursion (\a recurse is REC_FIXED)
199 size_t max_levels;
200 /// If choose==CHOOSE_BBP and maximum adjoint is less than this value, don't recurse
201 Real min_max_adj;
202 /// Heuristic for choosing clamping variable
203 ChooseMethodType choose;
204 /// Method for deciding when to stop recursing
205 RecurseType recursion;
206 /// Whether to clamp variables or factors
207 ClampType clamp;
208 /// Properties to pass to BBP
209 PropertySet bbp_props;
210 /// Cost function to use for BBP
211 bbp_cfn_t bbp_cfn;
212 /// Random seed
213 size_t rand_seed;
214 /// If non-empty, write clamping choices to this file
215 std::string clamp_outfile;
216
217 /// Set members from PropertySet
218 void set(const PropertySet &opts);
219 /// Get members into PropertySet
220 PropertySet get() const;
221 /// Convert to a string which can be parsed as a PropertySet
222 std::string toString() const;
223 } props;
224 /* }}} END OF GENERATED CODE */
225
226 /// Returns heuristic used for clamping variable
227 Properties::ChooseMethodType ChooseMethod() { return props.choose; }
228 /// Returns method used for deciding when to stop recursing
229 Properties::RecurseType Recursion() { return props.recursion; }
230 /// Returns clamping type used
231 Properties::ClampType Clamping() { return props.clamp; }
232 /// Returns maximum number of levels of recursion
233 size_t maxClampLevel() { return props.max_levels; }
234 /// Returns props.min_max_adj @see CBP::Properties::min_max_adj
235 Real minMaxAdj() { return props.min_max_adj; }
236 /// Returns tolerance used for controlling recursion depth
237 Real recTol() { return props.rec_tol; }
238 };
239
240
241 /// Given a sorted vector of states \a xis and total state count \a n_states, return a vector of states not in \a xis
242 std::vector<size_t> complement( std::vector<size_t>& xis, size_t n_states );
243
244 /// Computes \f$\frac{\exp(a)}{\exp(a)+\exp(b)}\f$
245 Real unSoftMax( Real a, Real b );
246
247 /// Computes log of sum of exponents, i.e., \f$\log\left(\exp(a) + \exp(b)\right)\f$
248 Real logSumExp( Real a, Real b );
249
250 /// Compute sum of pairwise L-infinity distances of the first \a nv factors in each vector
251 Real dist( const std::vector<Factor>& b1, const std::vector<Factor>& b2, size_t nv );
252
253
254 } // end of namespace dai
255
256
257 #endif