Multiple changes: changes in build system, one workaround and one bug fix
[libdai.git] / tests / testbbp.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <iostream>
10 #include <dai/alldai.h>
11
12
13 using namespace dai;
14 using namespace std;
15
16
17 int main( int argc, char *argv[] ) {
18 #ifdef DAI_WITH_CBP
19 if ( argc != 2 ) {
20 cout << "Usage: " << argv[0] << " <filename.fg>" << endl << endl;
21 cout << "Reads factor graph <filename.fg> and verifies" << endl;
22 cout << "whether BBP works correctly on it." << endl << endl;
23 return 1;
24 } else {
25 // Read FactorGraph from the file specified by the first command line argument
26 FactorGraph fg;
27 fg.ReadFromFile(argv[1]);
28
29 // Set some constants
30 size_t verbose = 0;
31 Real tol = 1.0e-9;
32 size_t maxiter = 10000;
33 Real damping = 0.0;
34
35 // Store the constants in a PropertySet object
36 PropertySet opts;
37 opts.set("verbose",verbose); // Verbosity (amount of output generated)
38 opts.set("tol",tol); // Tolerance for convergence
39 opts.set("maxiter",maxiter); // Maximum number of iterations
40 opts.set("damping",damping); // Amount of damping applied
41
42 // Construct a BP (belief propagation) object from the FactorGraph fg
43 BP bp(fg, opts("updates",string("SEQFIX"))("logdomain",false));
44 bp.recordSentMessages = true;
45 bp.init();
46 bp.run();
47
48 vector<size_t> state( fg.nrVars(), 0 );
49
50 for( size_t t = 0; t < 45; t++ ) {
51 BBP::Properties::UpdateType updates;
52 switch( t % 5 ) {
53 case BBP::Properties::UpdateType::SEQ_FIX:
54 updates = BBP::Properties::UpdateType::SEQ_FIX;
55 break;
56 case BBP::Properties::UpdateType::SEQ_MAX:
57 updates = BBP::Properties::UpdateType::SEQ_MAX;
58 break;
59 case BBP::Properties::UpdateType::SEQ_BP_REV:
60 updates = BBP::Properties::UpdateType::SEQ_BP_REV;
61 break;
62 case BBP::Properties::UpdateType::SEQ_BP_FWD:
63 updates = BBP::Properties::UpdateType::SEQ_BP_FWD;
64 break;
65 case BBP::Properties::UpdateType::PAR:
66 updates = BBP::Properties::UpdateType::PAR;
67 break;
68 }
69 BBPCostFunction cfn;
70 switch( (t / 5) % 9 ) {
71 case 0:
72 cfn = BBPCostFunction::CFN_GIBBS_B;
73 break;
74 case 1:
75 cfn = BBPCostFunction::CFN_GIBBS_B2;
76 break;
77 case 2:
78 cfn = BBPCostFunction::CFN_GIBBS_EXP;
79 break;
80 case 3:
81 cfn = BBPCostFunction::CFN_GIBBS_B_FACTOR;
82 break;
83 case 4:
84 cfn = BBPCostFunction::CFN_GIBBS_B2_FACTOR;
85 break;
86 case 5:
87 cfn = BBPCostFunction::CFN_GIBBS_EXP_FACTOR;
88 break;
89 case 6:
90 cfn = BBPCostFunction::CFN_VAR_ENT;
91 break;
92 case 7:
93 cfn = BBPCostFunction::CFN_FACTOR_ENT;
94 break;
95 case 8:
96 cfn = BBPCostFunction::CFN_BETHE_ENT;
97 break;
98 }
99
100 Real h = 1e-4;
101 Real result = numericBBPTest( bp, &state, opts("updates",updates), cfn, h );
102 cout << "result: " << result << ",\tupdates=" << updates << ", cfn=" << cfn << endl;
103 }
104 }
105
106 return 0;
107 #else
108 cout << "libDAI is configured not to include CBP (this can be changed in include/dai/dai_config.h)." << endl;
109 #endif
110 }