First working version of EM
[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 using namespace std;
10 using namespace dai;
11
12 void usage(const string& msg) {
13 cerr << msg << endl;
14 cerr << "Usage:" << endl;
15 cerr << " testem factorgraph.fg evidence.tab emconfig.em" << endl;
16 exit(1);
17 }
18
19 int main(int argc, char** argv) {
20 if (argc != 4) {
21 usage("Incorrect number of arguments.");
22 }
23
24 FactorGraph fg;
25 ifstream fgstream(argv[1]);
26 fgstream >> fg;
27
28 PropertySet infprops;
29 infprops.Set("verbose", (size_t)1);
30 infprops.Set("updates", string("HUGIN"));
31 InfAlg* inf = newInfAlg("JTREE", fg, infprops);
32 inf->init();
33
34 Evidence e;
35 ifstream estream(argv[2]);
36 e.addEvidenceTabFile(estream, fg);
37
38 cout << "Number of samples: " << e.nrSamples() << endl;
39 Evidence::iterator ps = e.begin();
40 for (; ps != e.end(); ps++) {
41 cout << "Sample " << ps->first << " has "
42 << ps->second.observations().size() << " observations." << endl;
43 }
44
45 ifstream emstream(argv[3]);
46 EMAlg em(e, *inf, emstream);
47
48 for (size_t i = 0; i < 10; ++i) {
49 Real l = em.iterate();
50 cout << "Iteration " << i << " likelihood: " << l <<endl;
51 }
52
53 return 0;
54 }