Fixed two problems related to g++ 4.0.0 on Darwin 9.8.0
[libdai.git] / tests / unit / daialg_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/daialg.h>
12 #include <dai/alldai.h>
13 #include <strstream>
14
15
16 using namespace dai;
17
18
19 const double tol = 1e-8;
20
21
22 #define BOOST_TEST_MODULE DAIAlgTest
23
24
25 #include <boost/test/unit_test.hpp>
26
27
28 BOOST_AUTO_TEST_CASE( calcMarginalTest ) {
29 Var v0( 0, 2 );
30 Var v1( 1, 2 );
31 Var v2( 2, 2 );
32 Var v3( 3, 2 );
33 VarSet v01( v0, v1 );
34 VarSet v02( v0, v2 );
35 VarSet v03( v0, v3 );
36 VarSet v12( v1, v2 );
37 VarSet v13( v1, v3 );
38 VarSet v23( v2, v3 );
39 std::vector<Factor> facs;
40 facs.push_back( createFactorIsing( v0, v1, 1.0 ) );
41 facs.push_back( createFactorIsing( v1, v2, 1.0 ) );
42 facs.push_back( createFactorIsing( v2, v3, 1.0 ) );
43 facs.push_back( createFactorIsing( v3, v0, 1.0 ) );
44 facs.push_back( createFactorIsing( v0, -1.0 ) );
45 facs.push_back( createFactorIsing( v1, -1.0 ) );
46 facs.push_back( createFactorIsing( v2, -1.0 ) );
47 facs.push_back( createFactorIsing( v3, 1.0 ) );
48 Factor joint = facs[0] * facs[1] * facs[2] * facs[3] * facs[4] * facs[5] * facs[6] * facs[7];
49 FactorGraph fg( facs );
50 ExactInf ei( fg, PropertySet()("verbose",(size_t)0) );
51 ei.init();
52 ei.run();
53 VarSet vs;
54
55 vs = v0; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
56 vs = v1; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
57 vs = v2; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
58 vs = v3; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
59 vs = v01; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
60 vs = v02; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
61 vs = v03; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
62 vs = v12; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
63 vs = v13; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
64 vs = v23; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
65 vs = v01 | v2; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
66 vs = v01 | v3; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
67 vs = v02 | v3; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
68 vs = v12 | v3; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
69 vs = v01 | v23; BOOST_CHECK( dist( calcMarginal( ei, vs, false ), joint.marginal( vs ), DISTTV ) < tol );
70 }
71
72
73 BOOST_AUTO_TEST_CASE( calcPairBeliefsTest ) {
74 Var v0( 0, 2 );
75 Var v1( 1, 2 );
76 Var v2( 2, 2 );
77 Var v3( 3, 2 );
78 VarSet v01( v0, v1 );
79 VarSet v02( v0, v2 );
80 VarSet v03( v0, v3 );
81 VarSet v12( v1, v2 );
82 VarSet v13( v1, v3 );
83 VarSet v23( v2, v3 );
84 std::vector<Factor> facs;
85 facs.push_back( createFactorIsing( v0, v1, 1.0 ) );
86 facs.push_back( createFactorIsing( v1, v2, 1.0 ) );
87 facs.push_back( createFactorIsing( v2, v3, 1.0 ) );
88 facs.push_back( createFactorIsing( v3, v0, 1.0 ) );
89 facs.push_back( createFactorIsing( v0, -1.0 ) );
90 facs.push_back( createFactorIsing( v1, -1.0 ) );
91 facs.push_back( createFactorIsing( v2, -1.0 ) );
92 facs.push_back( createFactorIsing( v3, 1.0 ) );
93 Factor joint = facs[0] * facs[1] * facs[2] * facs[3] * facs[4] * facs[5] * facs[6] * facs[7];
94 FactorGraph fg( facs );
95 ExactInf ei( fg, PropertySet()("verbose",(size_t)0) );
96 ei.init();
97 ei.run();
98 VarSet vs;
99
100 std::vector<Factor> pb = calcPairBeliefs( ei, v01 | v23, false, false );
101 BOOST_CHECK( dist( pb[0], joint.marginal( v01 ), DISTTV ) < tol );
102 BOOST_CHECK( dist( pb[1], joint.marginal( v02 ), DISTTV ) < tol );
103 BOOST_CHECK( dist( pb[2], joint.marginal( v03 ), DISTTV ) < tol );
104 BOOST_CHECK( dist( pb[3], joint.marginal( v12 ), DISTTV ) < tol );
105 BOOST_CHECK( dist( pb[4], joint.marginal( v13 ), DISTTV ) < tol );
106 BOOST_CHECK( dist( pb[5], joint.marginal( v23 ), DISTTV ) < tol );
107
108 pb = calcPairBeliefs( ei, v01 | v23, false, true );
109 BOOST_CHECK( dist( pb[0], joint.marginal( v01 ), DISTTV ) < tol );
110 BOOST_CHECK( dist( pb[1], joint.marginal( v02 ), DISTTV ) < tol );
111 BOOST_CHECK( dist( pb[2], joint.marginal( v03 ), DISTTV ) < tol );
112 BOOST_CHECK( dist( pb[3], joint.marginal( v12 ), DISTTV ) < tol );
113 BOOST_CHECK( dist( pb[4], joint.marginal( v13 ), DISTTV ) < tol );
114 BOOST_CHECK( dist( pb[5], joint.marginal( v23 ), DISTTV ) < tol );
115 }