Miscellaneous improvements in FactorGraph, Permute, HAK
[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(), 1 );
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( G1.isMaximal( 0 ) );
202 BOOST_CHECK( G1.isMaximal( 1 ) );
203 BOOST_CHECK( !G1.isMaximal( 2 ) );
204 BOOST_CHECK_EQUAL( G1.maximalFactor( 0 ), 0 );
205 BOOST_CHECK_EQUAL( G1.maximalFactor( 1 ), 1 );
206 BOOST_CHECK_EQUAL( G1.maximalFactor( 2 ), 0 );
207 BOOST_CHECK_EQUAL( G1.maximalFactorDomains().size(), 2 );
208 BOOST_CHECK_EQUAL( G1.maximalFactorDomains()[0], v01 );
209 BOOST_CHECK_EQUAL( G1.maximalFactorDomains()[1], v12 );
210
211 facs.push_back( Factor( v02 ) );
212 H.addEdge( 0, 2 );
213 K.addNode2();
214 K.addEdge( 0, 3 );
215 K.addEdge( 2, 3 );
216 FactorGraph G2( facs );
217 BOOST_CHECK_EQUAL( G2.nrVars(), 3 );
218 BOOST_CHECK_EQUAL( G2.nrFactors(), 4 );
219 BOOST_CHECK_EQUAL( G2.nrEdges(), 7 );
220 BOOST_CHECK_EQUAL( G2.findVar( v0 ), 0 );
221 BOOST_CHECK_EQUAL( G2.findVar( v1 ), 1 );
222 BOOST_CHECK_EQUAL( G2.findVar( v2 ), 2 );
223 BOOST_CHECK_EQUAL( G2.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
224 BOOST_CHECK_EQUAL( G2.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
225 BOOST_CHECK_EQUAL( G2.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
226 BOOST_CHECK_EQUAL( G2.findFactor( v01 ), 0 );
227 BOOST_CHECK_EQUAL( G2.findFactor( v12 ), 1 );
228 BOOST_CHECK_EQUAL( G2.findFactor( v1 ), 2 );
229 BOOST_CHECK_EQUAL( G2.findFactor( v02 ), 3 );
230 BOOST_CHECK_EQUAL( G2.delta( 0 ), v12 );
231 BOOST_CHECK_EQUAL( G2.delta( 1 ), v02 );
232 BOOST_CHECK_EQUAL( G2.delta( 2 ), v01 );
233 BOOST_CHECK_EQUAL( G2.Delta( 0 ), v012 );
234 BOOST_CHECK_EQUAL( G2.Delta( 1 ), v012 );
235 BOOST_CHECK_EQUAL( G2.Delta( 2 ), v012 );
236 BOOST_CHECK( G2.isConnected() );
237 BOOST_CHECK( !G2.isTree() );
238 BOOST_CHECK( G2.isBinary() );
239 BOOST_CHECK( G2.isPairwise() );
240 BOOST_CHECK( G2.MarkovGraph() == H );
241 BOOST_CHECK( G2.bipGraph() == K );
242 BOOST_CHECK( G2.isMaximal( 0 ) );
243 BOOST_CHECK( G2.isMaximal( 1 ) );
244 BOOST_CHECK( !G2.isMaximal( 2 ) );
245 BOOST_CHECK( G2.isMaximal( 3 ) );
246 BOOST_CHECK_EQUAL( G2.maximalFactor( 0 ), 0 );
247 BOOST_CHECK_EQUAL( G2.maximalFactor( 1 ), 1 );
248 BOOST_CHECK_EQUAL( G2.maximalFactor( 2 ), 0 );
249 BOOST_CHECK_EQUAL( G2.maximalFactor( 3 ), 3 );
250 BOOST_CHECK_EQUAL( G2.maximalFactorDomains().size(), 3 );
251 BOOST_CHECK_EQUAL( G2.maximalFactorDomains()[0], v01 );
252 BOOST_CHECK_EQUAL( G2.maximalFactorDomains()[1], v12 );
253 BOOST_CHECK_EQUAL( G2.maximalFactorDomains()[2], v02 );
254
255 Var v3( 3, 3 );
256 VarSet v03( v0, v3 );
257 VarSet v13( v1, v3 );
258 VarSet v23( v2, v3 );
259 VarSet v013 = v01 | v3;
260 VarSet v023 = v02 | v3;
261 VarSet v123 = v12 | v3;
262 VarSet v0123 = v012 | v3;
263 vars.push_back( v3 );
264 facs.push_back( Factor( v3 ) );
265 H.addNode();
266 K.addNode1();
267 K.addNode2();
268 K.addEdge( 3, 4 );
269 FactorGraph G3( facs );
270 BOOST_CHECK_EQUAL( G3.nrVars(), 4 );
271 BOOST_CHECK_EQUAL( G3.nrFactors(), 5 );
272 BOOST_CHECK_EQUAL( G3.nrEdges(), 8 );
273 BOOST_CHECK_EQUAL( G3.findVar( v0 ), 0 );
274 BOOST_CHECK_EQUAL( G3.findVar( v1 ), 1 );
275 BOOST_CHECK_EQUAL( G3.findVar( v2 ), 2 );
276 BOOST_CHECK_EQUAL( G3.findVar( v3 ), 3 );
277 BOOST_CHECK_EQUAL( G3.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
278 BOOST_CHECK_EQUAL( G3.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
279 BOOST_CHECK_EQUAL( G3.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
280 BOOST_CHECK_EQUAL( G3.findFactor( v01 ), 0 );
281 BOOST_CHECK_EQUAL( G3.findFactor( v12 ), 1 );
282 BOOST_CHECK_EQUAL( G3.findFactor( v1 ), 2 );
283 BOOST_CHECK_EQUAL( G3.findFactor( v02 ), 3 );
284 BOOST_CHECK_EQUAL( G3.findFactor( v3 ), 4 );
285 BOOST_CHECK_THROW( G3.findFactor( v23 ), Exception );
286 BOOST_CHECK_EQUAL( G3.delta( 0 ), v12 );
287 BOOST_CHECK_EQUAL( G3.delta( 1 ), v02 );
288 BOOST_CHECK_EQUAL( G3.delta( 2 ), v01 );
289 BOOST_CHECK_EQUAL( G3.delta( 3 ), VarSet() );
290 BOOST_CHECK_EQUAL( G3.Delta( 0 ), v012 );
291 BOOST_CHECK_EQUAL( G3.Delta( 1 ), v012 );
292 BOOST_CHECK_EQUAL( G3.Delta( 2 ), v012 );
293 BOOST_CHECK_EQUAL( G3.Delta( 3 ), v3 );
294 BOOST_CHECK( !G3.isConnected() );
295 BOOST_CHECK( !G3.isTree() );
296 BOOST_CHECK( !G3.isBinary() );
297 BOOST_CHECK( G3.isPairwise() );
298 BOOST_CHECK( G3.MarkovGraph() == H );
299 BOOST_CHECK( G3.bipGraph() == K );
300 BOOST_CHECK( G3.isMaximal( 0 ) );
301 BOOST_CHECK( G3.isMaximal( 1 ) );
302 BOOST_CHECK( !G3.isMaximal( 2 ) );
303 BOOST_CHECK( G3.isMaximal( 3 ) );
304 BOOST_CHECK( G3.isMaximal( 4 ) );
305 BOOST_CHECK_EQUAL( G3.maximalFactor( 0 ), 0 );
306 BOOST_CHECK_EQUAL( G3.maximalFactor( 1 ), 1 );
307 BOOST_CHECK_EQUAL( G3.maximalFactor( 2 ), 0 );
308 BOOST_CHECK_EQUAL( G3.maximalFactor( 3 ), 3 );
309 BOOST_CHECK_EQUAL( G3.maximalFactor( 4 ), 4 );
310 BOOST_CHECK_EQUAL( G3.maximalFactorDomains().size(), 4 );
311 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[0], v01 );
312 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[1], v12 );
313 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[2], v02 );
314 BOOST_CHECK_EQUAL( G3.maximalFactorDomains()[3], v3 );
315
316 facs.push_back( Factor( v123 ) );
317 H.addEdge( 1, 3 );
318 H.addEdge( 2, 3 );
319 K.addNode2();
320 K.addEdge( 1, 5 );
321 K.addEdge( 2, 5 );
322 K.addEdge( 3, 5 );
323 FactorGraph G4( facs );
324 BOOST_CHECK_EQUAL( G4.nrVars(), 4 );
325 BOOST_CHECK_EQUAL( G4.nrFactors(), 6 );
326 BOOST_CHECK_EQUAL( G4.nrEdges(), 11 );
327 BOOST_CHECK_EQUAL( G4.findVar( v0 ), 0 );
328 BOOST_CHECK_EQUAL( G4.findVar( v1 ), 1 );
329 BOOST_CHECK_EQUAL( G4.findVar( v2 ), 2 );
330 BOOST_CHECK_EQUAL( G4.findVar( v3 ), 3 );
331 BOOST_CHECK_EQUAL( G4.findVars( v01 ), SmallSet<size_t>( 0, 1 ) );
332 BOOST_CHECK_EQUAL( G4.findVars( v02 ), SmallSet<size_t>( 0, 2 ) );
333 BOOST_CHECK_EQUAL( G4.findVars( v12 ), SmallSet<size_t>( 1, 2 ) );
334 BOOST_CHECK_EQUAL( G4.findFactor( v01 ), 0 );
335 BOOST_CHECK_EQUAL( G4.findFactor( v12 ), 1 );
336 BOOST_CHECK_EQUAL( G4.findFactor( v1 ), 2 );
337 BOOST_CHECK_EQUAL( G4.findFactor( v02 ), 3 );
338 BOOST_CHECK_EQUAL( G4.findFactor( v3 ), 4 );
339 BOOST_CHECK_EQUAL( G4.findFactor( v123 ), 5 );
340 BOOST_CHECK_THROW( G4.findFactor( v23 ), Exception );
341 BOOST_CHECK_EQUAL( G4.delta( 0 ), v12 );
342 BOOST_CHECK_EQUAL( G4.delta( 1 ), v023 );
343 BOOST_CHECK_EQUAL( G4.delta( 2 ), v013 );
344 BOOST_CHECK_EQUAL( G4.delta( 3 ), v12 );
345 BOOST_CHECK_EQUAL( G4.Delta( 0 ), v012 );
346 BOOST_CHECK_EQUAL( G4.Delta( 1 ), v0123 );
347 BOOST_CHECK_EQUAL( G4.Delta( 2 ), v0123 );
348 BOOST_CHECK_EQUAL( G4.Delta( 3 ), v123 );
349 BOOST_CHECK( G4.isConnected() );
350 BOOST_CHECK( !G4.isTree() );
351 BOOST_CHECK( !G4.isBinary() );
352 BOOST_CHECK( !G4.isPairwise() );
353 BOOST_CHECK( G4.MarkovGraph() == H );
354 BOOST_CHECK( G4.bipGraph() == K );
355 BOOST_CHECK( G4.isMaximal( 0 ) );
356 BOOST_CHECK( !G4.isMaximal( 1 ) );
357 BOOST_CHECK( !G4.isMaximal( 2 ) );
358 BOOST_CHECK( G4.isMaximal( 3 ) );
359 BOOST_CHECK( !G4.isMaximal( 4 ) );
360 BOOST_CHECK( G4.isMaximal( 5 ) );
361 BOOST_CHECK_EQUAL( G4.maximalFactor( 0 ), 0 );
362 BOOST_CHECK_EQUAL( G4.maximalFactor( 1 ), 5 );
363 BOOST_CHECK_EQUAL( G4.maximalFactor( 2 ), 0 );
364 BOOST_CHECK_EQUAL( G4.maximalFactor( 3 ), 3 );
365 BOOST_CHECK_EQUAL( G4.maximalFactor( 4 ), 5 );
366 BOOST_CHECK_EQUAL( G4.maximalFactor( 5 ), 5 );
367 BOOST_CHECK_EQUAL( G4.maximalFactorDomains().size(), 3 );
368 BOOST_CHECK_EQUAL( G4.maximalFactorDomains()[0], v01 );
369 BOOST_CHECK_EQUAL( G4.maximalFactorDomains()[1], v02 );
370 BOOST_CHECK_EQUAL( G4.maximalFactorDomains()[2], v123 );
371 }
372
373
374 BOOST_AUTO_TEST_CASE( BackupRestoreTest ) {
375 Var v0( 0, 2 );
376 Var v1( 1, 2 );
377 Var v2( 2, 2 );
378 VarSet v01( v0, v1 );
379 VarSet v02( v0, v2 );
380 VarSet v12( v1, v2 );
381 VarSet v012 = v01 | v2;
382
383 std::vector<Factor> facs;
384 facs.push_back( Factor( v01 ) );
385 facs.push_back( Factor( v12 ) );
386 facs.push_back( Factor( v1 ) );
387 std::vector<Var> vars;
388 vars.push_back( v0 );
389 vars.push_back( v1 );
390 vars.push_back( v2 );
391
392 FactorGraph G( facs );
393 FactorGraph Gorg( G );
394
395 BOOST_CHECK_THROW( G.setFactor( 0, Factor( v0 ), false ), Exception );
396 G.setFactor( 0, Factor( v01, 2.0 ), false );
397 BOOST_CHECK_THROW( G.restoreFactor( 0 ), Exception );
398 G.setFactor( 0, Factor( v01, 3.0 ), true );
399 G.restoreFactor( 0 );
400 BOOST_CHECK_EQUAL( G.factor( 0 )[0], 2.0 );
401 G.setFactor( 0, Gorg.factor( 0 ), false );
402 G.backupFactor( 0 );
403 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
404 G.setFactor( 0, Factor( v01, 2.0 ), false );
405 BOOST_CHECK_EQUAL( G.factor( 0 )[0], 2.0 );
406 G.restoreFactor( 0 );
407 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
408
409 std::map<size_t, Factor> fs;
410 fs[0] = Factor( v01, 3.0 );
411 fs[2] = Factor( v1, 2.0 );
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), fs[0] );
418 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
419 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
420 G = Gorg;
421 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
422 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
423 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
424 G.setFactors( fs, true );
425 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
426 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
427 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
428 G.restoreFactors();
429 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
430 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
431 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
432 std::set<size_t> fsind;
433 fsind.insert( 0 );
434 fsind.insert( 2 );
435 G.backupFactors( fsind );
436 G.setFactors( fs, false );
437 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
438 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
439 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
440 G.restoreFactors();
441 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
442 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
443 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
444
445 G.backupFactors( v2 );
446 G.setFactor( 1, Factor(v12, 5.0) );
447 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
448 BOOST_CHECK_EQUAL( G.factor(1), Factor(v12, 5.0) );
449 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
450 G.restoreFactors( v2 );
451 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
452 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
453 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
454
455 G.backupFactors( v1 );
456 fs[1] = Factor( v12, 5.0 );
457 G.setFactors( fs, false );
458 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
459 BOOST_CHECK_EQUAL( G.factor(1), fs[1] );
460 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
461 G.restoreFactors();
462 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
463 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
464 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
465 G.setFactors( fs, true );
466 BOOST_CHECK_EQUAL( G.factor(0), fs[0] );
467 BOOST_CHECK_EQUAL( G.factor(1), fs[1] );
468 BOOST_CHECK_EQUAL( G.factor(2), fs[2] );
469 G.restoreFactors( v1 );
470 BOOST_CHECK_EQUAL( G.factor(0), Gorg.factor(0) );
471 BOOST_CHECK_EQUAL( G.factor(1), Gorg.factor(1) );
472 BOOST_CHECK_EQUAL( G.factor(2), Gorg.factor(2) );
473 }
474
475
476 BOOST_AUTO_TEST_CASE( TransformationsTest ) {
477 Var v0( 0, 2 );
478 Var v1( 1, 2 );
479 Var v2( 2, 2 );
480 VarSet v01( v0, v1 );
481 VarSet v02( v0, v2 );
482 VarSet v12( v1, v2 );
483 VarSet v012 = v01 | v2;
484
485 std::vector<Factor> facs;
486 facs.push_back( Factor( v01 ).randomize() );
487 facs.push_back( Factor( v12 ).randomize() );
488 facs.push_back( Factor( v1 ).randomize() );
489 std::vector<Var> vars;
490 vars.push_back( v0 );
491 vars.push_back( v1 );
492 vars.push_back( v2 );
493
494 FactorGraph G( facs );
495
496 FactorGraph Gsmall = G.maximalFactors();
497 BOOST_CHECK_EQUAL( Gsmall.nrVars(), 3 );
498 BOOST_CHECK_EQUAL( Gsmall.nrFactors(), 2 );
499 BOOST_CHECK_EQUAL( Gsmall.factor( 0 ), G.factor( 0 ) * G.factor( 2 ) );
500 BOOST_CHECK_EQUAL( Gsmall.factor( 1 ), G.factor( 1 ) );
501
502 size_t i = 0;
503 for( size_t x = 0; x < 2; x++ ) {
504 FactorGraph Gcl = G.clamped( i, x );
505 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
506 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
507 BOOST_CHECK_EQUAL( Gcl.factor(0), createFactorDelta(vars[i], x) );
508 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(0).slice(vars[i], x) * G.factor(2) );
509 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(1) );
510 }
511 i = 1;
512 for( size_t x = 0; x < 2; x++ ) {
513 FactorGraph Gcl = G.clamped( i, x );
514 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
515 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
516 BOOST_CHECK_EQUAL( Gcl.factor(0), createFactorDelta(vars[i], x) * G.factor(2).slice(vars[i],x) );
517 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(0).slice(vars[i], x) );
518 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(1).slice(vars[i], x) );
519 }
520 i = 2;
521 for( size_t x = 0; x < 2; x++ ) {
522 FactorGraph Gcl = G.clamped( i, x );
523 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
524 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
525 BOOST_CHECK_EQUAL( Gcl.factor(0), createFactorDelta(vars[i], x) );
526 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(0) );
527 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(1).slice(vars[i], x) * G.factor(2) );
528 }
529 }
530
531
532 BOOST_AUTO_TEST_CASE( OperationsTest ) {
533 Var v0( 0, 2 );
534 Var v1( 1, 2 );
535 Var v2( 2, 2 );
536 VarSet v01( v0, v1 );
537 VarSet v02( v0, v2 );
538 VarSet v12( v1, v2 );
539 VarSet v012 = v01 | v2;
540
541 std::vector<Factor> facs;
542 facs.push_back( Factor( v01 ).randomize() );
543 facs.push_back( Factor( v12 ).randomize() );
544 facs.push_back( Factor( v1 ).randomize() );
545 std::vector<Var> vars;
546 vars.push_back( v0 );
547 vars.push_back( v1 );
548 vars.push_back( v2 );
549
550 FactorGraph G( facs );
551
552 // clamp
553 FactorGraph Gcl = G;
554 for( size_t i = 0; i < 3; i++ )
555 for( size_t x = 0; x < 2; x++ ) {
556 Gcl.clamp( i, x, true );
557 Factor delta = createFactorDelta( vars[i], x );
558 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
559 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
560 for( size_t j = 0; j < 3; j++ )
561 if( G.factor(j).vars().contains( vars[i] ) )
562 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) * delta );
563 else
564 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
565
566 Gcl.restoreFactors();
567 for( size_t j = 0; j < 3; j++ )
568 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
569 }
570
571 // clampVar
572 for( size_t i = 0; i < 3; i++ )
573 for( size_t x = 0; x < 2; x++ ) {
574 Gcl.clampVar( i, std::vector<size_t>(1, x), true );
575 Factor delta = createFactorDelta( vars[i], x );
576 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
577 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
578 for( size_t j = 0; j < 3; j++ )
579 if( G.factor(j).vars().contains( vars[i] ) )
580 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) * delta );
581 else
582 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
583
584 Gcl.restoreFactors();
585 for( size_t j = 0; j < 3; j++ )
586 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
587 }
588 for( size_t i = 0; i < 3; i++ )
589 for( size_t x = 0; x < 2; x++ ) {
590 Gcl.clampVar( i, std::vector<size_t>(), true );
591 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
592 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
593 for( size_t j = 0; j < 3; j++ )
594 if( G.factor(j).vars().contains( vars[i] ) )
595 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) * 0.0 );
596 else
597 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
598
599 Gcl.restoreFactors();
600 for( size_t j = 0; j < 3; j++ )
601 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
602 }
603 std::vector<size_t> both;
604 both.push_back( 0 );
605 both.push_back( 1 );
606 for( size_t i = 0; i < 3; i++ )
607 for( size_t x = 0; x < 2; x++ ) {
608 Gcl.clampVar( i, both, true );
609 BOOST_CHECK_EQUAL( Gcl.nrVars(), 3 );
610 BOOST_CHECK_EQUAL( Gcl.nrFactors(), 3 );
611 for( size_t j = 0; j < 3; j++ )
612 BOOST_CHECK_EQUAL( Gcl.factor(j), G.factor(j) );
613 Gcl.restoreFactors();
614 }
615
616 // clampFactor
617 for( size_t x = 0; x < 4; x++ ) {
618 Gcl.clampFactor( 0, std::vector<size_t>(1,x), true );
619 Factor mask( v01, 0.0 );
620 mask.set( x, 1.0 );
621 BOOST_CHECK_EQUAL( Gcl.factor(0), G.factor(0) * mask );
622 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(1) );
623 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(2) );
624 Gcl.restoreFactor( 0 );
625 }
626 for( size_t x = 0; x < 4; x++ ) {
627 Gcl.clampFactor( 1, std::vector<size_t>(1,x), true );
628 Factor mask( v12, 0.0 );
629 mask.set( x, 1.0 );
630 BOOST_CHECK_EQUAL( Gcl.factor(0), G.factor(0) );
631 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(1) * mask );
632 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(2) );
633 Gcl.restoreFactor( 1 );
634 }
635 for( size_t x = 0; x < 2; x++ ) {
636 Gcl.clampFactor( 2, std::vector<size_t>(1,x), true );
637 Factor mask( v1, 0.0 );
638 mask.set( x, 1.0 );
639 BOOST_CHECK_EQUAL( Gcl.factor(0), G.factor(0) );
640 BOOST_CHECK_EQUAL( Gcl.factor(1), G.factor(1) );
641 BOOST_CHECK_EQUAL( Gcl.factor(2), G.factor(2) * mask );
642 Gcl.restoreFactors();
643 }
644
645 // makeCavity
646 FactorGraph Gcav( G );
647 Gcav.makeCavity( 0, true );
648 BOOST_CHECK_EQUAL( Gcav.factor(0), Factor( v01, 1.0 ) );
649 BOOST_CHECK_EQUAL( Gcav.factor(1), G.factor(1) );
650 BOOST_CHECK_EQUAL( Gcav.factor(2), G.factor(2) );
651 Gcav.restoreFactors();
652 Gcav.makeCavity( 1, true );
653 BOOST_CHECK_EQUAL( Gcav.factor(0), Factor( v01, 1.0 ) );
654 BOOST_CHECK_EQUAL( Gcav.factor(1), Factor( v12, 1.0 ) );
655 BOOST_CHECK_EQUAL( Gcav.factor(2), Factor( v1, 1.0 ) );
656 Gcav.restoreFactors();
657 Gcav.makeCavity( 2, true );
658 BOOST_CHECK_EQUAL( Gcav.factor(0), G.factor(0) );
659 BOOST_CHECK_EQUAL( Gcav.factor(1), Factor( v12, 1.0 ) );
660 BOOST_CHECK_EQUAL( Gcav.factor(2), G.factor(2) );
661 Gcav.restoreFactors();
662 }
663
664
665 BOOST_AUTO_TEST_CASE( IOTest ) {
666 Var v0( 0, 2 );
667 Var v1( 1, 2 );
668 Var v2( 2, 2 );
669 VarSet v01( v0, v1 );
670 VarSet v02( v0, v2 );
671 VarSet v12( v1, v2 );
672 VarSet v012 = v01 | v2;
673
674 std::vector<Factor> facs;
675 facs.push_back( Factor( v01 ).randomize() );
676 facs.push_back( Factor( v12 ).randomize() );
677 facs.push_back( Factor( v1 ).randomize() );
678 std::vector<Var> vars;
679 vars.push_back( v0 );
680 vars.push_back( v1 );
681 vars.push_back( v2 );
682
683 FactorGraph G( facs );
684
685 G.WriteToFile( "factorgraph_test.fg" );
686 FactorGraph G2;
687 G2.ReadFromFile( "factorgraph_test.fg" );
688
689 BOOST_CHECK( G.vars() == G2.vars() );
690 BOOST_CHECK( G.bipGraph() == G2.bipGraph() );
691 BOOST_CHECK_EQUAL( G.nrFactors(), G2.nrFactors() );
692 for( size_t I = 0; I < G.nrFactors(); I++ ) {
693 BOOST_CHECK( G.factor(I).vars() == G2.factor(I).vars() );
694 for( size_t s = 0; s < G.factor(I).nrStates(); s++ )
695 BOOST_CHECK_CLOSE( G.factor(I)[s], G2.factor(I)[s], tol );
696 }
697
698 std::stringstream ss;
699 std::string s;
700 G.printDot( ss );
701 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "graph FactorGraph {" );
702 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "node[shape=circle,width=0.4,fixedsize=true];" );
703 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv0;" );
704 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1;" );
705 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv2;" );
706 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "node[shape=box,width=0.3,height=0.3,fixedsize=true];" );
707 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tf0;" );
708 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tf1;" );
709 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tf2;" );
710 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv0 -- f0;" );
711 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1 -- f0;" );
712 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1 -- f1;" );
713 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv1 -- f2;" );
714 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "\tv2 -- f1;" );
715 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "}" );
716
717 G.setFactor( 0, Factor( G.factor(0).vars(), 1.0 ) );
718 G.setFactor( 1, Factor( G.factor(1).vars(), 2.0 ) );
719 G.setFactor( 2, Factor( G.factor(2).vars(), 3.0 ) );
720 ss << G;
721 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "3" );
722 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "" );
723 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2" );
724 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 1 " );
725 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 2 " );
726 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "4" );
727 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 1" );
728 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 1" );
729 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 1" );
730 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "3 1" );
731 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "" );
732 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2" );
733 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 2 " );
734 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 2 " );
735 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "4" );
736 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 2" );
737 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 2" );
738 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 2" );
739 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "3 2" );
740 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "" );
741 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1" );
742 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 " );
743 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2 " );
744 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "2" );
745 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "0 3" );
746 std::getline( ss, s ); BOOST_CHECK_EQUAL( s, "1 3" );
747
748 ss << G;
749 FactorGraph G3;
750 ss >> G3;
751 BOOST_CHECK( G.vars() == G3.vars() );
752 BOOST_CHECK( G.bipGraph() == G3.bipGraph() );
753 BOOST_CHECK_EQUAL( G.nrFactors(), G3.nrFactors() );
754 for( size_t I = 0; I < G.nrFactors(); I++ ) {
755 BOOST_CHECK( G.factor(I).vars() == G3.factor(I).vars() );
756 for( size_t s = 0; s < G.factor(I).nrStates(); s++ )
757 BOOST_CHECK_CLOSE( G.factor(I)[s], G3.factor(I)[s], tol );
758 }
759 }