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