Fixed clean_updates bug in BBP by commenting out this feature
[libdai.git] / tests / testbbp.cpp
1 /* Copyright (C) 2009 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Max Planck Institute for Biological Cybernetics, Germany
3
4 This file is part of libDAI.
5
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.
10
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.
15
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
19 */
20
21
22 #include <iostream>
23 #include <dai/alldai.h>
24 #include <dai/bbp.h>
25
26
27 using namespace dai;
28 using namespace std;
29
30
31 int main( int argc, char *argv[] ) {
32 if ( argc != 2 ) {
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;
36 return 1;
37 } else {
38 // Read FactorGraph from the file specified by the first command line argument
39 FactorGraph fg;
40 fg.ReadFromFile(argv[1]);
41
42 // Set some constants
43 size_t verbose = 0;
44 double tol = 1.0e-9;
45 size_t maxiter = 10000;
46 double damping = 0.0;
47 BBP::Properties::UpdateType updates = BBP::Properties::UpdateType::PAR;
48
49 // Store the constants in a PropertySet object
50 PropertySet opts;
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
55
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;
59 bp.init();
60 bp.run();
61
62 vector<size_t> state( fg.nrVars(), 0 );
63
64 for( size_t t = 0; t < 45; t++ ) {
65 BBP::Properties::UpdateType updates;
66 switch( t % 5 ) {
67 case BBP::Properties::UpdateType::SEQ_FIX:
68 updates = BBP::Properties::UpdateType::SEQ_FIX;
69 break;
70 case BBP::Properties::UpdateType::SEQ_MAX:
71 updates = BBP::Properties::UpdateType::SEQ_MAX;
72 break;
73 case BBP::Properties::UpdateType::SEQ_BP_REV:
74 updates = BBP::Properties::UpdateType::SEQ_BP_REV;
75 break;
76 case BBP::Properties::UpdateType::SEQ_BP_FWD:
77 updates = BBP::Properties::UpdateType::SEQ_BP_FWD;
78 break;
79 case BBP::Properties::UpdateType::PAR:
80 updates = BBP::Properties::UpdateType::PAR;
81 break;
82 }
83 bbp_cfn_t cfn;
84 switch( (t / 5) % 9 ) {
85 case 0:
86 cfn = bbp_cfn_t::CFN_GIBBS_B;
87 break;
88 case 1:
89 cfn = bbp_cfn_t::CFN_GIBBS_B2;
90 break;
91 case 2:
92 cfn = bbp_cfn_t::CFN_GIBBS_EXP;
93 break;
94 case 3:
95 cfn = bbp_cfn_t::CFN_GIBBS_B_FACTOR;
96 break;
97 case 4:
98 cfn = bbp_cfn_t::CFN_GIBBS_B2_FACTOR;
99 break;
100 case 5:
101 cfn = bbp_cfn_t::CFN_GIBBS_EXP_FACTOR;
102 break;
103 case 6:
104 cfn = bbp_cfn_t::CFN_VAR_ENT;
105 break;
106 case 7:
107 cfn = bbp_cfn_t::CFN_FACTOR_ENT;
108 break;
109 case 8:
110 cfn = bbp_cfn_t::CFN_BETHE_ENT;
111 break;
112 }
113
114 double h = 1e-4;
115 double result = numericBBPTest( bp, &state, opts("updates",updates), cfn, h );
116 cout << "result: " << result << ",\tupdates=" << updates << ", cfn=" << cfn << endl;
117 }
118 }
119
120 return 0;
121 }