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