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