497ca716ea8ed3c0087a7511f3e2c5533453e3d0
[libdai.git] / tests / unit / factorgraph.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) 2010 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #define BOOST_TEST_DYN_LINK
12
13
14 #include <dai/bipgraph.h>
15 #include <dai/factorgraph.h>
16 #include <vector>
17 #include <strstream>
18
19
20 using namespace dai;
21
22
23 const double tol = 1e-8;
24
25
26 #define BOOST_TEST_MODULE FactorGraphTest
27
28
29 #include <boost/test/unit_test.hpp>
30 #include <boost/test/floating_point_comparison.hpp>
31
32
33 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
34 FactorGraph G;
35 BOOST_CHECK_EQUAL( G.vars(), std::vector<Var>() );
36 BOOST_CHECK_EQUAL( G.factors(), std::vector<Factor>() );
37
38 std::vector<Factor> facs;
39 facs.push_back( Factor( VarSet( Var(0, 2), Var(1, 2) ) ) );
40 facs.push_back( Factor( VarSet( Var(0, 2), Var(2, 2) ) ) );
41 facs.push_back( Factor( VarSet( Var(1, 2), Var(2, 2) ) ) );
42 facs.push_back( Factor( VarSet( Var(1, 2) ) ) );
43 std::vector<Var> vars;
44 vars.push_back( Var( 0, 2 ) );
45 vars.push_back( Var( 1, 2 ) );
46 vars.push_back( Var( 2, 2 ) );
47
48 FactorGraph G1( facs );
49 BOOST_CHECK_EQUAL( G1.vars(), vars );
50 BOOST_CHECK_EQUAL( G1.factors(), facs );
51
52 FactorGraph G2( facs.begin(), facs.end(), vars.begin(), vars.end(), facs.size(), vars.size() );
53 BOOST_CHECK_EQUAL( G2.vars(), vars );
54 BOOST_CHECK_EQUAL( G2.factors(), facs );
55
56 FactorGraph *G3 = G2.clone();
57 BOOST_CHECK_EQUAL( G3->vars(), vars );
58 BOOST_CHECK_EQUAL( G3->factors(), facs );
59 delete G3;
60
61 FactorGraph G4 = G2;
62 BOOST_CHECK_EQUAL( G4.vars(), vars );
63 BOOST_CHECK_EQUAL( G4.factors(), facs );
64
65 FactorGraph G5( G2 );
66 BOOST_CHECK_EQUAL( G5.vars(), vars );
67 BOOST_CHECK_EQUAL( G5.factors(), facs );
68 }
69
70
71 BOOST_AUTO_TEST_CASE( AccMutIterTest ) {
72 std::vector<Factor> facs;
73 facs.push_back( Factor( VarSet( Var(0, 2), Var(1, 2) ) ) );
74 facs.push_back( Factor( VarSet( Var(0, 2), Var(2, 2) ) ) );
75 facs.push_back( Factor( VarSet( Var(1, 2), Var(2, 2) ) ) );
76 facs.push_back( Factor( VarSet( Var(1, 2) ) ) );
77 std::vector<Var> vars;
78 vars.push_back( Var( 0, 2 ) );
79 vars.push_back( Var( 1, 2 ) );
80 vars.push_back( Var( 2, 2 ) );
81
82 FactorGraph G( facs );
83 BOOST_CHECK_EQUAL( G.var(0), Var(0, 2) );
84 BOOST_CHECK_EQUAL( G.var(1), Var(1, 2) );
85 BOOST_CHECK_EQUAL( G.var(2), Var(2, 2) );
86 BOOST_CHECK_EQUAL( G.vars(), vars );
87 BOOST_CHECK_EQUAL( G.factor(0), facs[0] );
88 BOOST_CHECK_EQUAL( G.factor(1), facs[1] );
89 BOOST_CHECK_EQUAL( G.factor(2), facs[2] );
90 BOOST_CHECK_EQUAL( G.factor(3), facs[3] );
91 BOOST_CHECK_EQUAL( G.factors(), facs );
92 BOOST_CHECK_EQUAL( G.nbV(0).size(), 2 );
93 BOOST_CHECK_EQUAL( G.nbV(0,0), 0 );
94 BOOST_CHECK_EQUAL( G.nbV(0,1), 1 );
95 BOOST_CHECK_EQUAL( G.nbV(1).size(), 3 );
96 BOOST_CHECK_EQUAL( G.nbV(1,0), 0 );
97 BOOST_CHECK_EQUAL( G.nbV(1,1), 2 );
98 BOOST_CHECK_EQUAL( G.nbV(1,2), 3 );
99 BOOST_CHECK_EQUAL( G.nbV(0).size(), 2 );
100 BOOST_CHECK_EQUAL( G.nbV(2,0), 1 );
101 BOOST_CHECK_EQUAL( G.nbV(2,1), 2 );
102 BOOST_CHECK_EQUAL( G.nbF(0).size(), 2 );
103 BOOST_CHECK_EQUAL( G.nbF(0,0), 0 );
104 BOOST_CHECK_EQUAL( G.nbF(0,1), 1 );
105 BOOST_CHECK_EQUAL( G.nbF(1).size(), 2 );
106 BOOST_CHECK_EQUAL( G.nbF(1,0), 0 );
107 BOOST_CHECK_EQUAL( G.nbF(1,1), 2 );
108 BOOST_CHECK_EQUAL( G.nbF(2).size(), 2 );
109 BOOST_CHECK_EQUAL( G.nbF(2,0), 1 );
110 BOOST_CHECK_EQUAL( G.nbF(2,1), 2 );
111 BOOST_CHECK_EQUAL( G.nbF(3).size(), 1 );
112 BOOST_CHECK_EQUAL( G.nbF(3,0), 1 );
113
114 FactorGraph::const_iterator cit = G.begin();
115 FactorGraph::iterator it = G.begin();
116 for( size_t I = 0; I < G.nrFactors(); I++, cit++, it++ ) {
117 BOOST_CHECK_EQUAL( *cit, G.factor(I) );
118 BOOST_CHECK_EQUAL( *it, G.factor(I) );
119 }
120 BOOST_CHECK( cit == G.end() );
121 BOOST_CHECK( it == G.end() );
122 }
123
124
125 BOOST_AUTO_TEST_CASE( QueriesTest ) {
126 Var v0( 0, 2 );
127 Var v1( 1, 2 );
128 Var v2( 2, 2 );
129 VarSet v01( v0, v1 );
130 VarSet v02( v0, v2 );
131 VarSet v12( v1, v2 );
132 VarSet v012 = v01 | v2;
133
134 FactorGraph G0;
135 BOOST_CHECK_EQUAL( G0.nrVars(), 0 );
136 BOOST_CHECK_EQUAL( G0.nrFactors(), 0 );
137 BOOST_CHECK_EQUAL( G0.nrEdges(), 0 );
138 BOOST_CHECK_THROW( G0.findVar( v0 ), Exception );
139 BOOST_CHECK_THROW( G0.findVars( v01 ), Exception );
140 BOOST_CHECK_THROW( G0.findFactor( v01 ), Exception );
141 #ifdef DAI_DBEUG
142 BOOST_CHECK_THROW( G0.delta( 0 ), Exception );
143 BOOST_CHECK_THROW( G0.Delta( 0 ), Exception );
144 BOOST_CHECK_THROW( G0.delta( v0 ), Exception );
145 BOOST_CHECK_THROW( G0.Delta( v0 ), Exception );
146 #endif
147 BOOST_CHECK( G0.isConnected() );
148 BOOST_CHECK( G0.isTree() );
149 BOOST_CHECK( G0.isBinary() );
150 BOOST_CHECK( G0.isPairwise() );
151 BOOST_CHECK( G0.MarkovGraph() == GraphAL() );
152 BOOST_CHECK( G0.bipGraph() == BipartiteGraph() );
153 BOOST_CHECK_EQUAL( G0.maximalFactorDomains().size(), 0 );
154
155 std::vector<Factor> facs;
156 facs.push_back( Factor( v01 ) );
157 facs.push_back( Factor( v12 ) );
158 facs.push_back( Factor( v1 ) );
159 std::vector<Var> vars;
160 vars.push_back( v0 );
161 vars.push_back( v1 );
162 vars.push_back( v2 );
163 GraphAL H(3);
164 H.addEdge( 0, 1 );
165 H.addEdge( 1, 2 );
166 BipartiteGraph K(3, 3);
167 K.addEdge( 0, 0 );
168 K.addEdge( 1, 0 );
169 K.addEdge( 1, 1 );
170 K.addEdge( 2, 1 );
171 K.addEdge( 1, 2 );
172
173 FactorGraph G1( facs );
174 BOOST_CHECK_EQUAL( G1.nrVars(), 3 );
175 BOOST_CHECK_EQUAL( G1.nrFactors(), 3 );
176 BOOST_CHECK_EQUAL( G1.nrEdges(), 5 );
177 BOOST_CHECK_EQUAL( G1.findVar( v0 ), 0 );
178 BOOST_CHECK_EQUAL( G1.findVar( v1 ), 1 );
179 BOOST_CHECK_EQUAL( G1.findVar( v2 ), 2 );
180 BOOST_CHECK_EQUAL( G1.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
181 BOOST_CHECK_EQUAL( G1.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
182 BOOST_CHECK_EQUAL( G1.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
183 BOOST_CHECK_EQUAL( G1.findFactor( v01 ), 0 );
184 BOOST_CHECK_EQUAL( G1.findFactor( v12 ), 1 );
185 BOOST_CHECK_EQUAL( G1.findFactor( v1 ), 2 );
186 BOOST_CHECK_THROW( G1.findFactor( v02 ), Exception );
187 BOOST_CHECK_EQUAL( G1.delta( 0 ), v1 );
188 BOOST_CHECK_EQUAL( G1.delta( 1 ), v02 );
189 BOOST_CHECK_EQUAL( G1.delta( 2 ), v1 );
190 BOOST_CHECK_EQUAL( G1.Delta( 0 ), v01 );
191 BOOST_CHECK_EQUAL( G1.Delta( 1 ), v012 );
192 BOOST_CHECK_EQUAL( G1.Delta( 2 ), v12 );
193 BOOST_CHECK_EQUAL( G1.delta( v0 ), v1 );
194 BOOST_CHECK_EQUAL( G1.delta( v1 ), v02 );
195 BOOST_CHECK_EQUAL( G1.delta( v2 ), v1 );
196 BOOST_CHECK_EQUAL( G1.delta( v01 ), v2 );
197 BOOST_CHECK_EQUAL( G1.delta( v02 ), v1 );
198 BOOST_CHECK_EQUAL( G1.delta( v12 ), v0 );
199 BOOST_CHECK_EQUAL( G1.delta( v012 ), VarSet() );
200 BOOST_CHECK_EQUAL( G1.Delta( v0 ), v01 );
201 BOOST_CHECK_EQUAL( G1.Delta( v1 ), v012 );
202 BOOST_CHECK_EQUAL( G1.Delta( v2 ), v12 );
203 BOOST_CHECK_EQUAL( G1.Delta( v01 ), v012 );
204 BOOST_CHECK_EQUAL( G1.Delta( v02 ), v012 );
205 BOOST_CHECK_EQUAL( G1.Delta( v12 ), v012 );
206 BOOST_CHECK_EQUAL( G1.Delta( v012 ), v012 );
207 BOOST_CHECK( G1.isConnected() );
208 BOOST_CHECK( G1.isTree() );
209 BOOST_CHECK( G1.isBinary() );
210 BOOST_CHECK( G1.isPairwise() );
211 BOOST_CHECK( G1.MarkovGraph() == H );
212 BOOST_CHECK( G1.bipGraph() == K );
213 BOOST_CHECK_EQUAL( G1.maximalFactorDomains().size(), 2 );
214 BOOST_CHECK_EQUAL( G1.maximalFactorDomains()[0], v01 );
215 BOOST_CHECK_EQUAL( G1.maximalFactorDomains()[1], v12 );
216
217 facs.push_back( Factor( v02 ) );
218 H.addEdge( 0, 2 );
219 K.addNode2();
220 K.addEdge( 0, 3 );
221 K.addEdge( 2, 3 );
222 FactorGraph G2( facs );
223 BOOST_CHECK_EQUAL( G2.nrVars(), 3 );
224 BOOST_CHECK_EQUAL( G2.nrFactors(), 4 );
225 BOOST_CHECK_EQUAL( G2.nrEdges(), 7 );
226 BOOST_CHECK_EQUAL( G2.findVar( v0 ), 0 );
227 BOOST_CHECK_EQUAL( G2.findVar( v1 ), 1 );
228 BOOST_CHECK_EQUAL( G2.findVar( v2 ), 2 );
229 BOOST_CHECK_EQUAL( G2.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
230 BOOST_CHECK_EQUAL( G2.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
231 BOOST_CHECK_EQUAL( G2.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
232 BOOST_CHECK_EQUAL( G2.findFactor( v01 ), 0 );
233 BOOST_CHECK_EQUAL( G2.findFactor( v12 ), 1 );
234 BOOST_CHECK_EQUAL( G2.findFactor( v1 ), 2 );
235 BOOST_CHECK_EQUAL( G2.findFactor( v02 ), 3 );
236 BOOST_CHECK_EQUAL( G2.delta( 0 ), v12 );
237 BOOST_CHECK_EQUAL( G2.delta( 1 ), v02 );
238 BOOST_CHECK_EQUAL( G2.delta( 2 ), v01 );
239 BOOST_CHECK_EQUAL( G2.Delta( 0 ), v012 );
240 BOOST_CHECK_EQUAL( G2.Delta( 1 ), v012 );
241 BOOST_CHECK_EQUAL( G2.Delta( 2 ), v012 );
242 BOOST_CHECK( G2.isConnected() );
243 BOOST_CHECK( !G2.isTree() );
244 BOOST_CHECK( G2.isBinary() );
245 BOOST_CHECK( G2.isPairwise() );
246 BOOST_CHECK( G2.MarkovGraph() == H );
247 BOOST_CHECK( G2.bipGraph() == K );
248 BOOST_CHECK_EQUAL( G2.maximalFactorDomains().size(), 3 );
249 BOOST_CHECK_EQUAL( G2.maximalFactorDomains()[0], v01 );
250 BOOST_CHECK_EQUAL( G2.maximalFactorDomains()[1], v12 );
251 BOOST_CHECK_EQUAL( G2.maximalFactorDomains()[2], v02 );
252
253 Var v3( 3, 3 );
254 VarSet v03( v0, v3 );
255 VarSet v13( v1, v3 );
256 VarSet v23( v2, v3 );
257 VarSet v013 = v01 | v3;
258 VarSet v023 = v02 | v3;
259 VarSet v123 = v12 | v3;
260 VarSet v0123 = v012 | v3;
261 vars.push_back( v3 );
262 facs.push_back( Factor( v3 ) );
263 H.addNode();
264 K.addNode1();
265 K.addNode2();
266 K.addEdge( 3, 4 );
267 FactorGraph G3( facs );
268 BOOST_CHECK_EQUAL( G3.nrVars(), 4 );
269 BOOST_CHECK_EQUAL( G3.nrFactors(), 5 );
270 BOOST_CHECK_EQUAL( G3.nrEdges(), 8 );
271 BOOST_CHECK_EQUAL( G3.findVar( v0 ), 0 );
272 BOOST_CHECK_EQUAL( G3.findVar( v1 ), 1 );
273 BOOST_CHECK_EQUAL( G3.findVar( v2 ), 2 );
274 BOOST_CHECK_EQUAL( G3.findVar( v3 ), 3 );
275 BOOST_CHECK_EQUAL( G3.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
276 BOOST_CHECK_EQUAL( G3.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
277 BOOST_CHECK_EQUAL( G3.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
278 BOOST_CHECK_EQUAL( G3.findFactor( v01 ), 0 );
279 BOOST_CHECK_EQUAL( G3.findFactor( v12 ), 1 );
280 BOOST_CHECK_EQUAL( G3.findFactor( v1 ), 2 );
281 BOOST_CHECK_EQUAL( G3.findFactor( v02 ), 3 );
282 BOOST_CHECK_EQUAL( G3.findFactor( v3 ), 4 );
283 BOOST_CHECK_THROW( G3.findFactor( v23 ), Exception );
284 BOOST_CHECK_EQUAL( G3.delta( 0 ), v12 );
285 BOOST_CHECK_EQUAL( G3.delta( 1 ), v02 );
286 BOOST_CHECK_EQUAL( G3.delta( 2 ), v01 );
287 BOOST_CHECK_EQUAL( G3.delta( 3 ), VarSet() );
288 BOOST_CHECK_EQUAL( G3.Delta( 0 ), v012 );
289 BOOST_CHECK_EQUAL( G3.Delta( 1 ), v012 );
290 BOOST_CHECK_EQUAL( G3.Delta( 2 ), v012 );
291 BOOST_CHECK_EQUAL( G3.Delta( 3 ), v3 );
292 BOOST_CHECK( !G3.isConnected() );
293 BOOST_CHECK( !G3.isTree() );
294 BOOST_CHECK( !G3.isBinary() );
295 BOOST_CHECK( G3.isPairwise() );
296 BOOST_CHECK( G3.MarkovGraph() == H );
297 BOOST_CHECK( G3.bipGraph() == K );
298 BOOST_CHECK_EQUAL( G3.maximalFactorDomains().size(), 4 );
299 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[0], v01 );
300 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[1], v12 );
301 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[2], v02 );
302 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[3], v3 );
303
304 facs.push_back( Factor( v123 ) );
305 H.addEdge( 1, 3 );
306 H.addEdge( 2, 3 );
307 K.addNode2();
308 K.addEdge( 1, 5 );
309 K.addEdge( 2, 5 );
310 K.addEdge( 3, 5 );
311 FactorGraph G4( facs );
312 BOOST_CHECK_EQUAL( G4.nrVars(), 4 );
313 BOOST_CHECK_EQUAL( G4.nrFactors(), 6 );
314 BOOST_CHECK_EQUAL( G4.nrEdges(), 11 );
315 BOOST_CHECK_EQUAL( G4.findVar( v0 ), 0 );
316 BOOST_CHECK_EQUAL( G4.findVar( v1 ), 1 );
317 BOOST_CHECK_EQUAL( G4.findVar( v2 ), 2 );
318 BOOST_CHECK_EQUAL( G4.findVar( v3 ), 3 );
319 BOOST_CHECK_EQUAL( G4.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
320 BOOST_CHECK_EQUAL( G4.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
321 BOOST_CHECK_EQUAL( G4.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
322 BOOST_CHECK_EQUAL( G4.findFactor( v01 ), 0 );
323 BOOST_CHECK_EQUAL( G4.findFactor( v12 ), 1 );
324 BOOST_CHECK_EQUAL( G4.findFactor( v1 ), 2 );
325 BOOST_CHECK_EQUAL( G4.findFactor( v02 ), 3 );
326 BOOST_CHECK_EQUAL( G4.findFactor( v3 ), 4 );
327 BOOST_CHECK_EQUAL( G4.findFactor( v123 ), 5 );
328 BOOST_CHECK_THROW( G4.findFactor( v23 ), Exception );
329 BOOST_CHECK_EQUAL( G4.delta( 0 ), v12 );
330 BOOST_CHECK_EQUAL( G4.delta( 1 ), v023 );
331 BOOST_CHECK_EQUAL( G4.delta( 2 ), v013 );
332 BOOST_CHECK_EQUAL( G4.delta( 3 ), v12 );
333 BOOST_CHECK_EQUAL( G4.Delta( 0 ), v012 );
334 BOOST_CHECK_EQUAL( G4.Delta( 1 ), v0123 );
335 BOOST_CHECK_EQUAL( G4.Delta( 2 ), v0123 );
336 BOOST_CHECK_EQUAL( G4.Delta( 3 ), v123 );
337 BOOST_CHECK( G4.isConnected() );
338 BOOST_CHECK( !G4.isTree() );
339 BOOST_CHECK( !G4.isBinary() );
340 BOOST_CHECK( !G4.isPairwise() );
341 BOOST_CHECK( G4.MarkovGraph() == H );
342 BOOST_CHECK( G4.bipGraph() == K );
343 BOOST_CHECK_EQUAL( G4.maximalFactorDomains().size(), 3 );
344 BOOST_CHECK_EQUAL( G4.maximalFactorDomains()[0], v01 );
345 BOOST_CHECK_EQUAL( G4.maximalFactorDomains()[1], v02 );
346 BOOST_CHECK_EQUAL( G4.maximalFactorDomains()[2], v123 );
347 }
348
349
350 BOOST_AUTO_TEST_CASE( BackupRestoreTest ) {
351 Var v0( 0, 2 );
352 Var v1( 1, 2 );
353 Var v2( 2, 2 );
354 VarSet v01( v0, v1 );
355 VarSet v02( v0, v2 );
356 VarSet v12( v1, v2 );
357 VarSet v012 = v01 | v2;
358
359 std::vector<Factor> facs;
360 facs.push_back( Factor( v01 ) );
361 facs.push_back( Factor( v12 ) );
362 facs.push_back( Factor( v1 ) );
363 std::vector<Var> vars;
364 vars.push_back( v0 );
365 vars.push_back( v1 );
366 vars.push_back( v2 );
367
368 FactorGraph G( facs );
369 FactorGraph Gorg( G );
370
371 BOOST_CHECK_THROW( G.setFactor( 0, Factor( v0 ), false ), Exception );
372 G.setFactor( 0, Factor( v01, 2.0 ), false );
373 BOOST_CHECK_THROW( G.restoreFactor( 0 ), Exception );
374 G.setFactor( 0, Factor( v01, 3.0 ), true );
375 G.restoreFactor( 0 );
376 BOOST_CHECK_EQUAL( G.factor( 0 )[0], 2.0 );
377 G.setFactor( 0, Gorg.factor( 0 ), false );
378 G.backupFactor( 0 );
379 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
380 G.setFactor( 0, Factor( v01, 2.0 ), false );
381 BOOST_CHECK_EQUAL( G.factor( 0 )[0], 2.0 );
382 G.restoreFactor( 0 );
383 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
384
385 std::map<size_t, Factor> fs;
386 fs[0] = Factor( v01, 3.0 );
387 fs[2] = Factor( v1, 2.0 );
388 G.setFactors( fs, false );
389 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
390 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
391 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
392 G.restoreFactors();
393 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
394 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
395 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
396 G = Gorg;
397 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
398 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
399 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
400 G.setFactors( fs, true );
401 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
402 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
403 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
404 G.restoreFactors();
405 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
406 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
407 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
408 std::set<size_t> fsind;
409 fsind.insert( 0 );
410 fsind.insert( 2 );
411 G.backupFactors( fsind );
412 G.setFactors( fs, false );
413 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
414 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
415 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
416 G.restoreFactors();
417 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
418 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
419 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
420
421 G.backupFactors( v2 );
422 G.setFactor( 1, Factor(v12, 5.0) );
423 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
424 BOOST_CHECK_EQUAL( G.factor(1), Factor(v12, 5.0) );
425 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
426 G.restoreFactors( v2 );
427 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
428 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
429 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
430
431 G.backupFactors( v1 );
432 fs[1] = Factor( v12, 5.0 );
433 G.setFactors( fs, false );
434 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
435 BOOST_CHECK_EQUAL( G.factor(1), fs[1] );
436 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
437 G.restoreFactors();
438 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
439 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
440 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
441 G.setFactors( fs, true );
442 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
443 BOOST_CHECK_EQUAL( G.factor(1), fs[1] );
444 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
445 G.restoreFactors( v1 );
446 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
447 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
448 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
449 }
450
451
452 BOOST_AUTO_TEST_CASE( TransformationsTest ) {
453 Var v0( 0, 2 );
454 Var v1( 1, 2 );
455 Var v2( 2, 2 );
456 VarSet v01( v0, v1 );
457 VarSet v02( v0, v2 );
458 VarSet v12( v1, v2 );
459 VarSet v012 = v01 | v2;
460
461 std::vector<Factor> facs;
462 facs.push_back( Factor( v01 ).randomize() );
463 facs.push_back( Factor( v12 ).randomize() );
464 facs.push_back( Factor( v1 ).randomize() );
465 std::vector<Var> vars;
466 vars.push_back( v0 );
467 vars.push_back( v1 );
468 vars.push_back( v2 );
469
470 FactorGraph G( facs );
471
472 FactorGraph Gsmall = G.maximalFactors();
473 BOOST_CHECK_EQUAL( Gsmall.nrVars(), 3 );
474 BOOST_CHECK_EQUAL( Gsmall.nrFactors(), 2 );
475 BOOST_CHECK_EQUAL( Gsmall.factor( 0 ), G.factor( 0 ) * G.factor( 2 ) );
476 BOOST_CHECK_EQUAL( Gsmall.factor( 1 ), G.factor( 1 ) );
477
478 size_t i = 0;
479 for( size_t x = 0; x < 2; x++ ) {
480 FactorGraph Gcl = G.clamped( i, x );
481 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
482 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
483 BOOST_CHECK_EQUAL( Gcl.factor(0), createFactorDelta(vars[i], x) );
484 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(0).slice(vars[i], x) * G.factor(2) );
485 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(1) );
486 }
487 i = 1;
488 for( size_t x = 0; x < 2; x++ ) {
489 FactorGraph Gcl = G.clamped( i, x );
490 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
491 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
492 BOOST_CHECK_EQUAL( Gcl.factor(0), createFactorDelta(vars[i], x) * G.factor(2).slice(vars[i],x) );
493 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(0).slice(vars[i], x) );
494 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(1).slice(vars[i], x) );
495 }
496 i = 2;
497 for( size_t x = 0; x < 2; x++ ) {
498 FactorGraph Gcl = G.clamped( i, x );
499 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
500 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
501 BOOST_CHECK_EQUAL( Gcl.factor(0), createFactorDelta(vars[i], x) );
502 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(0) );
503 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(1).slice(vars[i], x) * G.factor(2) );
504 }
505 }
506
507
508 BOOST_AUTO_TEST_CASE( OperationsTest ) {
509 Var v0( 0, 2 );
510 Var v1( 1, 2 );
511 Var v2( 2, 2 );
512 VarSet v01( v0, v1 );
513 VarSet v02( v0, v2 );
514 VarSet v12( v1, v2 );
515 VarSet v012 = v01 | v2;
516
517 std::vector<Factor> facs;
518 facs.push_back( Factor( v01 ).randomize() );
519 facs.push_back( Factor( v12 ).randomize() );
520 facs.push_back( Factor( v1 ).randomize() );
521 std::vector<Var> vars;
522 vars.push_back( v0 );
523 vars.push_back( v1 );
524 vars.push_back( v2 );
525
526 FactorGraph G( facs );
527
528 // clamp
529 FactorGraph Gcl = G;
530 for( size_t i = 0; i < 3; i++ )
531 for( size_t x = 0; x < 2; x++ ) {
532 Gcl.clamp( i, x, true );
533 Factor delta = createFactorDelta( vars[i], x );
534 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
535 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
536 for( size_t j = 0; j < 3; j++ )
537 if( G.factor(j).vars().contains( vars[i] ) )
538 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) * delta );
539 else
540 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
541
542 Gcl.restoreFactors();
543 for( size_t j = 0; j < 3; j++ )
544 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
545 }
546
547 // clampVar
548 for( size_t i = 0; i < 3; i++ )
549 for( size_t x = 0; x < 2; x++ ) {
550 Gcl.clampVar( i, std::vector<size_t>(1, x), true );
551 Factor delta = createFactorDelta( vars[i], x );
552 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
553 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
554 for( size_t j = 0; j < 3; j++ )
555 if( G.factor(j).vars().contains( vars[i] ) )
556 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) * delta );
557 else
558 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
559
560 Gcl.restoreFactors();
561 for( size_t j = 0; j < 3; j++ )
562 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
563 }
564 for( size_t i = 0; i < 3; i++ )
565 for( size_t x = 0; x < 2; x++ ) {
566 Gcl.clampVar( i, std::vector<size_t>(), true );
567 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
568 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
569 for( size_t j = 0; j < 3; j++ )
570 if( G.factor(j).vars().contains( vars[i] ) )
571 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) * 0.0 );
572 else
573 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
574
575 Gcl.restoreFactors();
576 for( size_t j = 0; j < 3; j++ )
577 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
578 }
579 std::vector<size_t> both;
580 both.push_back( 0 );
581 both.push_back( 1 );
582 for( size_t i = 0; i < 3; i++ )
583 for( size_t x = 0; x < 2; x++ ) {
584 Gcl.clampVar( i, both, true );
585 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
586 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
587 for( size_t j = 0; j < 3; j++ )
588 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
589 Gcl.restoreFactors();
590 }
591
592 // clampFactor
593 for( size_t x = 0; x < 4; x++ ) {
594 Gcl.clampFactor( 0, std::vector<size_t>(1,x), true );
595 Factor mask( v01, 0.0 );
596 mask.set( x, 1.0 );
597 BOOST_CHECK_EQUAL( Gcl.factor(0), G.factor(0) * mask );
598 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(1) );
599 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(2) );
600 Gcl.restoreFactor( 0 );
601 }
602 for( size_t x = 0; x < 4; x++ ) {
603 Gcl.clampFactor( 1, std::vector<size_t>(1,x), true );
604 Factor mask( v12, 0.0 );
605 mask.set( x, 1.0 );
606 BOOST_CHECK_EQUAL( Gcl.factor(0), G.factor(0) );
607 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(1) * mask );
608 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(2) );
609 Gcl.restoreFactor( 1 );
610 }
611 for( size_t x = 0; x < 2; x++ ) {
612 Gcl.clampFactor( 2, std::vector<size_t>(1,x), true );
613 Factor mask( v1, 0.0 );
614 mask.set( x, 1.0 );
615 BOOST_CHECK_EQUAL( Gcl.factor(0), G.factor(0) );
616 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(1) );
617 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(2) * mask );
618 Gcl.restoreFactors();
619 }
620
621 // makeCavity
622 FactorGraph Gcav( G );
623 Gcav.makeCavity( 0, true );
624 BOOST_CHECK_EQUAL( Gcav.factor(0), Factor( v01, 1.0 ) );
625 BOOST_CHECK_EQUAL( Gcav.factor(1), G.factor(1) );
626 BOOST_CHECK_EQUAL( Gcav.factor(2), G.factor(2) );
627 Gcav.restoreFactors();
628 Gcav.makeCavity( 1, true );
629 BOOST_CHECK_EQUAL( Gcav.factor(0), Factor( v01, 1.0 ) );
630 BOOST_CHECK_EQUAL( Gcav.factor(1), Factor( v12, 1.0 ) );
631 BOOST_CHECK_EQUAL( Gcav.factor(2), Factor( v1, 1.0 ) );
632 Gcav.restoreFactors();
633 Gcav.makeCavity( 2, true );
634 BOOST_CHECK_EQUAL( Gcav.factor(0), G.factor(0) );
635 BOOST_CHECK_EQUAL( Gcav.factor(1), Factor( v12, 1.0 ) );
636 BOOST_CHECK_EQUAL( Gcav.factor(2), G.factor(2) );
637 Gcav.restoreFactors();
638 }
639
640
641 BOOST_AUTO_TEST_CASE( IOTest ) {
642 Var v0( 0, 2 );
643 Var v1( 1, 2 );
644 Var v2( 2, 2 );
645 VarSet v01( v0, v1 );
646 VarSet v02( v0, v2 );
647 VarSet v12( v1, v2 );
648 VarSet v012 = v01 | v2;
649
650 std::vector<Factor> facs;
651 facs.push_back( Factor( v01 ).randomize() );
652 facs.push_back( Factor( v12 ).randomize() );
653 facs.push_back( Factor( v1 ).randomize() );
654 std::vector<Var> vars;
655 vars.push_back( v0 );
656 vars.push_back( v1 );
657 vars.push_back( v2 );
658
659 FactorGraph G( facs );
660
661 G.WriteToFile( "factorgraph_test.fg" );
662 FactorGraph G2;
663 G2.ReadFromFile( "factorgraph_test.fg" );
664
665 BOOST_CHECK( G.vars() == G2.vars() );
666 BOOST_CHECK( G.bipGraph() == G2.bipGraph() );
667 BOOST_CHECK_EQUAL( G.nrFactors(), G2.nrFactors() );
668 for( size_t I = 0; I < G.nrFactors(); I++ ) {
669 BOOST_CHECK( G.factor(I).vars() == G2.factor(I).vars() );
670 for( size_t s = 0; s < G.factor(I).nrStates(); s++ )
671 BOOST_CHECK_CLOSE( G.factor(I)[s], G2.factor(I)[s], tol );
672 }
673
674 std::stringstream ss;
675 std::string s;
676 G.printDot( ss );
677 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "graph G {" );
678 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "node[shape=circle,width=0.4,fixedsize=true];" );
679 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv0;" );
680 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1;" );
681 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv2;" );
682 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "node[shape=box,width=0.3,height=0.3,fixedsize=true];" );
683 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tf0;" );
684 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tf1;" );
685 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tf2;" );
686 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv0 -- f0;" );
687 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1 -- f0;" );
688 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1 -- f1;" );
689 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1 -- f2;" );
690 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv2 -- f1;" );
691 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "}" );
692
693 G.factor(0).fill(1.0);
694 G.factor(1).fill(2.0);
695 G.factor(2).fill(3.0);
696 ss << G;
697 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "3" );
698 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "" );
699 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2" );
700 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 1 " );
701 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 2 " );
702 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "4" );
703 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 1" );
704 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 1" );
705 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 1" );
706 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "3 1" );
707 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "" );
708 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2" );
709 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 2 " );
710 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 2 " );
711 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "4" );
712 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 2" );
713 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 2" );
714 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 2" );
715 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "3 2" );
716 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "" );
717 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1" );
718 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 " );
719 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 " );
720 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2" );
721 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 3" );
722 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 3" );
723
724 ss << G;
725 FactorGraph G3;
726 ss >> G3;
727 BOOST_CHECK( G.vars() == G3.vars() );
728 BOOST_CHECK( G.bipGraph() == G3.bipGraph() );
729 BOOST_CHECK_EQUAL( G.nrFactors(), G3.nrFactors() );
730 for( size_t I = 0; I < G.nrFactors(); I++ ) {
731 BOOST_CHECK( G.factor(I).vars() == G3.factor(I).vars() );
732 for( size_t s = 0; s < G.factor(I).nrStates(); s++ )
733 BOOST_CHECK_CLOSE( G.factor(I)[s], G3.factor(I)[s], tol );
734 }
735 }