Renamed SampleData->Observation, changed Observation and Evidence
[libdai.git] / tests / testem / testem.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4
5 #include <dai/factorgraph.h>
6 #include <dai/evidence.h>
7 #include <dai/alldai.h>
8
9
10 using namespace std;
11 using namespace dai;
12
13
14 void usage( const string& msg ) {
15 cerr << msg << endl;
16 cerr << "Usage:" << endl;
17 cerr << " testem factorgraph.fg evidence.tab emconfig.em" << endl;
18 exit( 1 );
19 }
20
21
22 int main( int argc, char** argv ) {
23 if( argc != 4 )
24 usage("Incorrect number of arguments.");
25
26 FactorGraph fg;
27 ifstream fgstream( argv[1] );
28 fgstream >> fg;
29
30 PropertySet infprops;
31 infprops.Set( "verbose", (size_t)1 );
32 infprops.Set( "updates", string("HUGIN") );
33 InfAlg* inf = newInfAlg( "JTREE", fg, infprops );
34 inf->init();
35
36 Evidence e;
37 ifstream estream( argv[2] );
38 e.addEvidenceTabFile( estream, fg );
39
40 cout << "Number of samples: " << e.nrSamples() << endl;
41 Evidence::iterator ps = e.begin();
42 for( ; ps != e.end(); ps++ )
43 cout << "Sample #" << (ps - e.begin()) << " has " << ps->observations().size() << " observations." << endl;
44
45 ifstream emstream( argv[3] );
46 EMAlg em(e, *inf, emstream);
47
48 while( !em.hasSatisfiedTermConditions() ) {
49 Real l = em.iterate();
50 cout << "Iteration " << em.getCurrentIters() << " likelihood: " << l <<endl;
51 }
52
53 cout << endl << "Inferred Factor Graph:" << endl << "######################" << endl << inf->fg();
54
55 return 0;
56 }