Finished a new release: libDAI v0.2.6
[libdai.git] / tests / testbbp.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2009 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #include <iostream>
12 #include <dai/alldai.h>
13 #include <dai/bbp.h>
14
15
16 using namespace dai;
17 using namespace std;
18
19
20 int main( int argc, char *argv[] ) {
21 if ( argc != 2 ) {
22 cout << "Usage: " << argv[0] << " <filename.fg>" << endl << endl;
23 cout << "Reads factor graph <filename.fg> and verifies" << endl;
24 cout << "whether BBP works correctly on it." << endl << endl;
25 return 1;
26 } else {
27 // Read FactorGraph from the file specified by the first command line argument
28 FactorGraph fg;
29 fg.ReadFromFile(argv[1]);
30
31 // Set some constants
32 size_t verbose = 0;
33 Real tol = 1.0e-9;
34 size_t maxiter = 10000;
35 Real damping = 0.0;
36 BBP::Properties::UpdateType updates = BBP::Properties::UpdateType::PAR;
37
38 // Store the constants in a PropertySet object
39 PropertySet opts;
40 opts.set("verbose",verbose); // Verbosity (amount of output generated)
41 opts.set("tol",tol); // Tolerance for convergence
42 opts.set("maxiter",maxiter); // Maximum number of iterations
43 opts.set("damping",damping); // Amount of damping applied
44
45 // Construct a BP (belief propagation) object from the FactorGraph fg
46 BP bp(fg, opts("updates",string("SEQFIX"))("logdomain",false));
47 bp.recordSentMessages = true;
48 bp.init();
49 bp.run();
50
51 vector<size_t> state( fg.nrVars(), 0 );
52
53 for( size_t t = 0; t < 45; t++ ) {
54 BBP::Properties::UpdateType updates;
55 switch( t % 5 ) {
56 case BBP::Properties::UpdateType::SEQ_FIX:
57 updates = BBP::Properties::UpdateType::SEQ_FIX;
58 break;
59 case BBP::Properties::UpdateType::SEQ_MAX:
60 updates = BBP::Properties::UpdateType::SEQ_MAX;
61 break;
62 case BBP::Properties::UpdateType::SEQ_BP_REV:
63 updates = BBP::Properties::UpdateType::SEQ_BP_REV;
64 break;
65 case BBP::Properties::UpdateType::SEQ_BP_FWD:
66 updates = BBP::Properties::UpdateType::SEQ_BP_FWD;
67 break;
68 case BBP::Properties::UpdateType::PAR:
69 updates = BBP::Properties::UpdateType::PAR;
70 break;
71 }
72 BBPCostFunction cfn;
73 switch( (t / 5) % 9 ) {
74 case 0:
75 cfn = BBPCostFunction::CFN_GIBBS_B;
76 break;
77 case 1:
78 cfn = BBPCostFunction::CFN_GIBBS_B2;
79 break;
80 case 2:
81 cfn = BBPCostFunction::CFN_GIBBS_EXP;
82 break;
83 case 3:
84 cfn = BBPCostFunction::CFN_GIBBS_B_FACTOR;
85 break;
86 case 4:
87 cfn = BBPCostFunction::CFN_GIBBS_B2_FACTOR;
88 break;
89 case 5:
90 cfn = BBPCostFunction::CFN_GIBBS_EXP_FACTOR;
91 break;
92 case 6:
93 cfn = BBPCostFunction::CFN_VAR_ENT;
94 break;
95 case 7:
96 cfn = BBPCostFunction::CFN_FACTOR_ENT;
97 break;
98 case 8:
99 cfn = BBPCostFunction::CFN_BETHE_ENT;
100 break;
101 }
102
103 Real h = 1e-4;
104 Real result = numericBBPTest( bp, &state, opts("updates",updates), cfn, h );
105 cout << "result: " << result << ",\tupdates=" << updates << ", cfn=" << cfn << endl;
106 }
107 }
108
109 return 0;
110 }