502d8693a6887c3be24e1a4755a60b2c37e96174
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
;
48 bool clean_updates
= false;
50 // Store the constants in a PropertySet object
52 opts
.Set("verbose",verbose
); // Verbosity (amount of output generated)
53 opts
.Set("tol",tol
); // Tolerance for convergence
54 opts
.Set("maxiter",maxiter
); // Maximum number of iterations
55 opts
.Set("damping",damping
); // Amount of damping applied
57 // Construct a BP (belief propagation) object from the FactorGraph fg
58 BP
bp(fg
, opts("updates",string("SEQFIX"))("logdomain",false));
59 bp
.recordSentMessages
= true;
63 vector
<size_t> state( fg
.nrVars(), 0 );
65 for( size_t t
= 0; t
< 90; t
++ ) {
66 clean_updates
= t
% 2;
67 BBP::Properties::UpdateType updates
;
68 switch( (t
/ 2) % 5 ) {
69 case BBP::Properties::UpdateType::SEQ_FIX
:
70 updates
= BBP::Properties::UpdateType::SEQ_FIX
;
72 case BBP::Properties::UpdateType::SEQ_MAX
:
73 updates
= BBP::Properties::UpdateType::SEQ_MAX
;
75 case BBP::Properties::UpdateType::SEQ_BP_REV
:
76 updates
= BBP::Properties::UpdateType::SEQ_BP_REV
;
78 case BBP::Properties::UpdateType::SEQ_BP_FWD
:
79 updates
= BBP::Properties::UpdateType::SEQ_BP_FWD
;
81 case BBP::Properties::UpdateType::PAR
:
82 updates
= BBP::Properties::UpdateType::PAR
;
86 switch( (t
/ 10) % 9 ) {
88 cfn
= bbp_cfn_t::cfn_gibbs_b
;
91 cfn
= bbp_cfn_t::cfn_gibbs_b2
;
94 cfn
= bbp_cfn_t::cfn_gibbs_exp
;
97 cfn
= bbp_cfn_t::cfn_gibbs_b_factor
;
100 cfn
= bbp_cfn_t::cfn_gibbs_b2_factor
;
103 cfn
= bbp_cfn_t::cfn_gibbs_exp_factor
;
106 cfn
= bbp_cfn_t::cfn_var_ent
;
109 cfn
= bbp_cfn_t::cfn_factor_ent
;
112 cfn
= bbp_cfn_t::cfn_bethe_ent
;
117 double result
= numericBBPTest( bp
, &state
, opts("updates",updates
)("clean_updates",clean_updates
), cfn
, h
);
118 cout
<< "clean_updates=" << clean_updates
<< ", updates=" << updates
<< ", cfn=" << cfn
<< ", result: " << result
<< endl
;