-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.cpp
113 lines (95 loc) · 3.09 KB
/
wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <dai/alldai.h>
using namespace dai;
#include <boost/lexical_cast.hpp>
#include <cassert>
#include <chrono>
#include <ctime>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
static FactorGraph fg;
static PropertySet opts;
static BP bp;
static map<int, bool> clamps;
void initBP() {
bp = BP(fg, opts);
for (const auto& clamp : clamps) {
int varIndex = clamp.first;
bool varValue = clamp.second;
bp.clamp(varIndex, varValue ? 1 : 0);
}
bp.init();
}
void queryVariable() {
int varIndex;
cin >> varIndex;
clog << __LOGSTR__ << "Q " << varIndex << endl;
// auto ans = bp.belief(fg.var(varIndex)).get(1);
auto ans = bp.newBelief(varIndex);
clog << __LOGSTR__ << "Returning " << ans << "." << endl;
cout << ans << endl;
}
void queryFactor() {
int factorIndex, valueIndex;
cin >> factorIndex >> valueIndex;
clog << __LOGSTR__ << "FQ " << factorIndex << " " << valueIndex << endl;
auto ans = bp.beliefF(factorIndex).get(valueIndex);
clog << __LOGSTR__ << "Returning " << ans << "." << endl;
cout << ans << endl;
}
void runBP() {
double tolerance;
size_t minIters, maxIters, histLength;
cin >> tolerance >> minIters >> maxIters >> histLength;
clog << __LOGSTR__ << "BP " << tolerance << " " << minIters << " " << maxIters << " " << histLength << endl;
double yetToConvergeFraction = bp.run(tolerance, minIters, maxIters, histLength);
cout << yetToConvergeFraction << endl;
}
void clamp() {
int varIndex;
string varValueStr;
cin >> varIndex >> varValueStr;
assert(varValueStr == "true" || varValueStr == "false");
clog << __LOGSTR__ << "O " << varIndex << " " << varValueStr << endl;
bool varValue = (varValueStr == "true");
clamps[varIndex] = varValue;
initBP();
cout << "O " << varIndex << " " << varValueStr << endl;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
cerr << __LOGSTR__ << "Insufficient number of arguments." << endl;
return 1;
}
char *factorGraphFileName = argv[1];
clog << __LOGSTR__ << "Hello!" << endl
<< "Bingo compiled on " << __DATE__ << " at " << __TIME__ << "." << endl;
fg.ReadFromFile(factorGraphFileName);
clog << __LOGSTR__ << "Finished reading factor graph." << endl;
opts.set("maxiter", static_cast<size_t>(10000000));
opts.set("tol", Real(1e-6));
opts.set("verb", static_cast<size_t>(1));
opts.set("updates", string("SEQRND")); // "SEQRND", or "PARALL", or "SEQFIX", or "SEQRNDPAR"
opts.set("logdomain", true);
initBP();
string cmdType;
while (cin >> cmdType) {
clog << __LOGSTR__ << "Read command " << cmdType << endl;
if (cmdType == "Q") {
queryVariable();
} else if (cmdType == "FQ") {
queryFactor();
} else if (cmdType == "BP") {
runBP();
} else if (cmdType == "O") {
clamp();
} else {
assert(cmdType == "NL");
cout << endl;
}
}
clog << __LOGSTR__ << "Bye!" << endl;
return 0;
}