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