1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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 */
22 #include <algorithm>
23 #include <cmath>
24 #include <dai/regiongraph.h>
25 #include <dai/factorgraph.h>
26 #include <dai/clustergraph.h>
29 namespace dai {
32 using namespace std;
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) );
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();
56 // create bipartite graph
57 G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
59 // Check counting numbers
60 #ifdef DAI_DEBUG
61 Check_Counting_Numbers();
62 #endif
63 }
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();
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) );
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();
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 }
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() );
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,0.0) );
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 }
128 // create bipartite graph
129 G.create( nrORs(), nrIRs(), edges.begin(), edges.end() );
131 // Calculate counting numbers
132 Calc_Counting_Numbers();
134 // Check counting numbers
135 #ifdef DAI_DEBUG
136 Check_Counting_Numbers();
137 #endif
138 }
141 void RegionGraph::Calc_Counting_Numbers() {
142 // Calculates counting numbers of inner regions based upon counting numbers of outer regions
144 vector<vector<size_t> > ancestors(nrIRs());
145 vector<bool> assigned(nrIRs(), false);
146 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
147 IR(beta).c() = 0.0;
148 for( size_t beta2 = 0; beta2 < nrIRs(); beta2++ )
149 if( (beta2 != beta) && IR(beta2) >> IR(beta) )
150 ancestors[beta].push_back(beta2);
151 }
153 bool new_counting;
154 do {
155 new_counting = false;
156 for( size_t beta = 0; beta < nrIRs(); beta++ ) {
157 if( !assigned[beta] ) {
158 bool has_unassigned_ancestor = false;
159 for( vector<size_t>::const_iterator beta2 = ancestors[beta].begin(); (beta2 != ancestors[beta].end()) && !has_unassigned_ancestor; beta2++ )
160 if( !assigned[*beta2] )
161 has_unassigned_ancestor = true;
162 if( !has_unassigned_ancestor ) {
163 double c = 1.0;
164 foreach( const Neighbor &alpha, nbIR(beta) )
165 c -= OR(alpha).c();
166 for( vector<size_t>::const_iterator beta2 = ancestors[beta].begin(); beta2 != ancestors[beta].end(); beta2++ )
167 c -= IR(*beta2).c();
168 IR(beta).c() = c;
169 assigned[beta] = true;
170 new_counting = true;
171 }
172 }
173 }
174 } while( new_counting );
175 }
178 bool RegionGraph::Check_Counting_Numbers() {
179 // Checks whether the counting numbers satisfy the fundamental relation
181 bool all_valid = true;
182 for( vector<Var>::const_iterator n = vars.begin(); n != vars.end(); n++ ) {
183 double c_n = 0.0;
184 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
185 if( OR(alpha).vars().contains( *n ) )
186 c_n += OR(alpha).c();
187 for( size_t beta = 0; beta < nrIRs(); beta++ )
188 if( IR(beta).contains( *n ) )
189 c_n += IR(beta).c();
190 if( fabs(c_n - 1.0) > 1e-15 ) {
191 all_valid = false;
192 cout << "WARNING: counting numbers do not satisfy relation for " << *n << "(c_n = " << c_n << ")." << endl;
193 }
194 }
196 return all_valid;
197 }
200 void RegionGraph::RecomputeORs() {
201 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
202 OR(alpha).fill( 1.0 );
203 for( size_t I = 0; I < nrFactors(); I++ )
204 if( fac2OR[I] != -1U )
205 OR( fac2OR[I] ) *= factor( I );
206 }
209 void RegionGraph::RecomputeORs( const VarSet &ns ) {
210 for( size_t alpha = 0; alpha < nrORs(); alpha++ )
211 if( OR(alpha).vars().intersects( ns ) )
212 OR(alpha).fill( 1.0 );
213 for( size_t I = 0; I < nrFactors(); I++ )
214 if( fac2OR[I] != -1U )
215 if( OR( fac2OR[I] ).vars().intersects( ns ) )
216 OR( fac2OR[I] ) *= factor( I );
217 }
220 void RegionGraph::RecomputeOR( size_t I ) {
221 assert( I < nrFactors() );
222 if( fac2OR[I] != -1U ) {
223 size_t alpha = fac2OR[I];
224 OR(alpha).fill( 1.0 );
225 for( size_t J = 0; J < nrFactors(); J++ )
226 if( fac2OR[J] == alpha )
227 OR(alpha) *= factor( J );
228 }
229 }
232 ostream & operator << (ostream & os, const RegionGraph & rg) {
233 os << "Outer regions" << endl;
234 for( size_t alpha = 0; alpha < rg.nrORs(); alpha++ )
235 os << rg.OR(alpha).vars() << ": c = " << rg.OR(alpha).c() << endl;
237 os << "Inner regions" << endl;
238 for( size_t beta = 0; beta < rg.nrIRs(); beta++ )
239 os << (VarSet)rg.IR(beta) << ": c = " << rg.IR(beta).c() << endl;
241 return(os);
242 }
245 } // end of namespace dai