1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
14 #include <dai/jtree.h>
23 const char *JTree::Name
= "JTREE";
26 void JTree::setProperties( const PropertySet
&opts
) {
27 DAI_ASSERT( opts
.hasKey("verbose") );
28 DAI_ASSERT( opts
.hasKey("updates") );
30 props
.verbose
= opts
.getStringAs
<size_t>("verbose");
31 props
.updates
= opts
.getStringAs
<Properties::UpdateType
>("updates");
32 if( opts
.hasKey("inference") )
33 props
.inference
= opts
.getStringAs
<Properties::InfType
>("inference");
35 props
.inference
= Properties::InfType::SUMPROD
;
39 PropertySet
JTree::getProperties() const {
41 opts
.Set( "verbose", props
.verbose
);
42 opts
.Set( "updates", props
.updates
);
43 opts
.Set( "inference", props
.inference
);
48 string
JTree::printProperties() const {
49 stringstream
s( stringstream::out
);
51 s
<< "verbose=" << props
.verbose
<< ",";
52 s
<< "updates=" << props
.updates
<< ",";
53 s
<< "inference=" << props
.inference
<< "]";
58 JTree::JTree( const FactorGraph
&fg
, const PropertySet
&opts
, bool automatic
) : DAIAlgRG(fg
), _mes(), _logZ(), RTree(), Qa(), Qb(), props() {
59 setProperties( opts
);
62 DAI_THROW(FACTORGRAPH_NOT_CONNECTED
);
65 // Create ClusterGraph which contains factors as clusters
67 cl
.reserve( fg
.nrFactors() );
68 for( size_t I
= 0; I
< nrFactors(); I
++ )
69 cl
.push_back( factor(I
).vars() );
70 ClusterGraph
_cg( cl
);
72 if( props
.verbose
>= 3 )
73 cerr
<< "Initial clusters: " << _cg
<< endl
;
75 // Retain only maximal clusters
76 _cg
.eraseNonMaximal();
77 if( props
.verbose
>= 3 )
78 cerr
<< "Maximal clusters: " << _cg
<< endl
;
80 // Use MinFill heuristic to guess optimal elimination sequence
81 vector
<VarSet
> ElimVec
= _cg
.VarElim( eliminationChoice_MinFill
).eraseNonMaximal().toVector();
82 if( props
.verbose
>= 3 )
83 cerr
<< "VarElim result: " << ElimVec
<< endl
;
85 // Generate the junction tree corresponding to the elimination sequence
86 GenerateJT( ElimVec
);
91 void JTree::construct( const std::vector
<VarSet
> &cl
, bool verify
) {
92 // Construct a weighted graph (each edge is weighted with the cardinality
93 // of the intersection of the nodes, where the nodes are the elements of cl).
94 WeightedGraph
<int> JuncGraph
;
95 for( size_t i
= 0; i
< cl
.size(); i
++ )
96 for( size_t j
= i
+1; j
< cl
.size(); j
++ ) {
97 size_t w
= (cl
[i
] & cl
[j
]).size();
99 JuncGraph
[UEdge(i
,j
)] = w
;
102 // Construct maximal spanning tree using Prim's algorithm
103 RTree
= MaxSpanningTreePrims( JuncGraph
);
105 // Construct corresponding region graph
107 // Create outer regions
109 ORs
.reserve( cl
.size() );
110 for( size_t i
= 0; i
< cl
.size(); i
++ )
111 ORs
.push_back( FRegion( Factor(cl
[i
], 1.0), 1.0 ) );
113 // For each factor, find an outer region that subsumes that factor.
114 // Then, multiply the outer region with that factor.
116 fac2OR
.resize( nrFactors(), -1U );
117 for( size_t I
= 0; I
< nrFactors(); I
++ ) {
119 for( alpha
= 0; alpha
< nrORs(); alpha
++ )
120 if( OR(alpha
).vars() >> factor(I
).vars() ) {
125 DAI_ASSERT( alpha
!= nrORs() );
129 // Create inner regions and edges
131 IRs
.reserve( RTree
.size() );
133 edges
.reserve( 2 * RTree
.size() );
134 for( size_t i
= 0; i
< RTree
.size(); i
++ ) {
135 edges
.push_back( Edge( RTree
[i
].n1
, nrIRs() ) );
136 edges
.push_back( Edge( RTree
[i
].n2
, nrIRs() ) );
137 // inner clusters have counting number -1
138 IRs
.push_back( Region( cl
[RTree
[i
].n1
] & cl
[RTree
[i
].n2
], -1.0 ) );
141 // create bipartite graph
142 G
.construct( nrORs(), nrIRs(), edges
.begin(), edges
.end() );
144 // Check counting numbers
146 checkCountingNumbers();
151 Qa
.reserve( nrORs() );
152 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ )
153 Qa
.push_back( OR(alpha
) );
156 Qb
.reserve( nrIRs() );
157 for( size_t beta
= 0; beta
< nrIRs(); beta
++ )
158 Qb
.push_back( Factor( IR(beta
), 1.0 ) );
162 void JTree::GenerateJT( const std::vector
<VarSet
> &cl
) {
163 construct( cl
, true );
167 _mes
.reserve( nrORs() );
168 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ ) {
169 _mes
.push_back( vector
<Factor
>() );
170 _mes
[alpha
].reserve( nbOR(alpha
).size() );
171 foreach( const Neighbor
&beta
, nbOR(alpha
) )
172 _mes
[alpha
].push_back( Factor( IR(beta
), 1.0 ) );
175 if( props
.verbose
>= 3 )
176 cerr
<< "Regiongraph generated by JTree::GenerateJT: " << *this << endl
;
180 string
JTree::identify() const {
181 return string(Name
) + printProperties();
185 Factor
JTree::belief( const VarSet
&vs
) const {
186 vector
<Factor
>::const_iterator beta
;
187 for( beta
= Qb
.begin(); beta
!= Qb
.end(); beta
++ )
188 if( beta
->vars() >> vs
)
190 if( beta
!= Qb
.end() )
191 return( beta
->marginal(vs
) );
193 vector
<Factor
>::const_iterator alpha
;
194 for( alpha
= Qa
.begin(); alpha
!= Qa
.end(); alpha
++ )
195 if( alpha
->vars() >> vs
)
197 if( alpha
== Qa
.end() ) {
198 DAI_THROW(BELIEF_NOT_AVAILABLE
);
201 return( alpha
->marginal(vs
) );
206 vector
<Factor
> JTree::beliefs() const {
207 vector
<Factor
> result
;
208 for( size_t beta
= 0; beta
< nrIRs(); beta
++ )
209 result
.push_back( Qb
[beta
] );
210 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ )
211 result
.push_back( Qa
[alpha
] );
216 void JTree::runHUGIN() {
217 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ )
218 Qa
[alpha
] = OR(alpha
);
220 for( size_t beta
= 0; beta
< nrIRs(); beta
++ )
221 Qb
[beta
].fill( 1.0 );
225 for( size_t i
= RTree
.size(); (i
--) != 0; ) {
226 // Make outer region RTree[i].n1 consistent with outer region RTree[i].n2
227 // IR(i) = seperator OR(RTree[i].n1) && OR(RTree[i].n2)
229 if( props
.inference
== Properties::InfType::SUMPROD
)
230 new_Qb
= Qa
[RTree
[i
].n2
].marginal( IR( i
), false );
232 new_Qb
= Qa
[RTree
[i
].n2
].maxMarginal( IR( i
), false );
234 _logZ
+= log(new_Qb
.normalize());
235 Qa
[RTree
[i
].n1
] *= new_Qb
/ Qb
[i
];
239 _logZ
+= log(Qa
[0].normalize() );
241 _logZ
+= log(Qa
[RTree
[0].n1
].normalize());
243 // DistributeEvidence
244 for( size_t i
= 0; i
< RTree
.size(); i
++ ) {
245 // Make outer region RTree[i].n2 consistent with outer region RTree[i].n1
246 // IR(i) = seperator OR(RTree[i].n1) && OR(RTree[i].n2)
248 if( props
.inference
== Properties::InfType::SUMPROD
)
249 new_Qb
= Qa
[RTree
[i
].n1
].marginal( IR( i
) );
251 new_Qb
= Qa
[RTree
[i
].n1
].maxMarginal( IR( i
) );
253 Qa
[RTree
[i
].n2
] *= new_Qb
/ Qb
[i
];
258 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ )
259 Qa
[alpha
].normalize();
263 void JTree::runShaferShenoy() {
266 for( size_t e
= nrIRs(); (e
--) != 0; ) {
267 // send a message from RTree[e].n2 to RTree[e].n1
268 // or, actually, from the seperator IR(e) to RTree[e].n1
270 size_t i
= nbIR(e
)[1].node
; // = RTree[e].n2
271 size_t j
= nbIR(e
)[0].node
; // = RTree[e].n1
272 size_t _e
= nbIR(e
)[0].dual
;
275 foreach( const Neighbor
&k
, nbOR(i
) )
277 msg
*= message( i
, k
.iter
);
278 if( props
.inference
== Properties::InfType::SUMPROD
)
279 message( j
, _e
) = msg
.marginal( IR(e
), false );
281 message( j
, _e
) = msg
.maxMarginal( IR(e
), false );
282 _logZ
+= log( message(j
,_e
).normalize() );
286 for( size_t e
= 0; e
< nrIRs(); e
++ ) {
287 size_t i
= nbIR(e
)[0].node
; // = RTree[e].n1
288 size_t j
= nbIR(e
)[1].node
; // = RTree[e].n2
289 size_t _e
= nbIR(e
)[1].dual
;
292 foreach( const Neighbor
&k
, nbOR(i
) )
294 msg
*= message( i
, k
.iter
);
295 if( props
.inference
== Properties::InfType::SUMPROD
)
296 message( j
, _e
) = msg
.marginal( IR(e
) );
298 message( j
, _e
) = msg
.maxMarginal( IR(e
) );
302 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ ) {
303 Factor piet
= OR(alpha
);
304 foreach( const Neighbor
&k
, nbOR(alpha
) )
305 piet
*= message( alpha
, k
.iter
);
307 _logZ
+= log( piet
.normalize() );
309 } else if( alpha
== nbIR(0)[0].node
/*RTree[0].n1*/ ) {
310 _logZ
+= log( piet
.normalize() );
313 Qa
[alpha
] = piet
.normalized();
316 // Only for logZ (and for belief)...
317 for( size_t beta
= 0; beta
< nrIRs(); beta
++ ) {
318 if( props
.inference
== Properties::InfType::SUMPROD
)
319 Qb
[beta
] = Qa
[nbIR(beta
)[0].node
].marginal( IR(beta
) );
321 Qb
[beta
] = Qa
[nbIR(beta
)[0].node
].maxMarginal( IR(beta
) );
327 if( props
.updates
== Properties::UpdateType::HUGIN
)
329 else if( props
.updates
== Properties::UpdateType::SHSH
)
335 Real
JTree::logZ() const {
337 for( size_t beta
= 0; beta
< nrIRs(); beta
++ )
338 s
+= IR(beta
).c() * Qb
[beta
].entropy();
339 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ ) {
340 s
+= OR(alpha
).c() * Qa
[alpha
].entropy();
341 s
+= (OR(alpha
).log(true) * Qa
[alpha
]).sum();
347 size_t JTree::findEfficientTree( const VarSet
& vs
, RootedTree
&Tree
, size_t PreviousRoot
) const {
348 // find new root clique (the one with maximal statespace overlap with vs)
349 size_t maxval
= 0, maxalpha
= 0;
350 for( size_t alpha
= 0; alpha
< nrORs(); alpha
++ ) {
351 size_t val
= VarSet(vs
& OR(alpha
).vars()).nrStates();
358 // reorder the tree edges such that maxalpha becomes the new root
359 RootedTree
newTree( Graph( RTree
.begin(), RTree
.end() ), maxalpha
);
361 // identify subtree that contains all variables of vs which are not in the new root
362 VarSet vsrem
= vs
/ OR(maxalpha
).vars();
364 // for each variable in vs that is not in the root clique
365 for( VarSet::const_iterator n
= vsrem
.begin(); n
!= vsrem
.end(); n
++ ) {
366 // find first occurence of *n in the tree, which is closest to the root
368 for( ; e
!= newTree
.size(); e
++ ) {
369 if( OR(newTree
[e
].n2
).vars().contains( *n
) )
372 DAI_ASSERT( e
!= newTree
.size() );
374 // track-back path to root and add edges to subTree
375 subTree
.insert( newTree
[e
] );
376 size_t pos
= newTree
[e
].n1
;
378 if( newTree
[e
-1].n2
== pos
) {
379 subTree
.insert( newTree
[e
-1] );
380 pos
= newTree
[e
-1].n1
;
383 if( PreviousRoot
!= (size_t)-1 && PreviousRoot
!= maxalpha
) {
384 // find first occurence of PreviousRoot in the tree, which is closest to the new root
386 for( ; e
!= newTree
.size(); e
++ ) {
387 if( newTree
[e
].n2
== PreviousRoot
)
390 DAI_ASSERT( e
!= newTree
.size() );
392 // track-back path to root and add edges to subTree
393 subTree
.insert( newTree
[e
] );
394 size_t pos
= newTree
[e
].n1
;
396 if( newTree
[e
-1].n2
== pos
) {
397 subTree
.insert( newTree
[e
-1] );
398 pos
= newTree
[e
-1].n1
;
402 // Resulting Tree is a reordered copy of newTree
403 // First add edges in subTree to Tree
405 vector
<DEdge
> remTree
;
406 for( RootedTree::const_iterator e
= newTree
.begin(); e
!= newTree
.end(); e
++ )
407 if( subTree
.count( *e
) )
408 Tree
.push_back( *e
);
410 remTree
.push_back( *e
);
411 size_t subTreeSize
= Tree
.size();
412 // Then add remaining edges
413 copy( remTree
.begin(), remTree
.end(), back_inserter( Tree
) );
419 Factor
JTree::calcMarginal( const VarSet
& vs
) {
420 vector
<Factor
>::const_iterator beta
;
421 for( beta
= Qb
.begin(); beta
!= Qb
.end(); beta
++ )
422 if( beta
->vars() >> vs
)
424 if( beta
!= Qb
.end() )
425 return( beta
->marginal(vs
) );
427 vector
<Factor
>::const_iterator alpha
;
428 for( alpha
= Qa
.begin(); alpha
!= Qa
.end(); alpha
++ )
429 if( alpha
->vars() >> vs
)
431 if( alpha
!= Qa
.end() )
432 return( alpha
->marginal(vs
) );
434 // Find subtree to do efficient inference
436 size_t Tsize
= findEfficientTree( vs
, T
);
438 // Find remaining variables (which are not in the new root)
439 VarSet vsrem
= vs
/ OR(T
.front().n1
).vars();
440 Factor
Pvs (vs
, 0.0);
442 // Save Qa and Qb on the subtree
443 map
<size_t,Factor
> Qa_old
;
444 map
<size_t,Factor
> Qb_old
;
445 vector
<size_t> b(Tsize
, 0);
446 for( size_t i
= Tsize
; (i
--) != 0; ) {
447 size_t alpha1
= T
[i
].n1
;
448 size_t alpha2
= T
[i
].n2
;
450 for( beta
= 0; beta
< nrIRs(); beta
++ )
451 if( UEdge( RTree
[beta
].n1
, RTree
[beta
].n2
) == UEdge( alpha1
, alpha2
) )
453 DAI_ASSERT( beta
!= nrIRs() );
456 if( !Qa_old
.count( alpha1
) )
457 Qa_old
[alpha1
] = Qa
[alpha1
];
458 if( !Qa_old
.count( alpha2
) )
459 Qa_old
[alpha2
] = Qa
[alpha2
];
460 if( !Qb_old
.count( beta
) )
461 Qb_old
[beta
] = Qb
[beta
];
464 // For all states of vsrem
465 for( State
s(vsrem
); s
.valid(); s
++ ) {
468 for( size_t i
= Tsize
; (i
--) != 0; ) {
469 // Make outer region T[i].n1 consistent with outer region T[i].n2
470 // IR(i) = seperator OR(T[i].n1) && OR(T[i].n2)
472 for( VarSet::const_iterator n
= vsrem
.begin(); n
!= vsrem
.end(); n
++ )
473 if( Qa
[T
[i
].n2
].vars() >> *n
) {
474 Factor
piet( *n
, 0.0 );
479 Factor new_Qb
= Qa
[T
[i
].n2
].marginal( IR( b
[i
] ), false );
480 logZ
+= log(new_Qb
.normalize());
481 Qa
[T
[i
].n1
] *= new_Qb
/ Qb
[b
[i
]];
484 logZ
+= log(Qa
[T
[0].n1
].normalize());
486 Factor
piet( vsrem
, 0.0 );
488 Pvs
+= piet
* Qa
[T
[0].n1
].marginal( vs
/ vsrem
, false ); // OPTIMIZE ME
490 // Restore clamped beliefs
491 for( map
<size_t,Factor
>::const_iterator alpha
= Qa_old
.begin(); alpha
!= Qa_old
.end(); alpha
++ )
492 Qa
[alpha
->first
] = alpha
->second
;
493 for( map
<size_t,Factor
>::const_iterator beta
= Qb_old
.begin(); beta
!= Qb_old
.end(); beta
++ )
494 Qb
[beta
->first
] = beta
->second
;
497 return( Pvs
.normalized() );
503 std::pair
<size_t,size_t> boundTreewidth( const FactorGraph
& fg
) {
507 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ )
508 _cg
.insert( fg
.factor(I
).vars() );
510 // Retain only maximal clusters
511 _cg
.eraseNonMaximal();
513 // Obtain elimination sequence
514 vector
<VarSet
> ElimVec
= _cg
.VarElim( eliminationChoice_MinFill
).eraseNonMaximal().toVector();
516 // Calculate treewidth
517 size_t treewidth
= 0;
519 for( size_t i
= 0; i
< ElimVec
.size(); i
++ ) {
520 if( ElimVec
[i
].size() > treewidth
)
521 treewidth
= ElimVec
[i
].size();
522 size_t s
= ElimVec
[i
].nrStates();
527 return pair
<size_t,size_t>(treewidth
, nrstates
);
531 std::pair
<size_t,size_t> treewidth( const FactorGraph
& fg
)
533 return boundTreewidth( fg
);
537 std::vector
<size_t> JTree::findMaximum() const {
538 vector
<size_t> maximum( nrVars() );
539 vector
<bool> visitedVars( nrVars(), false );
540 vector
<bool> visitedFactors( nrFactors(), false );
541 stack
<size_t> scheduledFactors
;
542 for( size_t i
= 0; i
< nrVars(); ++i
) {
545 visitedVars
[i
] = true;
547 // Maximise with respect to variable i
548 Prob prod
= beliefV(i
).p();
549 maximum
[i
] = prod
.argmax().first
;
551 foreach( const Neighbor
&I
, nbV(i
) )
552 if( !visitedFactors
[I
] )
553 scheduledFactors
.push(I
);
555 while( !scheduledFactors
.empty() ){
556 size_t I
= scheduledFactors
.top();
557 scheduledFactors
.pop();
558 if( visitedFactors
[I
] )
560 visitedFactors
[I
] = true;
562 // Evaluate if some neighboring variables still need to be fixed; if not, we're done
563 bool allDetermined
= true;
564 foreach( const Neighbor
&j
, nbF(I
) )
565 if( !visitedVars
[j
.node
] ) {
566 allDetermined
= false;
572 // Calculate product of incoming messages on factor I
573 Prob prod2
= beliefF(I
).p();
575 // The allowed configuration is restrained according to the variables assigned so far:
576 // pick the argmax amongst the allowed states
577 Real maxProb
= numeric_limits
<Real
>::min();
578 State
maxState( factor(I
).vars() );
579 for( State
s( factor(I
).vars() ); s
.valid(); ++s
){
580 // First, calculate whether this state is consistent with variables that
581 // have been assigned already
582 bool allowedState
= true;
583 foreach( const Neighbor
&j
, nbF(I
) )
584 if( visitedVars
[j
.node
] && maximum
[j
.node
] != s(var(j
.node
)) ) {
585 allowedState
= false;
588 // If it is consistent, check if its probability is larger than what we have seen so far
589 if( allowedState
&& prod2
[s
] > maxProb
) {
596 foreach( const Neighbor
&j
, nbF(I
) ) {
597 if( visitedVars
[j
.node
] ) {
598 // We have already visited j earlier - hopefully our state is consistent
599 if( maximum
[j
.node
] != maxState(var(j
.node
)) && props
.verbose
>= 1 )
600 cerr
<< "JTree::findMaximum - warning: maximum not consistent due to loops." << endl
;
602 // We found a consistent state for variable j
603 visitedVars
[j
.node
] = true;
604 maximum
[j
.node
] = maxState( var(j
.node
) );
605 foreach( const Neighbor
&J
, nbV(j
) )
606 if( !visitedFactors
[J
] )
607 scheduledFactors
.push(J
);
616 } // end of namespace dai