1d11eda3af1024ba212b4c41587732b813a06d3c
[libdai.git] / tests / unit / alldai_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/alldai.h>
12 #include <strstream>
13 #include <fstream>
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( newInfAlgTest ) {
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 VarSet vs = v01;
51
52 InfAlg* ia = newInfAlg( "EXACT", fg, PropertySet()("verbose",(size_t)0) );
53 ia->init();
54 ia->run();
55 BOOST_CHECK( dist( ia->belief( v01 ), joint.marginal( vs ), DISTTV ) < tol );
56 BOOST_CHECK( dist( calcMarginal( *ia, vs, true ), joint.marginal( vs ), DISTTV ) < tol );
57 delete ia;
58
59 ia = newInfAlgFromString( "EXACT[verbose=0]", fg );
60 ia->init();
61 ia->run();
62 BOOST_CHECK( dist( ia->belief( v01 ), joint.marginal( vs ), DISTTV ) < tol );
63 BOOST_CHECK( dist( calcMarginal( *ia, vs, true ), joint.marginal( vs ), DISTTV ) < tol );
64 delete ia;
65
66 std::map<std::string, std::string> aliases;
67 aliases["alias"] = "EXACT[verbose=1]";
68 ia = newInfAlgFromString( "alias[verbose=0]", fg, aliases );
69 ia->init();
70 ia->run();
71 BOOST_CHECK( dist( ia->belief( v01 ), joint.marginal( vs ), DISTTV ) < tol );
72 BOOST_CHECK( dist( calcMarginal( *ia, vs, true ), joint.marginal( vs ), DISTTV ) < tol );
73 delete ia;
74 }
75
76
77 BOOST_AUTO_TEST_CASE( parseTest ) {
78 std::pair<std::string, PropertySet> nameProps = parseNameProperties( "name" );
79 BOOST_CHECK_EQUAL( nameProps.first, "name" );
80 BOOST_CHECK_EQUAL( nameProps.second.size(), 0 );
81
82 nameProps = parseNameProperties( "name[]" );
83 BOOST_CHECK_EQUAL( nameProps.first, "name" );
84 BOOST_CHECK_EQUAL( nameProps.second.size(), 0 );
85
86 nameProps = parseNameProperties( "name[key1=value,key2=0.5]" );
87 BOOST_CHECK_EQUAL( nameProps.first, "name" );
88 BOOST_CHECK_EQUAL( nameProps.second.size(), 2 );
89 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
90 BOOST_CHECK( nameProps.second.hasKey( "key2" ) );
91 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "value" );
92 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key2"), "0.5" );
93
94 std::map<std::string, std::string> aliases;
95 aliases["alias"] = "name[key1=other]";
96
97 nameProps = parseNameProperties( "alias", aliases );
98 BOOST_CHECK_EQUAL( nameProps.first, "name" );
99 BOOST_CHECK_EQUAL( nameProps.second.size(), 1 );
100 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
101 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "other" );
102
103 nameProps = parseNameProperties( "alias[]", aliases );
104 BOOST_CHECK_EQUAL( nameProps.first, "name" );
105 BOOST_CHECK_EQUAL( nameProps.second.size(), 1 );
106 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
107 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "other" );
108
109 nameProps = parseNameProperties( "alias[key1=value,key2=0.5]", aliases );
110 BOOST_CHECK_EQUAL( nameProps.first, "name" );
111 BOOST_CHECK_EQUAL( nameProps.second.size(), 2 );
112 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
113 BOOST_CHECK( nameProps.second.hasKey( "key2" ) );
114 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "value" );
115 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key2"), "0.5" );
116 }
117
118
119 BOOST_AUTO_TEST_CASE( readAliasFileTest ) {
120 std::ofstream outfile;
121 std::string filename( "alldai_test.aliases" );
122 outfile.open( filename.c_str() );
123 if( outfile.is_open() ) {
124 outfile << "alias:\tname[key1=other]" << std::endl;
125 outfile.close();
126 } else
127 DAI_THROWE(CANNOT_WRITE_FILE,"Cannot write to file " + std::string(filename));
128
129 std::map<std::string, std::string> aliases;
130 aliases["alias"] = "name[key1=other]";
131
132 std::map<std::string, std::string> aliases2 = readAliasesFile( filename );
133 BOOST_CHECK( aliases == aliases2 );
134 }