c55296ea0eda78cbcdc7a2566216bce4a0db1294
[libdai.git] / tests / testem / testem.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) 2009 Charles Vaske [cvaske at soe dot ucsc dot edu]
8 * Copyright (C) 2009 University of California Santa Cruz
9 */
10
11
12 #include <iostream>
13 #include <fstream>
14 #include <string>
15
16 #include <dai/factorgraph.h>
17 #include <dai/evidence.h>
18 #include <dai/alldai.h>
19
20
21 using namespace std;
22 using namespace dai;
23
24
25 void usage( const string &msg ) {
26 cerr << msg << endl;
27 cerr << "Usage:" << endl;
28 cerr << " testem factorgraph.fg evidence.tab emconfig.em" << endl;
29 exit( 1 );
30 }
31
32
33 int main( int argc, char** argv ) {
34 if( argc != 4 )
35 usage("Incorrect number of arguments.");
36
37 FactorGraph fg;
38 fg.ReadFromFile( argv[1] );
39
40 PropertySet infprops;
41 infprops.set( "verbose", (size_t)1 );
42 infprops.set( "updates", string("HUGIN") );
43 InfAlg* inf = newInfAlg( "JTREE", fg, infprops );
44 inf->init();
45
46 Evidence e;
47 ifstream estream( argv[2] );
48 e.addEvidenceTabFile( estream, fg );
49
50 cout << "Number of samples: " << e.nrSamples() << endl;
51 for( Evidence::iterator ps = e.begin(); ps != e.end(); ps++ )
52 cout << "Sample #" << (ps - e.begin()) << " has " << ps->size() << " observations." << endl;
53
54 ifstream emstream( argv[3] );
55 EMAlg em(e, *inf, emstream);
56
57 while( !em.hasSatisfiedTermConditions() ) {
58 Real l = em.iterate();
59 cout << "Iteration " << em.Iterations() << " likelihood: " << l <<endl;
60 }
61
62 cout << endl << "Inferred Factor Graph:" << endl << "######################" << endl;
63 cout.precision(12);
64 cout << inf->fg();
65
66 delete inf;
67
68 return 0;
69 }