-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathml-stats.cpp
288 lines (264 loc) · 8.75 KB
/
ml-stats.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "ml-stats.hh"
#include "nodecursor.hh"
#include "nodevisitor.hh"
#include "data.hh"
#include <unordered_map>
#include <set>
// **************************************************
// StatsEntry
// **************************************************
using std::string;
class StatsEntry {
public:
unsigned int nodeid;
unsigned int gid;
int parentid;
NodeStatus status;
int alternative;
// int restartNumber;
int depth;
int decisionLevel;
string label;
int subtreeDepth;
int subtreeSize;
int subtreeSolutions;
int nogoodStringLength;
string nogoodString;
int nogoodLength;
int nogoodNumberVariables;
int nogoodBLD;
// bool usesAssumptions;
int backjumpDistance;
int backjumpDestination;
unsigned long long timestamp;
string solutionString;
};
#define INCLUDE_NOGOOD_STRING 1
void printStatsHeader(std::ostream& out = std::cout) {
out << "id"
<< "," << "gid"
<< "," << "parentId"
<< "," << "status"
<< "," << "alternative"
// << "," << "restartNumber"
<< "," << "depth"
<< "," << "decisionLevel"
<< "," << "label"
<< "," << "subtreeDepth"
<< "," << "subtreeSize"
<< "," << "subtreeSolutions"
<< "," << "nogoodStringLength"
#if INCLUDE_NOGOOD_STRING
<< "," << "nogoodString"
#endif
<< "," << "nogoodLength"
<< "," << "nogoodNumberVariables"
<< "," << "nogoodBLD"
// << "," << "usesAssumptions"
<< "," << "backjumpDistance"
<< "," << "backjumpDestination"
<< "," << "timestamp"
<< "," << "solutionString"
<< "\n";
}
string
csvquote(const string& input) {
std::stringstream out;
out << '"';
for (string::const_iterator it = input.begin() ; it != input.end() ; it++) {
// CSV quoting is to replace " with "", e.g.
// "one", "two", "I said, ""two,"" pay attention!"
if (*it == '"')
out << '"';
out << *it;
}
out << '"';
return out.str();
}
void printStatsEntry(const StatsEntry& se, std::ostream& out = std::cout) {
out << se.nodeid
<< "," << se.gid
<< "," << se.parentid
<< "," << se.status
<< "," << se.alternative
// << "," << se.restartNumber
<< "," << se.depth
<< "," << se.decisionLevel
<< "," << se.label
<< "," << se.subtreeDepth
<< "," << se.subtreeSize
<< "," << se.subtreeSolutions
<< "," << se.nogoodStringLength
#if INCLUDE_NOGOOD_STRING
<< "," << se.nogoodString
#endif
<< "," << se.nogoodLength
<< "," << se.nogoodNumberVariables
<< "," << se.nogoodBLD
// << "," << se.usesAssumptions
<< "," << se.backjumpDistance
<< "," << se.backjumpDestination
<< "," << se.timestamp
<< "," << csvquote(se.solutionString)
<< "\n";
}
// **************************************************
// StatsCursor
// **************************************************
class StatsCursor : public NodeCursor {
private:
Execution* execution;
int depth;
std::vector<StatsEntry> stack;
std::ostream& out;
public:
StatsCursor(VisualNode* root, const NodeAllocator& na, Execution* execution_,
std::ostream& out_)
: NodeCursor(root, na)
, execution(execution_)
, depth(0)
, out(out_)
{
printStatsHeader(out);
enter();
}
string getSolutionString(NodeUID uid) {
auto maybe_info = execution->getInfo(uid);
if (maybe_info != nullptr) {
return *maybe_info;
} else {
return "";
}
}
int calculateNogoodLength(string nogood) {
int count = 0;
for (unsigned int i = 0 ; i < nogood.size() ; i++) {
if (nogood[i] == ' ') {
count++;
}
}
return count;
}
int calculateNogoodNumberVariables(string nogood) {
std::set<string> variables;
int start = 0;
while (true) {
size_t space = nogood.find(' ', start);
string literalSubstring = nogood.substr(start, space - start);
if (literalSubstring.size() > 0) {
size_t punctuation = literalSubstring.find_first_of("<>=!");
if (punctuation != string::npos) {
string variableSubstring = literalSubstring.substr(0, punctuation);
variables.insert(variableSubstring);
}
}
if (space == string::npos)
break;
start = space+1;
}
return variables.size();
}
// What to do when we see a node for the first time: create a
// StatsEntry for it and add it to the stack.
void enter() {
StatsEntry se;
se.depth = depth;
se.subtreeDepth = 1;
se.status = node()->getStatus();
switch (se.status) {
case SKIPPED:
case UNDETERMINED:
se.subtreeSize = 0;
break;
default:
se.subtreeSize = 1;
break;
}
se.subtreeSolutions = se.status == SOLVED ? 1 : 0;
int gid = node()->getIndex(na);
// Some nodes (e.g. undetermined nodes) do not have entries;
// be careful with those.
se.gid = gid;
DbEntry* entry = execution->getEntry(gid);
if (entry != nullptr) {
auto nid = entry->nodeUID.nid;
se.nodeid = nid;
se.parentid = entry->parentUID.nid;
se.alternative = entry->alt;
// se.restartNumber = entry->restart_id;
se.nogoodStringLength = execution->getNogoodByUID(entry->nodeUID, true, false).length();
se.nogoodString = execution->getNogoodByUID(entry->nodeUID, true, false);
se.nogoodLength = calculateNogoodLength(se.nogoodString);
se.nogoodNumberVariables = calculateNogoodNumberVariables(se.nogoodString);
// se.nogoodBLD = entry->nogood_bld;
// se.usesAssumptions = entry->usesAssumptions;
// se.backjumpDistance = entry->backjump_distance;
// se.decisionLevel = entry->decision_level;
se.label = entry->label;
se.timestamp = entry->time_stamp;
se.solutionString = getSolutionString(entry->nodeUID);
se.backjumpDestination = se.decisionLevel - se.backjumpDistance;
} else {
se.nodeid = -1;
se.parentid = -1;
se.alternative = -1;
// se.restartNumber = -1;
se.nogoodStringLength = 0;
se.nogoodString = "";
se.nogoodLength = 0;
se.nogoodNumberVariables = 0;
se.label = "";
se.decisionLevel = -1;
se.timestamp = 0;
se.solutionString = "";
se.backjumpDestination = -1;
se.backjumpDistance = -1;
}
stack.push_back(se);
}
void moveDownwards() {
NodeCursor::moveDownwards();
depth++;
enter();
}
// What to do when we see a node for the second (last) time: pop
// its StatsEntry from the stack and update its parent's subtree
// information. This means that a node's subtree information is
// correct when its last child is done.
void processCurrentNode() {
StatsEntry se = stack.back();
stack.pop_back();
// Undetermined nodes are not real nodes (the solver never
// visited them), so we don't do anything with those.
// Also let's ignore the "super root".
if (se.status != UNDETERMINED && se.depth >= 1) {
printStatsEntry(se, out);
if (stack.size() > 0) {
stack.back().subtreeDepth = std::max(stack.back().subtreeDepth, 1 + se.subtreeDepth);
stack.back().subtreeSize += se.subtreeSize;
stack.back().subtreeSolutions += se.subtreeSolutions;
}
}
}
void moveUpwards() {
NodeCursor::moveUpwards();
depth--;
}
void moveSidewards() {
NodeCursor::moveSidewards();
enter();
}
};
// **************************************************
// Module interface
// **************************************************
// Collect the machine-learning statistics for a (sub)tree. The first
// argument are the root of the subtree and the node-allocator for the
// tree, as are the usual arguments to a NodeCursor. The third
// argument is the execution the subtree comes from, which is used to
// find the solver node id and branching/no-good information.
void collectMLStats(VisualNode* root, const NodeAllocator& na, Execution* execution, std::ostream& out) {
StatsCursor c(root, na, execution, out);
PostorderNodeVisitor<StatsCursor> v(c);
v.run();
}