af2d26173845397aba076efa5374e1a1befdc8fa
1 /* Copyright (C) 2009 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Max Planck Institute for Biological Cybernetics, Germany
4 This file is part of libDAI.
6 libDAI 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.
11 libDAI 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.
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <dai/alldai.h>
31 int main( int argc
, char *argv
[] ) {
33 cout
<< "Usage: " << argv
[0] << " <filename.fg>" << endl
<< endl
;
34 cout
<< "Reads factor graph <filename.fg> and verifies" << endl
;
35 cout
<< "whether BBP works correctly on it." << endl
<< endl
;
38 // Read FactorGraph from the file specified by the first command line argument
40 fg
.ReadFromFile(argv
[1]);
45 size_t maxiter
= 10000;
47 BBP::Properties::UpdateType updates
= BBP::Properties::UpdateType::PAR
;
49 // Store the constants in a PropertySet object
51 opts
.Set("verbose",verbose
); // Verbosity (amount of output generated)
52 opts
.Set("tol",tol
); // Tolerance for convergence
53 opts
.Set("maxiter",maxiter
); // Maximum number of iterations
54 opts
.Set("damping",damping
); // Amount of damping applied
56 // Construct a BP (belief propagation) object from the FactorGraph fg
57 BP
bp(fg
, opts("updates",string("SEQFIX"))("logdomain",false));
58 bp
.recordSentMessages
= true;
62 vector
<size_t> state( fg
.nrVars(), 0 );
64 for( size_t t
= 0; t
< 45; t
++ ) {
65 BBP::Properties::UpdateType updates
;
67 case BBP::Properties::UpdateType::SEQ_FIX
:
68 updates
= BBP::Properties::UpdateType::SEQ_FIX
;
70 case BBP::Properties::UpdateType::SEQ_MAX
:
71 updates
= BBP::Properties::UpdateType::SEQ_MAX
;
73 case BBP::Properties::UpdateType::SEQ_BP_REV
:
74 updates
= BBP::Properties::UpdateType::SEQ_BP_REV
;
76 case BBP::Properties::UpdateType::SEQ_BP_FWD
:
77 updates
= BBP::Properties::UpdateType::SEQ_BP_FWD
;
79 case BBP::Properties::UpdateType::PAR
:
80 updates
= BBP::Properties::UpdateType::PAR
;
84 switch( (t
/ 5) % 9 ) {
86 cfn
= bbp_cfn_t::CFN_GIBBS_B
;
89 cfn
= bbp_cfn_t::CFN_GIBBS_B2
;
92 cfn
= bbp_cfn_t::CFN_GIBBS_EXP
;
95 cfn
= bbp_cfn_t::CFN_GIBBS_B_FACTOR
;
98 cfn
= bbp_cfn_t::CFN_GIBBS_B2_FACTOR
;
101 cfn
= bbp_cfn_t::CFN_GIBBS_EXP_FACTOR
;
104 cfn
= bbp_cfn_t::CFN_VAR_ENT
;
107 cfn
= bbp_cfn_t::CFN_FACTOR_ENT
;
110 cfn
= bbp_cfn_t::CFN_BETHE_ENT
;
115 double result
= numericBBPTest( bp
, &state
, opts("updates",updates
), cfn
, h
);
116 cout
<< "result: " << result
<< ",\tupdates=" << updates
<< ", cfn=" << cfn
<< endl
;