Improved ClusterGraph implementation and MaxSpanningTreePrims implementation.
[libdai.git] / src / regiongraph.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #include <algorithm>
23 #include <cmath>
24 #include <dai/regiongraph.h>
25 #include <dai/factorgraph.h>
26 #include <dai/clustergraph.h>
27
28
29 namespace dai {
30
31
32 using namespace std;
33
34
35 RegionGraph::RegionGraph( const FactorGraph &fg, const std::vector<Region> &ors, const std::vector<Region> &irs, const std::vector<std::pair<size_t,size_t> > &edges) : FactorGraph(fg), G(), ORs(), IRs(irs), fac2OR() {
36 // Copy outer regions (giving them counting number 1.0)
37 ORs.reserve( ors.size() );
38 for( vector<Region>::const_iterator alpha = ors.begin(); alpha != ors.end(); alpha++ )
39 ORs.push_back( FRegion(Factor(*alpha, 1.0), 1.0) );
40
41 // For each factor, find an outer regions that subsumes that factor.
42 // Then, multiply the outer region with that factor.
43 fac2OR.reserve( nrFactors() );
44 for( size_t I = 0; I < nrFactors(); I++ ) {
45 size_t alpha;
46 for( alpha = 0; alpha < nrORs(); alpha++ )
47 if( OR(alpha).vars() >> factor(I).vars() ) {
48 fac2OR.push_back( alpha );
49 // OR(alpha) *= factor(I);
50 break;
51 }
52 assert( alpha != nrORs() );
53 }
54 RecomputeORs();
55
56 // create bipartite graph
57 G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
58
59 // Check counting numbers
60 #ifdef DAI_DEBUG
61 Check_Counting_Numbers();
62 #endif
63 }
64
65
66 // CVM style
67 RegionGraph::RegionGraph( const FactorGraph &fg, const std::vector<VarSet> &cl ) : FactorGraph(fg), G(), ORs(), IRs(), fac2OR() {
68 // Retain only maximal clusters
69 ClusterGraph cg( cl );
70 cg.eraseNonMaximal();
71
72 // Create outer regions, giving them counting number 1.0
73 ORs.reserve( cg.size() );
74 foreach( const VarSet &ns, cg.clusters )
75 ORs.push_back( FRegion(Factor(ns, 1.0), 1.0) );
76
77 // For each factor, find an outer regions that subsumes that factor.
78 // Then, multiply the outer region with that factor.
79 fac2OR.reserve( nrFactors() );
80 for( size_t I = 0; I < nrFactors(); I++ ) {
81 size_t alpha;
82 for( alpha = 0; alpha < nrORs(); alpha++ )
83 if( OR(alpha).vars() >> factor(I).vars() ) {
84 fac2OR.push_back( alpha );
85 // OR(alpha) *= factor(I);
86 break;
87 }
88 assert( alpha != nrORs() );
89 }
90 RecomputeORs();
91
92 // Create inner regions - first pass
93 set<VarSet> betas;
94 for( size_t alpha = 0; alpha < cg.clusters.size(); alpha++ )
95 for( size_t alpha2 = alpha; (++alpha2) != cg.clusters.size(); ) {
96 VarSet intersect = cg.clusters[alpha] & cg.clusters[alpha2];
97 if( intersect.size() > 0 )
98 betas.insert( intersect );
99 }
100
101 // Create inner regions - subsequent passes
102 set<VarSet> new_betas;
103 do {
104 new_betas.clear();
105 for( set<VarSet>::const_iterator gamma = betas.begin(); gamma != betas.end(); gamma++ )
106 for( set<VarSet>::const_iterator gamma2 = gamma; (++gamma2) != betas.end(); ) {
107 VarSet intersect = (*gamma) & (*gamma2);
108 if( (intersect.size() > 0) && (betas.count(intersect) == 0) )
109 new_betas.insert( intersect );
110 }
111 betas.insert(new_betas.begin(), new_betas.end());
112 } while( new_betas.size() );
113
114 // Create inner regions - store them in the bipartite graph
115 IRs.reserve( betas.size() );
116 for( set<VarSet>::const_iterator beta = betas.begin(); beta != betas.end(); beta++ )
117 IRs.push_back( Region(*beta,NAN) );
118
119 // Create edges
120 vector<pair<size_t,size_t> > edges;
121 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
122 for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
123 if( OR(alpha).vars() >> IR(beta) )
124 edges.push_back( pair<size_t,size_t>(alpha,beta) );
125 }
126 }
127
128 // create bipartite graph
129 G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
130
131 // Calculate counting numbers
132 Calc_Counting_Numbers();
133
134 // Check counting numbers
135 #ifdef DAI_DEBUG
136 Check_Counting_Numbers();
137 #endif
138 }
139
140
141 void RegionGraph::Calc_Counting_Numbers() {
142 // Calculates counting numbers of inner regions based upon counting numbers of outer regions
143
144 vector<vector<size_t> > ancestors(nrIRs());
145 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
146 IR(beta).c() = NAN;
147 for( size_t beta2 = 0; beta2 < nrIRs(); beta2++ )
148 if( (beta2 != beta) && IR(beta2) >> IR(beta) )
149 ancestors[beta].push_back(beta2);
150 }
151
152 bool new_counting;
153 do {
154 new_counting = false;
155 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
156 if( isnan( IR(beta).c() ) ) {
157 bool has_nan_ancestor = false;
158 for( vector<size_t>::const_iterator beta2 = ancestors[beta].begin(); (beta2 != ancestors[beta].end()) && !has_nan_ancestor; beta2++ )
159 if( isnan( IR(*beta2).c() ) )
160 has_nan_ancestor = true;
161 if( !has_nan_ancestor ) {
162 double c = 1.0;
163 foreach( const Neighbor &alpha, nbIR(beta) )
164 c -= OR(alpha).c();
165 for( vector<size_t>::const_iterator beta2 = ancestors[beta].begin(); beta2 != ancestors[beta].end(); beta2++ )
166 c -= IR(*beta2).c();
167 IR(beta).c() = c;
168 new_counting = true;
169 }
170 }
171 }
172 } while( new_counting );
173 }
174
175
176 bool RegionGraph::Check_Counting_Numbers() {
177 // Checks whether the counting numbers satisfy the fundamental relation
178
179 bool all_valid = true;
180 for( vector<Var>::const_iterator n = vars.begin(); n != vars.end(); n++ ) {
181 double c_n = 0.0;
182 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
183 if( OR(alpha).vars() && *n )
184 c_n += OR(alpha).c();
185 for( size_t beta = 0; beta < nrIRs(); beta++ )
186 if( IR(beta) && *n )
187 c_n += IR(beta).c();
188 if( fabs(c_n - 1.0) > 1e-15 ) {
189 all_valid = false;
190 cout << "WARNING: counting numbers do not satisfy relation for " << *n << "(c_n = " << c_n << ")." << endl;
191 }
192 }
193
194 return all_valid;
195 }
196
197
198 void RegionGraph::RecomputeORs() {
199 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
200 OR(alpha).fill( 1.0 );
201 for( size_t I = 0; I < nrFactors(); I++ )
202 if( fac2OR[I] != -1U )
203 OR( fac2OR[I] ) *= factor( I );
204 }
205
206
207 void RegionGraph::RecomputeORs( const VarSet &ns ) {
208 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
209 if( OR(alpha).vars() && ns )
210 OR(alpha).fill( 1.0 );
211 for( size_t I = 0; I < nrFactors(); I++ )
212 if( fac2OR[I] != -1U )
213 if( OR( fac2OR[I] ).vars() && ns )
214 OR( fac2OR[I] ) *= factor( I );
215 }
216
217
218 void RegionGraph::RecomputeOR( size_t I ) {
219 assert( I < nrFactors() );
220 if( fac2OR[I] != -1U ) {
221 size_t alpha = fac2OR[I];
222 OR(alpha).fill( 1.0 );
223 for( size_t J = 0; J < nrFactors(); J++ )
224 if( fac2OR[J] == alpha )
225 OR(alpha) *= factor( J );
226 }
227 }
228
229
230 ostream & operator << (ostream & os, const RegionGraph & rg) {
231 os << "Outer regions" << endl;
232 for( size_t alpha = 0; alpha < rg.nrORs(); alpha++ )
233 os << rg.OR(alpha).vars() << ": c = " << rg.OR(alpha).c() << endl;
234
235 os << "Inner regions" << endl;
236 for( size_t beta = 0; beta < rg.nrIRs(); beta++ )
237 os << (VarSet)rg.IR(beta) << ": c = " << rg.IR(beta).c() << endl;
238
239 return(os);
240 }
241
242
243 } // end of namespace dai