1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 This file is part of libDAI.
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <dai/alldai.h> // Include main libDAI header file
31 int main( int argc
, char *argv
[] ) {
33 cout
<< "Usage: " << argv
[0] << " <filename.fg>" << endl
<< endl
;
34 cout
<< "Reads factor graph <filename.fg> and runs" << endl
;
35 cout
<< "Belief Propagation and JunctionTree on it." << endl
<< endl
;
38 // Read FactorGraph from the file specified by the first command line argument
40 fg
.ReadFromFile(argv
[1]);
43 size_t maxiter
= 10000;
47 // Store the constants in a PropertySet object
49 opts
.Set("maxiter",maxiter
); // Maximum number of iterations
50 opts
.Set("tol",tol
); // Tolerance for convergence
51 opts
.Set("verbose",verb
); // Verbosity (amount of output generated)
53 // Construct a JTree (junction tree) object from the FactorGraph fg
54 // using the parameters specified by opts and an additional property
55 // that specifies the type of updates the JTree algorithm should perform
56 JTree
jt( fg
, opts("updates",string("HUGIN")) );
57 // Initialize junction tree algoritm
59 // Run junction tree algorithm
62 // Construct a BP (belief propagation) object from the FactorGraph fg
63 // using the parameters specified by opts and two additional properties,
64 // specifying the type of updates the BP algorithm should perform and
65 // whether they should be done in the real or in the logdomain
66 BP
bp(fg
, opts("updates",string("SEQFIX"))("logdomain",false));
67 // Initialize belief propagation algorithm
69 // Run belief propagation algorithm
72 // Report single-variable marginals for fg, calculated by the junction tree algorithm
73 cout
<< "Exact single-variable marginals:" << endl
;
74 for( size_t i
= 0; i
< fg
.nrVars(); i
++ ) // iterate over all variables in fg
75 cout
<< jt
.belief(fg
.var(i
)) << endl
; // display the "belief" of jt for that variable
77 // Report single-variable marginals for fg, calculated by the belief propagation algorithm
78 cout
<< "Approximate (loopy belief propagation) single-variable marginals:" << endl
;
79 for( size_t i
= 0; i
< fg
.nrVars(); i
++ ) // iterate over all variables in fg
80 cout
<< bp
.belief(fg
.var(i
)) << endl
; // display the belief of bp for that variable
82 // Report factor marginals for fg, calculated by the junction tree algorithm
83 cout
<< "Exact factor marginals:" << endl
;
84 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ ) // iterate over all factors in fg
85 cout
<< jt
.belief(fg
.factor(I
).vars()) << endl
; // display the "belief" of jt for the variables in that factor
87 // Report factor marginals for fg, calculated by the belief propagation algorithm
88 cout
<< "Approximate (loopy belief propagation) factor marginals:" << endl
;
89 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ ) // iterate over all factors in fg
90 cout
<< bp
.belief(fg
.factor(I
).vars()) << endl
; // display the belief of bp for the variables in that factor
92 // Report log partition sum (normalizing constant) of fg, calculated by the junction tree algorithm
93 cout
<< "Exact log partition sum: " << jt
.logZ() << endl
;
95 // Report log partition sum of fg, approximated by the belief propagation algorithm
96 cout
<< "Approximate (loopy belief propagation) log partition sum: " << bp
.logZ() << endl
;