Even more cleanup of BBP code
[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 bool clean_updates = false;
49
50 // Store the constants in a PropertySet object
51 PropertySet opts;
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
56
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;
60 bp.init();
61 bp.run();
62
63 vector<size_t> state( fg.nrVars(), 0 );
64
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;
71 break;
72 case BBP::Properties::UpdateType::SEQ_MAX:
73 updates = BBP::Properties::UpdateType::SEQ_MAX;
74 break;
75 case BBP::Properties::UpdateType::SEQ_BP_REV:
76 updates = BBP::Properties::UpdateType::SEQ_BP_REV;
77 break;
78 case BBP::Properties::UpdateType::SEQ_BP_FWD:
79 updates = BBP::Properties::UpdateType::SEQ_BP_FWD;
80 break;
81 case BBP::Properties::UpdateType::PAR:
82 updates = BBP::Properties::UpdateType::PAR;
83 break;
84 }
85 bbp_cfn_t cfn;
86 switch( (t / 10) % 9 ) {
87 case 0:
88 cfn = bbp_cfn_t::CFN_GIBBS_B;
89 break;
90 case 1:
91 cfn = bbp_cfn_t::CFN_GIBBS_B2;
92 break;
93 case 2:
94 cfn = bbp_cfn_t::CFN_GIBBS_EXP;
95 break;
96 case 3:
97 cfn = bbp_cfn_t::CFN_GIBBS_B_FACTOR;
98 break;
99 case 4:
100 cfn = bbp_cfn_t::CFN_GIBBS_B2_FACTOR;
101 break;
102 case 5:
103 cfn = bbp_cfn_t::CFN_GIBBS_EXP_FACTOR;
104 break;
105 case 6:
106 cfn = bbp_cfn_t::CFN_VAR_ENT;
107 break;
108 case 7:
109 cfn = bbp_cfn_t::CFN_FACTOR_ENT;
110 break;
111 case 8:
112 cfn = bbp_cfn_t::CFN_BETHE_ENT;
113 break;
114 }
115
116 double h = 1e-4;
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;
119 }
120 }
121
122 return 0;
123 }