Initial commit of libDAI-0.2.1
[libdai.git] / example.cpp
1 /* Copyright 2006-2008 Joris Mooij
2 jorism@jorismooij.nl
3
4 This file is part of AI.
5
6 AI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 AI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with AI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #include <iostream>
23 #include "alldai.h"
24
25
26 using namespace std;
27
28
29 int main( int argc, char *argv[] ) {
30 if ( argc != 2 ) {
31 cout << "Usage: " << argv[0] << " <filename.fg>" << endl << endl;
32 cout << "Reads factor graph <filename.fg> and runs" << endl;
33 cout << "Belief Propagation and JunctionTree on it." << endl << endl;
34 return 1;
35 } else {
36 FactorGraph fg;
37
38 if( fg.ReadFromFile(argv[1]) ) {
39 cout << "Error reading " << argv[1] << endl;
40 return 2;
41 } else {
42 size_t maxiter = 1000;
43 double tol = 1e-9;
44 size_t verb = 1;
45
46 Properties opts;
47 opts.Set("maxiter",maxiter);
48 opts.Set("tol",tol);
49 opts.Set("verbose",verb);
50
51 JTree jt( fg, opts("updates",string("HUGIN")) );
52 jt.init();
53 jt.run();
54
55 cout << "Exact single node marginals:" << endl;
56 for( size_t i = 0; i < fg.nrVars(); i++ )
57 cout << jt.belief(fg.var(i)) << endl;
58
59 BP bp(fg, opts("updates",string("SEQMAX")));
60 bp.init();
61 bp.run();
62
63 cout << "Exact single node marginals:" << endl;
64 for( size_t i = 0; i < fg.nrVars(); i++ )
65 cout << bp.belief(fg.var(i)) << endl;
66 }
67 }
68
69 return 0;
70 }