Wrote alldai.h/cpp unit tests
[libdai.git] / tests / unit / alldai.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/alldai.h>
15 #include <strstream>
16 #include <fstream>
17
18
19 using namespace dai;
20
21
22 const double tol = 1e-8;
23
24
25 #define BOOST_TEST_MODULE DAIAlgTest
26
27
28 #include <boost/test/unit_test.hpp>
29
30
31 BOOST_AUTO_TEST_CASE( newInfAlgTest ) {
32 Var v0( 0, 2 );
33 Var v1( 1, 2 );
34 Var v2( 2, 2 );
35 Var v3( 3, 2 );
36 VarSet v01( v0, v1 );
37 VarSet v02( v0, v2 );
38 VarSet v03( v0, v3 );
39 VarSet v12( v1, v2 );
40 VarSet v13( v1, v3 );
41 VarSet v23( v2, v3 );
42 std::vector<Factor> facs;
43 facs.push_back( createFactorIsing( v0, v1, 1.0 ) );
44 facs.push_back( createFactorIsing( v1, v2, 1.0 ) );
45 facs.push_back( createFactorIsing( v2, v3, 1.0 ) );
46 facs.push_back( createFactorIsing( v3, v0, 1.0 ) );
47 facs.push_back( createFactorIsing( v0, -1.0 ) );
48 facs.push_back( createFactorIsing( v1, -1.0 ) );
49 facs.push_back( createFactorIsing( v2, -1.0 ) );
50 facs.push_back( createFactorIsing( v3, 1.0 ) );
51 Factor joint = facs[0] * facs[1] * facs[2] * facs[3] * facs[4] * facs[5] * facs[6] * facs[7];
52 FactorGraph fg( facs );
53 VarSet vs = v01;
54
55 InfAlg* ia = newInfAlg( "EXACT", fg, PropertySet()("verbose",(size_t)0) );
56 ia->init();
57 ia->run();
58 BOOST_CHECK( dist( ia->belief( v01 ), joint.marginal( vs ), DISTTV ) < tol );
59 BOOST_CHECK( dist( calcMarginal( *ia, vs, true ), joint.marginal( vs ), DISTTV ) < tol );
60 delete ia;
61
62 ia = newInfAlgFromString( "EXACT[verbose=0]", fg );
63 ia->init();
64 ia->run();
65 BOOST_CHECK( dist( ia->belief( v01 ), joint.marginal( vs ), DISTTV ) < tol );
66 BOOST_CHECK( dist( calcMarginal( *ia, vs, true ), joint.marginal( vs ), DISTTV ) < tol );
67 delete ia;
68
69 std::map<std::string, std::string> aliases;
70 aliases["alias"] = "EXACT[verbose=1]";
71 ia = newInfAlgFromString( "alias[verbose=0]", fg, aliases );
72 ia->init();
73 ia->run();
74 BOOST_CHECK( dist( ia->belief( v01 ), joint.marginal( vs ), DISTTV ) < tol );
75 BOOST_CHECK( dist( calcMarginal( *ia, vs, true ), joint.marginal( vs ), DISTTV ) < tol );
76 delete ia;
77 }
78
79
80 BOOST_AUTO_TEST_CASE( parseTest ) {
81 std::pair<std::string, PropertySet> nameProps = parseNameProperties( "name" );
82 BOOST_CHECK_EQUAL( nameProps.first, "name" );
83 BOOST_CHECK_EQUAL( nameProps.second.size(), 0 );
84
85 nameProps = parseNameProperties( "name[]" );
86 BOOST_CHECK_EQUAL( nameProps.first, "name" );
87 BOOST_CHECK_EQUAL( nameProps.second.size(), 0 );
88
89 nameProps = parseNameProperties( "name[key1=value,key2=0.5]" );
90 BOOST_CHECK_EQUAL( nameProps.first, "name" );
91 BOOST_CHECK_EQUAL( nameProps.second.size(), 2 );
92 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
93 BOOST_CHECK( nameProps.second.hasKey( "key2" ) );
94 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "value" );
95 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key2"), "0.5" );
96
97 std::map<std::string, std::string> aliases;
98 aliases["alias"] = "name[key1=other]";
99
100 nameProps = parseNameProperties( "alias", aliases );
101 BOOST_CHECK_EQUAL( nameProps.first, "name" );
102 BOOST_CHECK_EQUAL( nameProps.second.size(), 1 );
103 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
104 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "other" );
105
106 nameProps = parseNameProperties( "alias[]", aliases );
107 BOOST_CHECK_EQUAL( nameProps.first, "name" );
108 BOOST_CHECK_EQUAL( nameProps.second.size(), 1 );
109 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
110 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "other" );
111
112 nameProps = parseNameProperties( "alias[key1=value,key2=0.5]", aliases );
113 BOOST_CHECK_EQUAL( nameProps.first, "name" );
114 BOOST_CHECK_EQUAL( nameProps.second.size(), 2 );
115 BOOST_CHECK( nameProps.second.hasKey( "key1" ) );
116 BOOST_CHECK( nameProps.second.hasKey( "key2" ) );
117 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key1"), "value" );
118 BOOST_CHECK_EQUAL( nameProps.second.getAs<std::string>("key2"), "0.5" );
119 }
120
121
122 BOOST_AUTO_TEST_CASE( readAliasFileTest ) {
123 std::ofstream outfile;
124 std::string filename( "alldai_test.aliases" );
125 outfile.open( filename.c_str() );
126 if( outfile.is_open() ) {
127 outfile << "alias:\tname[key1=other]" << std::endl;
128 outfile.close();
129 } else
130 DAI_THROWE(CANNOT_WRITE_FILE,"Cannot write to file " + std::string(filename));
131
132 std::map<std::string, std::string> aliases;
133 aliases["alias"] = "name[key1=other]";
134
135 std::map<std::string, std::string> aliases2 = readAliasesFile( filename );
136 BOOST_CHECK( aliases == aliases2 );
137 }