Added examples example_sprinkler_gibbs and example_sprinkler_em
[libdai.git] / src / hak.cpp
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) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <map>
13 #include <dai/hak.h>
14 #include <dai/util.h>
15 #include <dai/exceptions.h>
16
17
18 namespace dai {
19
20
21 using namespace std;
22
23
24 const char *HAK::Name = "HAK";
25
26
27 /// Sets factor entries that lie between 0 and \a epsilon to \a epsilon
28 template <class T>
29 TFactor<T>& makePositive( TFactor<T> &f, T epsilon ) {
30 for( size_t t = 0; t < f.states(); t++ )
31 if( (0 < f[t]) && (f[t] < epsilon) )
32 f[t] = epsilon;
33 return f;
34 }
35
36 /// Sets factor entries that are smaller (in absolute value) than \a epsilon to 0
37 template <class T>
38 TFactor<T>& makeZero( TFactor<T> &f, T epsilon ) {
39 for( size_t t = 0; t < f.states(); t++ )
40 if( f[t] < epsilon && f[t] > -epsilon )
41 f[t] = 0;
42 return f;
43 }
44
45
46 void HAK::setProperties( const PropertySet &opts ) {
47 DAI_ASSERT( opts.hasKey("tol") );
48 DAI_ASSERT( opts.hasKey("maxiter") );
49 DAI_ASSERT( opts.hasKey("verbose") );
50 DAI_ASSERT( opts.hasKey("doubleloop") );
51 DAI_ASSERT( opts.hasKey("clusters") );
52
53 props.tol = opts.getStringAs<Real>("tol");
54 props.maxiter = opts.getStringAs<size_t>("maxiter");
55 props.verbose = opts.getStringAs<size_t>("verbose");
56 props.doubleloop = opts.getStringAs<bool>("doubleloop");
57 props.clusters = opts.getStringAs<Properties::ClustersType>("clusters");
58
59 if( opts.hasKey("loopdepth") )
60 props.loopdepth = opts.getStringAs<size_t>("loopdepth");
61 else
62 DAI_ASSERT( props.clusters != Properties::ClustersType::LOOP );
63 if( opts.hasKey("damping") )
64 props.damping = opts.getStringAs<Real>("damping");
65 else
66 props.damping = 0.0;
67 if( opts.hasKey("init") )
68 props.init = opts.getStringAs<Properties::InitType>("init");
69 else
70 props.init = Properties::InitType::UNIFORM;
71 }
72
73
74 PropertySet HAK::getProperties() const {
75 PropertySet opts;
76 opts.Set( "tol", props.tol );
77 opts.Set( "maxiter", props.maxiter );
78 opts.Set( "verbose", props.verbose );
79 opts.Set( "doubleloop", props.doubleloop );
80 opts.Set( "clusters", props.clusters );
81 opts.Set( "init", props.init );
82 opts.Set( "loopdepth", props.loopdepth );
83 opts.Set( "damping", props.damping );
84 return opts;
85 }
86
87
88 string HAK::printProperties() const {
89 stringstream s( stringstream::out );
90 s << "[";
91 s << "tol=" << props.tol << ",";
92 s << "maxiter=" << props.maxiter << ",";
93 s << "verbose=" << props.verbose << ",";
94 s << "doubleloop=" << props.doubleloop << ",";
95 s << "clusters=" << props.clusters << ",";
96 s << "init=" << props.init << ",";
97 s << "loopdepth=" << props.loopdepth << ",";
98 s << "damping=" << props.damping << "]";
99 return s.str();
100 }
101
102
103 void HAK::construct() {
104 // Create outer beliefs
105 _Qa.clear();
106 _Qa.reserve(nrORs());
107 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
108 _Qa.push_back( Factor( OR(alpha) ) );
109
110 // Create inner beliefs
111 _Qb.clear();
112 _Qb.reserve(nrIRs());
113 for( size_t beta = 0; beta < nrIRs(); beta++ )
114 _Qb.push_back( Factor( IR(beta) ) );
115
116 // Create messages
117 _muab.clear();
118 _muab.reserve( nrORs() );
119 _muba.clear();
120 _muba.reserve( nrORs() );
121 for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
122 _muab.push_back( vector<Factor>() );
123 _muba.push_back( vector<Factor>() );
124 _muab[alpha].reserve( nbOR(alpha).size() );
125 _muba[alpha].reserve( nbOR(alpha).size() );
126 foreach( const Neighbor &beta, nbOR(alpha) ) {
127 _muab[alpha].push_back( Factor( IR(beta) ) );
128 _muba[alpha].push_back( Factor( IR(beta) ) );
129 }
130 }
131 }
132
133
134 HAK::HAK( const RegionGraph &rg, const PropertySet &opts ) : DAIAlgRG(rg), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {
135 setProperties( opts );
136
137 construct();
138 }
139
140
141 void HAK::findLoopClusters( const FactorGraph & fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars ) {
142 for( VarSet::const_iterator in = vars.begin(); in != vars.end(); in++ ) {
143 VarSet ind = fg.delta( fg.findVar( *in ) );
144 if( (newcl.size()) >= 2 && ind.contains( root ) )
145 allcl.insert( newcl | *in );
146 else if( length > 1 )
147 findLoopClusters( fg, allcl, newcl | *in, root, length - 1, ind / newcl );
148 }
149 }
150
151
152 HAK::HAK(const FactorGraph & fg, const PropertySet &opts) : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {
153 setProperties( opts );
154
155 vector<VarSet> cl;
156 if( props.clusters == Properties::ClustersType::MIN ) {
157 cl = fg.Cliques();
158 } else if( props.clusters == Properties::ClustersType::DELTA ) {
159 for( size_t i = 0; i < fg.nrVars(); i++ )
160 cl.push_back(fg.Delta(i));
161 } else if( props.clusters == Properties::ClustersType::LOOP ) {
162 cl = fg.Cliques();
163 set<VarSet> scl;
164 for( size_t i0 = 0; i0 < fg.nrVars(); i0++ ) {
165 VarSet i0d = fg.delta(i0);
166 if( props.loopdepth > 1 )
167 findLoopClusters( fg, scl, fg.var(i0), fg.var(i0), props.loopdepth - 1, fg.delta(i0) );
168 }
169 for( set<VarSet>::const_iterator c = scl.begin(); c != scl.end(); c++ )
170 cl.push_back(*c);
171 if( props.verbose >= 3 ) {
172 cerr << Name << " uses the following clusters: " << endl;
173 for( vector<VarSet>::const_iterator cli = cl.begin(); cli != cl.end(); cli++ )
174 cerr << *cli << endl;
175 }
176 } else
177 DAI_THROW(UNKNOWN_ENUM_VALUE);
178
179 RegionGraph rg(fg,cl);
180 RegionGraph::operator=(rg);
181 construct();
182
183 if( props.verbose >= 3 )
184 cerr << Name << " regiongraph: " << *this << endl;
185 }
186
187
188 string HAK::identify() const {
189 return string(Name) + printProperties();
190 }
191
192
193 void HAK::init( const VarSet &ns ) {
194 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
195 if( _Qa[alpha].vars().intersects( ns ) ) {
196 if( props.init == Properties::InitType::UNIFORM )
197 _Qa[alpha].setUniform();
198 else
199 _Qa[alpha].randomize();
200 _Qa[alpha] *= OR(alpha);
201 _Qa[alpha].normalize();
202 }
203
204 for( size_t beta = 0; beta < nrIRs(); beta++ )
205 if( IR(beta).intersects( ns ) ) {
206 if( props.init == Properties::InitType::UNIFORM )
207 _Qb[beta].fill( 1.0 );
208 else
209 _Qb[beta].randomize();
210 foreach( const Neighbor &alpha, nbIR(beta) ) {
211 size_t _beta = alpha.dual;
212 if( props.init == Properties::InitType::UNIFORM ) {
213 muab( alpha, _beta ).fill( 1.0 );
214 muba( alpha, _beta ).fill( 1.0 );
215 } else {
216 muab( alpha, _beta ).randomize();
217 muba( alpha, _beta ).randomize();
218 }
219 }
220 }
221 }
222
223
224 void HAK::init() {
225 for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
226 if( props.init == Properties::InitType::UNIFORM )
227 _Qa[alpha].setUniform();
228 else
229 _Qa[alpha].randomize();
230 _Qa[alpha] *= OR(alpha);
231 _Qa[alpha].normalize();
232 }
233
234 for( size_t beta = 0; beta < nrIRs(); beta++ )
235 if( props.init == Properties::InitType::UNIFORM )
236 _Qb[beta].setUniform();
237 else
238 _Qb[beta].randomize();
239
240 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
241 foreach( const Neighbor &beta, nbOR(alpha) ) {
242 size_t _beta = beta.iter;
243 if( props.init == Properties::InitType::UNIFORM ) {
244 muab( alpha, _beta ).setUniform();
245 muba( alpha, _beta ).setUniform();
246 } else {
247 muab( alpha, _beta ).randomize();
248 muba( alpha, _beta ).randomize();
249 }
250 }
251 }
252
253
254 Real HAK::doGBP() {
255 if( props.verbose >= 1 )
256 cerr << "Starting " << identify() << "...";
257 if( props.verbose >= 3)
258 cerr << endl;
259
260 double tic = toc();
261
262 // Check whether counting numbers won't lead to problems
263 for( size_t beta = 0; beta < nrIRs(); beta++ )
264 DAI_ASSERT( nbIR(beta).size() + IR(beta).c() != 0.0 );
265
266 // Keep old beliefs to check convergence
267 vector<Factor> oldBeliefsV;
268 oldBeliefsV.reserve( nrVars() );
269 for( size_t i = 0; i < nrVars(); i++ )
270 oldBeliefsV.push_back( beliefV(i) );
271 vector<Factor> oldBeliefsF;
272 oldBeliefsF.reserve( nrFactors() );
273 for( size_t I = 0; I < nrFactors(); I++ )
274 oldBeliefsF.push_back( beliefF(I) );
275
276 // do several passes over the network until maximum number of iterations has
277 // been reached or until the maximum belief difference is smaller than tolerance
278 Real maxDiff = INFINITY;
279 for( _iters = 0; _iters < props.maxiter && maxDiff > props.tol; _iters++ ) {
280 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
281 foreach( const Neighbor &alpha, nbIR(beta) ) {
282 size_t _beta = alpha.dual;
283 muab( alpha, _beta ) = _Qa[alpha].marginal(IR(beta)) / muba(alpha,_beta);
284 /* TODO: INVESTIGATE THIS PROBLEM
285 *
286 * In some cases, the muab's can have very large entries because the muba's have very
287 * small entries. This may cause NANs later on (e.g., multiplying large quantities may
288 * result in +inf; normalization then tries to calculate inf / inf which is NAN).
289 * A fix of this problem would consist in normalizing the messages muab.
290 * However, it is not obvious whether this is a real solution, because it has a
291 * negative performance impact and the NAN's seem to be a symptom of a fundamental
292 * numerical unstability.
293 */
294 muab(alpha,_beta).normalize();
295 }
296
297 Factor Qb_new;
298 foreach( const Neighbor &alpha, nbIR(beta) ) {
299 size_t _beta = alpha.dual;
300 Qb_new *= muab(alpha,_beta) ^ (1 / (nbIR(beta).size() + IR(beta).c()));
301 }
302
303 Qb_new.normalize();
304 if( Qb_new.hasNaNs() ) {
305 // TODO: WHAT TO DO IN THIS CASE?
306 cerr << Name << "::doGBP: Qb_new has NaNs!" << endl;
307 return 1.0;
308 }
309 /* TODO: WHAT IS THE PURPOSE OF THE FOLLOWING CODE?
310 *
311 * _Qb[beta] = Qb_new.makeZero(1e-100);
312 */
313
314 if( props.doubleloop || props.damping == 0.0 )
315 _Qb[beta] = Qb_new; // no damping for double loop
316 else
317 _Qb[beta] = (Qb_new^(1.0 - props.damping)) * (_Qb[beta]^props.damping);
318
319 foreach( const Neighbor &alpha, nbIR(beta) ) {
320 size_t _beta = alpha.dual;
321 muba(alpha,_beta) = _Qb[beta] / muab(alpha,_beta);
322
323 /* TODO: INVESTIGATE WHETHER THIS HACK (INVENTED BY KEES) TO PREVENT NANS MAKES SENSE
324 *
325 * muba(beta,*alpha).makePositive(1e-100);
326 *
327 */
328
329 Factor Qa_new = OR(alpha);
330 foreach( const Neighbor &gamma, nbOR(alpha) )
331 Qa_new *= muba(alpha,gamma.iter);
332 Qa_new ^= (1.0 / OR(alpha).c());
333 Qa_new.normalize();
334 if( Qa_new.hasNaNs() ) {
335 cerr << Name << "::doGBP: Qa_new has NaNs!" << endl;
336 return 1.0;
337 }
338 /* TODO: WHAT IS THE PURPOSE OF THE FOLLOWING CODE?
339 *
340 * _Qb[beta] = Qb_new.makeZero(1e-100);
341 */
342
343 if( props.doubleloop || props.damping == 0.0 )
344 _Qa[alpha] = Qa_new; // no damping for double loop
345 else
346 // FIXME: GEOMETRIC DAMPING IS SLOW!
347 _Qa[alpha] = (Qa_new^(1.0 - props.damping)) * (_Qa[alpha]^props.damping);
348 }
349 }
350
351 // Calculate new single variable beliefs and compare with old ones
352 maxDiff = -INFINITY;
353 for( size_t i = 0; i < nrVars(); ++i ) {
354 Factor b = beliefV(i);
355 maxDiff = std::max( maxDiff, dist( b, oldBeliefsV[i], Prob::DISTLINF ) );
356 oldBeliefsV[i] = b;
357 }
358 for( size_t I = 0; I < nrFactors(); ++I ) {
359 Factor b = beliefF(I);
360 maxDiff = std::max( maxDiff, dist( b, oldBeliefsF[I], Prob::DISTLINF ) );
361 oldBeliefsF[I] = b;
362 }
363
364 if( props.verbose >= 3 )
365 cerr << Name << "::doGBP: maxdiff " << maxDiff << " after " << _iters+1 << " passes" << endl;
366 }
367
368 if( maxDiff > _maxdiff )
369 _maxdiff = maxDiff;
370
371 if( props.verbose >= 1 ) {
372 if( maxDiff > props.tol ) {
373 if( props.verbose == 1 )
374 cerr << endl;
375 cerr << Name << "::doGBP: WARNING: not converged within " << props.maxiter << " passes (" << toc() - tic << " seconds)...final maxdiff:" << maxDiff << endl;
376 } else {
377 if( props.verbose >= 2 )
378 cerr << Name << "::doGBP: ";
379 cerr << "converged in " << _iters << " passes (" << toc() - tic << " seconds)." << endl;
380 }
381 }
382
383 return maxDiff;
384 }
385
386
387 Real HAK::doDoubleLoop() {
388 if( props.verbose >= 1 )
389 cerr << "Starting " << identify() << "...";
390 if( props.verbose >= 3)
391 cerr << endl;
392
393 double tic = toc();
394
395 // Save original outer regions
396 vector<FRegion> org_ORs = ORs;
397
398 // Save original inner counting numbers and set negative counting numbers to zero
399 vector<Real> org_IR_cs( nrIRs(), 0.0 );
400 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
401 org_IR_cs[beta] = IR(beta).c();
402 if( IR(beta).c() < 0.0 )
403 IR(beta).c() = 0.0;
404 }
405
406 // Keep old beliefs to check convergence
407 vector<Factor> oldBeliefsV;
408 oldBeliefsV.reserve( nrVars() );
409 for( size_t i = 0; i < nrVars(); i++ )
410 oldBeliefsV.push_back( beliefV(i) );
411 vector<Factor> oldBeliefsF;
412 oldBeliefsF.reserve( nrFactors() );
413 for( size_t I = 0; I < nrFactors(); I++ )
414 oldBeliefsF.push_back( beliefF(I) );
415
416 size_t outer_maxiter = props.maxiter;
417 Real outer_tol = props.tol;
418 size_t outer_verbose = props.verbose;
419 Real org_maxdiff = _maxdiff;
420
421 // Set parameters for inner loop
422 props.maxiter = 5;
423 props.verbose = outer_verbose ? outer_verbose - 1 : 0;
424
425 size_t outer_iter = 0;
426 size_t total_iter = 0;
427 Real maxDiff = INFINITY;
428 for( outer_iter = 0; outer_iter < outer_maxiter && maxDiff > outer_tol; outer_iter++ ) {
429 // Calculate new outer regions
430 for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
431 OR(alpha) = org_ORs[alpha];
432 foreach( const Neighbor &beta, nbOR(alpha) )
433 OR(alpha) *= _Qb[beta] ^ ((IR(beta).c() - org_IR_cs[beta]) / nbIR(beta).size());
434 }
435
436 // Inner loop
437 if( isnan( doGBP() ) )
438 return 1.0;
439
440 // Calculate new single variable beliefs and compare with old ones
441 maxDiff = -INFINITY;
442 for( size_t i = 0; i < nrVars(); ++i ) {
443 Factor b = beliefV(i);
444 maxDiff = std::max( maxDiff, dist( b, oldBeliefsV[i], Prob::DISTLINF ) );
445 oldBeliefsV[i] = b;
446 }
447 for( size_t I = 0; I < nrFactors(); ++I ) {
448 Factor b = beliefF(I);
449 maxDiff = std::max( maxDiff, dist( b, oldBeliefsF[I], Prob::DISTLINF ) );
450 oldBeliefsF[I] = b;
451 }
452
453 total_iter += Iterations();
454
455 if( props.verbose >= 3 )
456 cerr << Name << "::doDoubleLoop: maxdiff " << maxDiff << " after " << total_iter << " passes" << endl;
457 }
458
459 // restore _maxiter, _verbose and _maxdiff
460 props.maxiter = outer_maxiter;
461 props.verbose = outer_verbose;
462 _maxdiff = org_maxdiff;
463
464 _iters = total_iter;
465 if( maxDiff > _maxdiff )
466 _maxdiff = maxDiff;
467
468 // Restore original outer regions
469 ORs = org_ORs;
470
471 // Restore original inner counting numbers
472 for( size_t beta = 0; beta < nrIRs(); ++beta )
473 IR(beta).c() = org_IR_cs[beta];
474
475 if( props.verbose >= 1 ) {
476 if( maxDiff > props.tol ) {
477 if( props.verbose == 1 )
478 cerr << endl;
479 cerr << Name << "::doDoubleLoop: WARNING: not converged within " << outer_maxiter << " passes (" << toc() - tic << " seconds)...final maxdiff:" << maxDiff << endl;
480 } else {
481 if( props.verbose >= 3 )
482 cerr << Name << "::doDoubleLoop: ";
483 cerr << "converged in " << total_iter << " passes (" << toc() - tic << " seconds)." << endl;
484 }
485 }
486
487 return maxDiff;
488 }
489
490
491 Real HAK::run() {
492 if( props.doubleloop )
493 return doDoubleLoop();
494 else
495 return doGBP();
496 }
497
498
499 Factor HAK::belief( const VarSet &ns ) const {
500 vector<Factor>::const_iterator beta;
501 for( beta = _Qb.begin(); beta != _Qb.end(); beta++ )
502 if( beta->vars() >> ns )
503 break;
504 if( beta != _Qb.end() )
505 return( beta->marginal(ns) );
506 else {
507 vector<Factor>::const_iterator alpha;
508 for( alpha = _Qa.begin(); alpha != _Qa.end(); alpha++ )
509 if( alpha->vars() >> ns )
510 break;
511 if( alpha == _Qa.end() )
512 DAI_THROW(BELIEF_NOT_AVAILABLE);
513 return( alpha->marginal(ns) );
514 }
515 }
516
517
518 vector<Factor> HAK::beliefs() const {
519 vector<Factor> result;
520 for( size_t beta = 0; beta < nrIRs(); beta++ )
521 result.push_back( Qb(beta) );
522 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
523 result.push_back( Qa(alpha) );
524 return result;
525 }
526
527
528 Real HAK::logZ() const {
529 Real s = 0.0;
530 for( size_t beta = 0; beta < nrIRs(); beta++ )
531 s += IR(beta).c() * Qb(beta).entropy();
532 for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
533 s += OR(alpha).c() * Qa(alpha).entropy();
534 s += (OR(alpha).log(true) * Qa(alpha)).sum();
535 }
536 return s;
537 }
538
539
540 } // end of namespace dai