c649fe438381c498b6bc3c796c0d00d5d8c10a35
[libdai.git] / src / cbp.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <iostream>
10 #include <sstream>
11 #include <map>
12 #include <set>
13 #include <algorithm>
14
15 #include <dai/util.h>
16 #include <dai/properties.h>
17 #include <dai/gibbs.h>
18 #include <dai/bp.h>
19 #include <dai/cbp.h>
20 #include <dai/bbp.h>
21
22
23 namespace dai {
24
25
26 using namespace std;
27 using boost::shared_ptr;
28
29
30 /// Given a sorted vector of states \a xis and total state count \a n_states, return a vector of states not in \a xis
31 vector<size_t> complement( vector<size_t> &xis, size_t n_states ) {
32 vector<size_t> cmp_xis( 0 );
33 size_t j = 0;
34 for( size_t xi = 0; xi < n_states; xi++ ) {
35 while( j < xis.size() && xis[j] < xi )
36 j++;
37 if( j >= xis.size() || xis[j] > xi )
38 cmp_xis.push_back(xi);
39 }
40 DAI_ASSERT( xis.size()+cmp_xis.size() == n_states );
41 return cmp_xis;
42 }
43
44
45 /// Computes \f$\frac{\exp(a)}{\exp(a)+\exp(b)}\f$
46 Real unSoftMax( Real a, Real b ) {
47 if( a > b )
48 return 1.0 / (1.0 + exp(b-a));
49 else
50 return exp(a-b) / (exp(a-b) + 1.0);
51 }
52
53
54 /// Computes log of sum of exponents, i.e., \f$\log\left(\exp(a) + \exp(b)\right)\f$
55 Real logSumExp( Real a, Real b ) {
56 if( a > b )
57 return a + log1p( exp( b-a ) );
58 else
59 return b + log1p( exp( a-b ) );
60 }
61
62
63 /// Compute sum of pairwise L-infinity distances of the first \a nv factors in each vector
64 Real dist( const vector<Factor> &b1, const vector<Factor> &b2, size_t nv ) {
65 Real d = 0.0;
66 for( size_t k = 0; k < nv; k++ )
67 d += dist( b1[k], b2[k], DISTLINF );
68 return d;
69 }
70
71
72 void CBP::setBeliefs( const std::vector<Factor> &bs, Real logZ ) {
73 size_t i = 0;
74 _beliefsV.clear();
75 _beliefsV.reserve( nrVars() );
76 _beliefsF.clear();
77 _beliefsF.reserve( nrFactors() );
78 for( i = 0; i < nrVars(); i++ )
79 _beliefsV.push_back( bs[i] );
80 for( ; i < nrVars() + nrFactors(); i++ )
81 _beliefsF.push_back( bs[i] );
82 _logZ = logZ;
83 }
84
85
86 void CBP::construct() {
87 _beliefsV.clear();
88 _beliefsV.reserve(nrVars());
89 for( size_t i = 0; i < nrVars(); i++ )
90 _beliefsV.push_back( Factor(var(i)).normalized() );
91
92 _beliefsF.clear();
93 _beliefsF.reserve(nrFactors());
94 for( size_t I = 0; I < nrFactors(); I++ ) {
95 Factor f = factor(I);
96 f.fill(1); f.normalize();
97 _beliefsF.push_back( f );
98 }
99
100 // to compute average level
101 _sum_level = 0;
102 _num_leaves = 0;
103
104 _maxdiff = 0;
105 _iters = 0;
106
107 if( props.clamp_outfile.length() > 0 ) {
108 _clamp_ofstream = shared_ptr<ofstream>(new ofstream( props.clamp_outfile.c_str(), ios_base::out|ios_base::trunc ));
109 *_clamp_ofstream << "# COUNT LEVEL VAR STATE" << endl;
110 }
111 }
112
113
114 /// Calculates a vector of mixtures p * b + (1-p) * c
115 static vector<Factor> mixBeliefs( Real p, const vector<Factor> &b, const vector<Factor> &c ) {
116 vector<Factor> out;
117 DAI_ASSERT( b.size() == c.size() );
118 out.reserve( b.size() );
119 Real pc = 1 - p;
120 for( size_t i = 0; i < b.size(); i++ )
121 // probably already normalized, but do it again just in case
122 out.push_back( b[i].normalized() * p + c[i].normalized() * pc );
123 return out;
124 }
125
126
127 Real CBP::run() {
128 size_t seed = props.rand_seed;
129 if( seed > 0 )
130 rnd_seed( seed );
131
132 InfAlg *bp = getInfAlg();
133 bp->init();
134 bp->run();
135 _iters += bp->Iterations();
136
137 vector<Factor> beliefs_out;
138 Real lz_out;
139 size_t choose_count=0;
140 runRecurse( bp, bp->logZ(), vector<size_t>(0), _num_leaves, choose_count, _sum_level, lz_out, beliefs_out );
141 if( props.verbose >= 1 )
142 cerr << "CBP average levels = " << (_sum_level / _num_leaves) << ", leaves = " << _num_leaves << endl;
143 setBeliefs( beliefs_out, lz_out );
144 delete bp;
145 return 0.0;
146 }
147
148
149 InfAlg* CBP::getInfAlg() {
150 PropertySet bpProps;
151 bpProps.set("updates", props.updates);
152 bpProps.set("tol", props.tol);
153 bpProps.set("maxiter", props.maxiter);
154 bpProps.set("verbose", props.verbose);
155 bpProps.set("logdomain", false);
156 bpProps.set("damping", (Real)0.0);
157 BP *bp = new BP( *this, bpProps );
158 bp->recordSentMessages = true;
159 bp->init();
160 return bp;
161 }
162
163
164 void CBP::runRecurse( InfAlg *bp, Real orig_logZ, vector<size_t> clamped_vars_list, size_t &num_leaves,
165 size_t &choose_count, Real &sum_level, Real &lz_out, vector<Factor>& beliefs_out) {
166 // choose a variable/states to clamp:
167 size_t i;
168 vector<size_t> xis;
169 Real maxVar = 0.0;
170 bool found;
171 bool clampingVar = (props.clamp == Properties::ClampType::CLAMP_VAR);
172
173 if( props.recursion == Properties::RecurseType::REC_LOGZ && props.rec_tol > 0 && exp( bp->logZ() - orig_logZ ) < props.rec_tol )
174 found = false;
175 else
176 found = chooseNextClampVar( bp, clamped_vars_list, i, xis, &maxVar );
177
178 if( !found ) {
179 num_leaves++;
180 sum_level += clamped_vars_list.size();
181 beliefs_out = bp->beliefs();
182 lz_out = bp->logZ();
183 return;
184 }
185
186 choose_count++;
187 if( props.clamp_outfile.length() > 0 )
188 *_clamp_ofstream << choose_count << "\t" << clamped_vars_list.size() << "\t" << i << "\t" << xis[0] << endl;
189
190 if( clampingVar )
191 bforeach( size_t xi, xis )
192 DAI_ASSERT(/*0<=xi &&*/ xi < var(i).states() );
193 else
194 bforeach( size_t xI, xis )
195 DAI_ASSERT(/*0<=xI &&*/ xI < factor(i).nrStates() );
196 // - otherwise, clamp and recurse, saving margin estimates for each
197 // clamp setting. afterwards, combine estimates.
198
199 // compute complement of 'xis'
200 vector<size_t> cmp_xis = complement( xis, clampingVar ? var(i).states() : factor(i).nrStates() );
201
202 /// \idea dai::CBP::runRecurse() could be implemented more efficiently with a nesting version of backupFactors/restoreFactors
203 // this improvement could also be done locally: backup the clamped factor in a local variable,
204 // and restore it just before we return.
205 Real lz;
206 vector<Factor> b;
207 InfAlg *bp_c = bp->clone();
208 if( clampingVar ) {
209 bp_c->fg().clampVar( i, xis );
210 bp_c->init( var(i) );
211 } else {
212 bp_c->fg().clampFactor( i, xis );
213 bp_c->init( factor(i).vars() );
214 }
215 bp_c->run();
216 _iters += bp_c->Iterations();
217
218 lz = bp_c->logZ();
219 b = bp_c->beliefs();
220
221 Real cmp_lz;
222 vector<Factor> cmp_b;
223 InfAlg *cmp_bp_c = bp->clone();
224 if( clampingVar ) {
225 cmp_bp_c->fg().clampVar( i, cmp_xis );
226 cmp_bp_c->init(var(i));
227 } else {
228 cmp_bp_c->fg().clampFactor( i, cmp_xis );
229 cmp_bp_c->init( factor(i).vars() );
230 }
231 cmp_bp_c->run();
232 _iters += cmp_bp_c->Iterations();
233
234 cmp_lz = cmp_bp_c->logZ();
235 cmp_b = cmp_bp_c->beliefs();
236
237 Real p = unSoftMax( lz, cmp_lz );
238 Real bp__d = 0.0;
239
240 if( props.recursion == Properties::RecurseType::REC_BDIFF && props.rec_tol > 0 ) {
241 vector<Factor> combined_b( mixBeliefs( p, b, cmp_b ) );
242 Real new_lz = logSumExp( lz,cmp_lz );
243 bp__d = dist( bp->beliefs(), combined_b, nrVars() );
244 if( exp( new_lz - orig_logZ) * bp__d < props.rec_tol ) {
245 num_leaves++;
246 sum_level += clamped_vars_list.size();
247 beliefs_out = combined_b;
248 lz_out = new_lz;
249 return;
250 }
251 }
252
253 // either we are not doing REC_BDIFF or the distance was large
254 // enough to recurse:
255 runRecurse( bp_c, orig_logZ, clamped_vars_list, num_leaves, choose_count, sum_level, lz, b );
256 runRecurse( cmp_bp_c, orig_logZ, clamped_vars_list, num_leaves, choose_count, sum_level, cmp_lz, cmp_b );
257
258 p = unSoftMax( lz, cmp_lz );
259
260 beliefs_out = mixBeliefs( p, b, cmp_b );
261 lz_out = logSumExp( lz, cmp_lz );
262
263 if( props.verbose >= 2 ) {
264 Real d = dist( bp->beliefs(), beliefs_out, nrVars() );
265 cerr << "Distance (clamping " << i << "): " << d;
266 if( props.recursion == Properties::RecurseType::REC_BDIFF )
267 cerr << "; bp_dual predicted " << bp__d;
268 cerr << "; max_adjoint = " << maxVar << "; logZ = " << lz_out << " (in " << bp->logZ() << ") (orig " << orig_logZ << "); p = " << p << "; level = " << clamped_vars_list.size() << endl;
269 }
270
271 delete bp_c;
272 delete cmp_bp_c;
273 }
274
275
276 // 'xis' must be sorted
277 bool CBP::chooseNextClampVar( InfAlg *bp, vector<size_t> &clamped_vars_list, size_t &i, vector<size_t> &xis, Real *maxVarOut ) {
278 Real tiny = 1.0e-14;
279 if( props.verbose >= 3 )
280 cerr << "clamped_vars_list" << clamped_vars_list << endl;
281 if( clamped_vars_list.size() >= props.max_levels )
282 return false;
283 if( props.choose == Properties::ChooseMethodType::CHOOSE_RANDOM ) {
284 if( props.clamp == Properties::ClampType::CLAMP_VAR ) {
285 int t = 0, t1 = 100;
286 do {
287 i = rnd( nrVars() );
288 t++;
289 } while( abs( bp->beliefV(i).p().max() - 1) < tiny && t < t1 );
290 if( t == t1 ) {
291 return false;
292 // die("Too many levels requested in CBP");
293 }
294 // only pick probable values for variable
295 size_t xi;
296 do {
297 xi = rnd( var(i).states() );
298 t++;
299 } while( bp->beliefV(i).p()[xi] < tiny && t < t1 );
300 DAI_ASSERT( t < t1 );
301 xis.resize( 1, xi );
302 // DAI_ASSERT(!_clamped_vars.count(i)); // not true for >2-ary variables
303 DAI_IFVERB(2, "CHOOSE_RANDOM at level " << clamped_vars_list.size() << " chose variable " << i << " state " << xis[0] << endl);
304 } else {
305 int t = 0, t1 = 100;
306 do {
307 i = rnd( nrFactors() );
308 t++;
309 } while( abs( bp->beliefF(i).p().max() - 1) < tiny && t < t1 );
310 if( t == t1 )
311 return false;
312 // die("Too many levels requested in CBP");
313 // only pick probable values for variable
314 size_t xi;
315 do {
316 xi = rnd( factor(i).nrStates() );
317 t++;
318 } while( bp->beliefF(i).p()[xi] < tiny && t < t1 );
319 DAI_ASSERT( t < t1 );
320 xis.resize( 1, xi );
321 // DAI_ASSERT(!_clamped_vars.count(i)); // not true for >2-ary variables
322 DAI_IFVERB(2, endl<<"CHOOSE_RANDOM chose factor "<<i<<" state "<<xis[0]<<endl);
323 }
324 } else if( props.choose == Properties::ChooseMethodType::CHOOSE_MAXENT ) {
325 if( props.clamp == Properties::ClampType::CLAMP_VAR ) {
326 Real max_ent = -1.0;
327 int win_k = -1, win_xk = -1;
328 for( size_t k = 0; k < nrVars(); k++ ) {
329 Real ent=bp->beliefV(k).entropy();
330 if( max_ent < ent ) {
331 max_ent = ent;
332 win_k = k;
333 win_xk = bp->beliefV(k).p().argmax().first;
334 }
335 }
336 DAI_ASSERT( win_k >= 0 );
337 DAI_ASSERT( win_xk >= 0 );
338 i = win_k;
339 xis.resize( 1, win_xk );
340 DAI_IFVERB(2, endl<<"CHOOSE_MAXENT chose variable "<<i<<" state "<<xis[0]<<endl);
341 if( bp->beliefV(i).p()[xis[0]] < tiny ) {
342 DAI_IFVERB(2, "Warning: CHOOSE_MAXENT found unlikely state, not recursing");
343 return false;
344 }
345 } else {
346 Real max_ent = -1.0;
347 int win_k = -1, win_xk = -1;
348 for( size_t k = 0; k < nrFactors(); k++ ) {
349 Real ent = bp->beliefF(k).entropy();
350 if( max_ent < ent ) {
351 max_ent = ent;
352 win_k = k;
353 win_xk = bp->beliefF(k).p().argmax().first;
354 }
355 }
356 DAI_ASSERT( win_k >= 0 );
357 DAI_ASSERT( win_xk >= 0 );
358 i = win_k;
359 xis.resize( 1, win_xk );
360 DAI_IFVERB(2, endl<<"CHOOSE_MAXENT chose factor "<<i<<" state "<<xis[0]<<endl);
361 if( bp->beliefF(i).p()[xis[0]] < tiny ) {
362 DAI_IFVERB(2, "Warning: CHOOSE_MAXENT found unlikely state, not recursing");
363 return false;
364 }
365 }
366 } else if( props.choose==Properties::ChooseMethodType::CHOOSE_BP_L1 ||
367 props.choose==Properties::ChooseMethodType::CHOOSE_BP_CFN ) {
368 bool doL1 = (props.choose == Properties::ChooseMethodType::CHOOSE_BP_L1);
369 vector<size_t> state;
370 if( !doL1 && props.bbp_cfn.needGibbsState() )
371 state = getGibbsState( bp->fg(), 2*bp->Iterations() );
372 // try clamping each variable manually
373 DAI_ASSERT( props.clamp == Properties::ClampType::CLAMP_VAR );
374 Real max_cost = 0.0;
375 int win_k = -1, win_xk = -1;
376 for( size_t k = 0; k < nrVars(); k++ ) {
377 for( size_t xk = 0; xk < var(k).states(); xk++ ) {
378 if( bp->beliefV(k)[xk] < tiny )
379 continue;
380 InfAlg *bp1 = bp->clone();
381 bp1->clamp( k, xk );
382 bp1->init( var(k) );
383 bp1->run();
384 Real cost = 0;
385 if( doL1 )
386 for( size_t j = 0; j < nrVars(); j++ )
387 cost += dist( bp->beliefV(j), bp1->beliefV(j), DISTL1 );
388 else
389 cost = props.bbp_cfn.evaluate( *bp1, &state );
390 if( cost > max_cost || win_k == -1 ) {
391 max_cost = cost;
392 win_k = k;
393 win_xk = xk;
394 }
395 delete bp1;
396 }
397 }
398 DAI_ASSERT( win_k >= 0 );
399 DAI_ASSERT( win_xk >= 0 );
400 i = win_k;
401 xis.resize( 1, win_xk );
402 } else if( props.choose == Properties::ChooseMethodType::CHOOSE_BBP ) {
403 Real mvo;
404 if( !maxVarOut )
405 maxVarOut = &mvo;
406 bool clampingVar = (props.clamp == Properties::ClampType::CLAMP_VAR);
407 pair<size_t, size_t> cv = BBPFindClampVar( *bp, clampingVar, props.bbp_props, props.bbp_cfn, &mvo );
408
409 // if slope isn't big enough then don't clamp
410 if( mvo < props.min_max_adj )
411 return false;
412
413 size_t xi = cv.second;
414 i = cv.first;
415 #define VAR_INFO (clampingVar?"variable ":"factor ") \
416 << i << " state " << xi \
417 << " (p=" << (clampingVar?bp->beliefV(i)[xi]:bp->beliefF(i)[xi]) \
418 << ", entropy = " << (clampingVar?bp->beliefV(i):bp->beliefF(i)).entropy() \
419 << ", maxVar = "<< mvo << ")"
420 Prob b = ( clampingVar ? bp->beliefV(i).p() : bp->beliefF(i).p());
421 if( b[xi] < tiny ) {
422 cerr << "Warning, at level " << clamped_vars_list.size() << ", BBPFindClampVar found unlikely " << VAR_INFO << endl;
423 return false;
424 }
425 if( abs(b[xi] - 1) < tiny ) {
426 cerr << "Warning at level " << clamped_vars_list.size() << ", BBPFindClampVar found overly likely " << VAR_INFO << endl;
427 return false;
428 }
429
430 xis.resize( 1, xi );
431 if( clampingVar )
432 DAI_ASSERT(/*0<=xi &&*/ xi < var(i).states() );
433 else
434 DAI_ASSERT(/*0<=xi &&*/ xi < factor(i).nrStates() );
435 DAI_IFVERB(2, "CHOOSE_BBP (num clamped = " << clamped_vars_list.size() << ") chose " << i << " state " << xi << endl);
436 } else
437 DAI_THROW(UNKNOWN_ENUM_VALUE);
438 clamped_vars_list.push_back( i );
439 return true;
440 }
441
442
443 void CBP::printDebugInfo() {
444 DAI_PV(_beliefsV);
445 DAI_PV(_beliefsF);
446 DAI_PV(_logZ);
447 }
448
449
450 std::pair<size_t, size_t> BBPFindClampVar( const InfAlg &in_bp, bool clampingVar, const PropertySet &bbp_props, const BBPCostFunction &cfn, Real *maxVarOut ) {
451 BBP bbp( &in_bp, bbp_props );
452 bbp.initCostFnAdj( cfn, NULL );
453 bbp.run();
454
455 // find and return the (variable,state) with the largest adj_psi_V
456 size_t argmax_var = 0;
457 size_t argmax_var_state = 0;
458 Real max_var = 0;
459 if( clampingVar ) {
460 for( size_t i = 0; i < in_bp.fg().nrVars(); i++ ) {
461 Prob adj_psi_V = bbp.adj_psi_V(i);
462 if(0) {
463 // helps to account for amount of movement possible in variable
464 // i's beliefs? seems not..
465 adj_psi_V *= in_bp.beliefV(i).entropy();
466 }
467 if(0){
468 // adj_psi_V *= Prob(in_bp.fg().var(i).states(),1.0)-in_bp.beliefV(i).p();
469 adj_psi_V *= in_bp.beliefV(i).p();
470 }
471 // try to compensate for effect on same variable (doesn't work)
472 // adj_psi_V[gibbs.state()[i]] -= bp_dual.beliefV(i)[gibbs.state()[i]]/10;
473 pair<size_t,Real> argmax_state = adj_psi_V.argmax();
474
475 if( i == 0 || argmax_state.second > max_var ) {
476 argmax_var = i;
477 max_var = argmax_state.second;
478 argmax_var_state = argmax_state.first;
479 }
480 }
481 DAI_ASSERT(/*0 <= argmax_var_state &&*/
482 argmax_var_state < in_bp.fg().var(argmax_var).states() );
483 } else {
484 for( size_t I = 0; I < in_bp.fg().nrFactors(); I++ ) {
485 Prob adj_psi_F = bbp.adj_psi_F(I);
486 if(0) {
487 // helps to account for amount of movement possible in variable
488 // i's beliefs? seems not..
489 adj_psi_F *= in_bp.beliefF(I).entropy();
490 }
491 // try to compensate for effect on same variable (doesn't work)
492 // adj_psi_V[gibbs.state()[i]] -= bp_dual.beliefV(i)[gibbs.state()[i]]/10;
493 pair<size_t,Real> argmax_state = adj_psi_F.argmax();
494
495 if( I == 0 || argmax_state.second > max_var ) {
496 argmax_var = I;
497 max_var = argmax_state.second;
498 argmax_var_state = argmax_state.first;
499 }
500 }
501 DAI_ASSERT(/*0 <= argmax_var_state &&*/
502 argmax_var_state < in_bp.fg().factor(argmax_var).nrStates() );
503 }
504 if( maxVarOut )
505 *maxVarOut = max_var;
506 return make_pair( argmax_var, argmax_var_state );
507 }
508
509
510 } // end of namespace dai
511
512
513 /* {{{ GENERATED CODE: DO NOT EDIT. Created by
514 ./scripts/regenerate-properties include/dai/cbp.h src/cbp.cpp
515 */
516 namespace dai {
517
518 void CBP::Properties::set(const PropertySet &opts)
519 {
520 const std::set<PropertyKey> &keys = opts.keys();
521 std::string errormsg;
522 for( std::set<PropertyKey>::const_iterator i = keys.begin(); i != keys.end(); i++ ) {
523 if( *i == "verbose" ) continue;
524 if( *i == "tol" ) continue;
525 if( *i == "updates" ) continue;
526 if( *i == "maxiter" ) continue;
527 if( *i == "rec_tol" ) continue;
528 if( *i == "max_levels" ) continue;
529 if( *i == "min_max_adj" ) continue;
530 if( *i == "choose" ) continue;
531 if( *i == "recursion" ) continue;
532 if( *i == "clamp" ) continue;
533 if( *i == "bbp_props" ) continue;
534 if( *i == "bbp_cfn" ) continue;
535 if( *i == "rand_seed" ) continue;
536 if( *i == "clamp_outfile" ) continue;
537 errormsg = errormsg + "CBP: Unknown property " + *i + "\n";
538 }
539 if( !errormsg.empty() )
540 DAI_THROWE(UNKNOWN_PROPERTY, errormsg);
541 if( !opts.hasKey("tol") )
542 errormsg = errormsg + "CBP: Missing property \"tol\" for method \"CBP\"\n";
543 if( !opts.hasKey("updates") )
544 errormsg = errormsg + "CBP: Missing property \"updates\" for method \"CBP\"\n";
545 if( !opts.hasKey("maxiter") )
546 errormsg = errormsg + "CBP: Missing property \"maxiter\" for method \"CBP\"\n";
547 if( !opts.hasKey("rec_tol") )
548 errormsg = errormsg + "CBP: Missing property \"rec_tol\" for method \"CBP\"\n";
549 if( !opts.hasKey("min_max_adj") )
550 errormsg = errormsg + "CBP: Missing property \"min_max_adj\" for method \"CBP\"\n";
551 if( !opts.hasKey("choose") )
552 errormsg = errormsg + "CBP: Missing property \"choose\" for method \"CBP\"\n";
553 if( !opts.hasKey("recursion") )
554 errormsg = errormsg + "CBP: Missing property \"recursion\" for method \"CBP\"\n";
555 if( !opts.hasKey("clamp") )
556 errormsg = errormsg + "CBP: Missing property \"clamp\" for method \"CBP\"\n";
557 if( !opts.hasKey("bbp_props") )
558 errormsg = errormsg + "CBP: Missing property \"bbp_props\" for method \"CBP\"\n";
559 if( !opts.hasKey("bbp_cfn") )
560 errormsg = errormsg + "CBP: Missing property \"bbp_cfn\" for method \"CBP\"\n";
561 if( !errormsg.empty() )
562 DAI_THROWE(NOT_ALL_PROPERTIES_SPECIFIED,errormsg);
563 if( opts.hasKey("verbose") ) {
564 verbose = opts.getStringAs<size_t>("verbose");
565 } else {
566 verbose = 0;
567 }
568 tol = opts.getStringAs<Real>("tol");
569 updates = opts.getStringAs<UpdateType>("updates");
570 maxiter = opts.getStringAs<size_t>("maxiter");
571 rec_tol = opts.getStringAs<Real>("rec_tol");
572 if( opts.hasKey("max_levels") ) {
573 max_levels = opts.getStringAs<size_t>("max_levels");
574 } else {
575 max_levels = 10;
576 }
577 min_max_adj = opts.getStringAs<Real>("min_max_adj");
578 choose = opts.getStringAs<ChooseMethodType>("choose");
579 recursion = opts.getStringAs<RecurseType>("recursion");
580 clamp = opts.getStringAs<ClampType>("clamp");
581 bbp_props = opts.getStringAs<PropertySet>("bbp_props");
582 bbp_cfn = opts.getStringAs<BBPCostFunction>("bbp_cfn");
583 if( opts.hasKey("rand_seed") ) {
584 rand_seed = opts.getStringAs<size_t>("rand_seed");
585 } else {
586 rand_seed = 0;
587 }
588 if( opts.hasKey("clamp_outfile") ) {
589 clamp_outfile = opts.getStringAs<std::string>("clamp_outfile");
590 } else {
591 clamp_outfile = "";
592 }
593 }
594 PropertySet CBP::Properties::get() const {
595 PropertySet opts;
596 opts.set("verbose", verbose);
597 opts.set("tol", tol);
598 opts.set("updates", updates);
599 opts.set("maxiter", maxiter);
600 opts.set("rec_tol", rec_tol);
601 opts.set("max_levels", max_levels);
602 opts.set("min_max_adj", min_max_adj);
603 opts.set("choose", choose);
604 opts.set("recursion", recursion);
605 opts.set("clamp", clamp);
606 opts.set("bbp_props", bbp_props);
607 opts.set("bbp_cfn", bbp_cfn);
608 opts.set("rand_seed", rand_seed);
609 opts.set("clamp_outfile", clamp_outfile);
610 return opts;
611 }
612 string CBP::Properties::toString() const {
613 stringstream s(stringstream::out);
614 s << "[";
615 s << "verbose=" << verbose << ",";
616 s << "tol=" << tol << ",";
617 s << "updates=" << updates << ",";
618 s << "maxiter=" << maxiter << ",";
619 s << "rec_tol=" << rec_tol << ",";
620 s << "max_levels=" << max_levels << ",";
621 s << "min_max_adj=" << min_max_adj << ",";
622 s << "choose=" << choose << ",";
623 s << "recursion=" << recursion << ",";
624 s << "clamp=" << clamp << ",";
625 s << "bbp_props=" << bbp_props << ",";
626 s << "bbp_cfn=" << bbp_cfn << ",";
627 s << "rand_seed=" << rand_seed << ",";
628 s << "clamp_outfile=" << clamp_outfile;
629 s << "]";
630 return s.str();
631 }
632 } // end of namespace dai
633 /* }}} END OF GENERATED CODE */