-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtreebuilder.cpp
309 lines (249 loc) · 8.8 KB
/
treebuilder.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "treebuilder.hh"
#include "globalhelper.hh"
#include "libs/perf_helper.hh"
#include "readingQueue.hh"
#include "nodetree.hh"
#include <cassert>
TreeBuilder::TreeBuilder(Execution* exec, QObject* parent)
: QThread(parent),
execution(*exec),
_data{execution.getData()},
_na(execution.nodeTree().getNA()) {
read_queue.reset(new ReadingQueue(_data.getEntries()));
}
TreeBuilder::~TreeBuilder() {}
bool TreeBuilder::processRoot(DbEntry& dbEntry) {
QMutexLocker locker(&execution.getTreeMutex());
QMutexLocker layoutLocker(&execution.getLayoutMutex());
Statistics& stats = execution.getStatistics();
stats.choices++;
stats.undetermined += dbEntry.numberOfKids;
// can be a real root, or one of initial nodes in restarts
VisualNode* root = nullptr;
if (execution.isRestarts()) {
#ifdef MAXIM_DEBUG
qDebug() << "creating restart root";
#endif
// create a node for a new root
int restart_root = (_na)[0]->addChild(_na);
root = (_na)[restart_root];
// The "super root" now has an extra child, so its children
// haven't been laid out yet.
(_na)[0]->setChildrenLayoutDone(false);
(_na)[0]->setHasOpenChildren(true);
// The "super root" is effectively a branch node.
(_na)[0]->setStatus(BRANCH);
dbEntry.gid = restart_root;
dbEntry.depth = 2;
} else {
root = (_na)[0]; // use the root that is already there
if (root->getStatus() != UNDETERMINED) {
std::cout << dbEntry << std::endl;
return true;
}
dbEntry.gid = 0;
dbEntry.depth = 1;
}
auto& gid2entry = _data.gid2entry;
gid2entry[dbEntry.gid] = &dbEntry;
/// setNumberOfChildren
root->setNumberOfChildren(dbEntry.numberOfKids, _na);
root->setStatus(BRANCH);
root->setHasSolvedChildren(false);
root->setHasOpenChildren(true);
root->dirtyUp(_na);
emit addedRoot();
emit addedNode();
return true;
}
bool TreeBuilder::processNode(DbEntry& dbEntry, bool is_delayed) {
QMutexLocker locker(&execution.getTreeMutex());
QMutexLocker layoutLocker(&execution.getLayoutMutex());
NodeUID p_uid = dbEntry.parentUID; /// parent ID as it comes from Solver
int alt = dbEntry.alt; /// which alternative the current node is
int nalt = dbEntry.numberOfKids; /// number of kids in current node
char status = dbEntry.status;
/// find out if node exists
auto pid_it = _data.uid2aid.find(p_uid);
if (pid_it == _data.uid2aid.end()) {
if (!is_delayed) {
read_queue->readLater(&dbEntry);
}
return false;
}
const DbEntry& parentEntry = *_data.getEntries().at(pid_it->second);
/// parent ID as it is in Node Allocator (Gist)
int parent_gid = parentEntry.gid;
/// put delayed also if parent node hasn't been processed yet:
if (parent_gid == -1) {
if (!is_delayed)
read_queue->readLater(&dbEntry);
else {
// qDebug() << "node already in the queue";
}
return false;
}
auto parent = (_na)[parent_gid];
if (!parent) {
#ifdef MAXIM_DEBUG
std::cerr << "can't parse node" << dbEntry;
#endif
ignored_entries.push_back(&dbEntry);
return false;
}
auto& stats = execution.getStatistics();
while (alt >= parent->getNumberOfChildren()) {
parent->addChild(_na);
stats.undetermined++;
}
auto& node = *parent->getChild(_na, alt);
/// Normal behaviour: insert into Undetermined
if (node.getStatus() == UNDETERMINED) {
stats.undetermined--;
int gid = node.getIndex(_na); // node ID as it is in Gist
/// fill in empty fields of dbEntry
dbEntry.gid = gid;
dbEntry.depth = parentEntry.depth + 1; /// parent's depth + 1
// For now, assume that the solver sends the decision level.
// // It is not always clear how to compute a node's decision
// // level with respect to its parent. For now, let us say that
// // the right-most branch is not a decision, and all other
// // branches are decisions.
// bool thisIsRightmost = (alt == parentEntry.numberOfKids - 1);
// dbEntry.decisionLevel =
// parentEntry.decisionLevel + (thisIsRightmost ? 0 : 1);
_data.gid2entry[gid] = &dbEntry;
stats.maxDepth = std::max(stats.maxDepth, static_cast<int>(dbEntry.depth));
/// NOTE: don't distinguish between -1 and 0
/// -1 is the default for Chuffed and 0 -- for Gecode
if (dbEntry.thread_id == -1) { dbEntry.thread_id = 0; }
node._tid = dbEntry.thread_id; /// TODO: tid should be in node's flags
node.setNumberOfChildren(nalt, _na);
switch (status) {
case FAILED: // 1
node.setHasOpenChildren(false);
node.setHasSolvedChildren(false);
node.setHasFailedChildren(true);
node.setStatus(FAILED);
parent->closeChild(_na, true, false);
stats.failures++;
break;
case SKIPPED: // 6
// check if node hasn't been explored by other thread
// (for now just check if failure node)
node.setHasOpenChildren(false);
node.setHasSolvedChildren(false);
node.setHasFailedChildren(true);
node.setStatus(SKIPPED);
parent->closeChild(_na, true, false);
stats.failures++;
break;
case SOLVED: // 0
node.setHasFailedChildren(false);
node.setHasSolvedChildren(true);
node.setHasOpenChildren(false);
node.setStatus(SOLVED);
parent->closeChild(_na, false, true);
stats.solutions++;
break;
case BRANCH: // 2
node.setHasOpenChildren(true);
node.setStatus(BRANCH);
stats.choices++;
stats.undetermined += nalt;
break;
default:
qDebug() << "need to handle this type of Node: " << status;
break;
}
node.dirtyUp(_na);
emit addedNode();
// std::cerr << "TreeBuilder::processNode, normal case\n";
} else {
/// Not normal cases:
/// 1. Branch Node or Failed node into Skipped
if (node.getStatus() == SKIPPED) {
switch (status) {
case FAILED: // 1
node.setHasOpenChildren(false);
node.setHasSolvedChildren(false);
node.setHasFailedChildren(true);
node.setStatus(FAILED);
parent->closeChild(_na, true, false);
stats.failures++;
break;
case BRANCH: // 2
node.setHasOpenChildren(true);
node.setStatus(BRANCH);
stats.choices++;
stats.undetermined += nalt;
break;
default:
qDebug() << "need to handle this type of Node: " << status;
assert(status != SOLVED);
break;
}
node.dirtyUp(_na);
emit addedNode();
// std::cerr << "TreeBuilder::processNode, not-normal case\n";
} else {
// assert(status == SKIPPED);
ignored_entries.push_back(&dbEntry);
/// sometimes branch wants to override branch
}
}
return true;
}
void TreeBuilder::run() {
perf_helper::Timer timer;
timer.begin();
QMutex& dataMutex = _data.dataMutex;
bool is_delayed;
while (true) {
dataMutex.lock();
/// check if done
if (!read_queue->canRead()) {
dataMutex.unlock();
if (_data.isDone()) {
break;
}
/// can't read, but receiving not done, waiting...
msleep(1);
continue;
}
/// ask queue for an entry, note: is_delayed gets assigned here
DbEntry* entry = read_queue->next(is_delayed);
bool isRoot = (entry->parentUID.nid == -1) ? true : false;
/// try to put node into the tree
bool success = isRoot ? processRoot(*entry) : processNode(*entry, is_delayed);
read_queue->update(success);
dataMutex.unlock();
}
emit doneBuilding(true);
auto elapsed_ms = timer.end();
std::cout << "Time elapsed: " << elapsed_ms << "ms\n";
if (GlobalParser::isSet(GlobalParser::test_option)) {
qDebug() << "test mode, terminate";
qApp->exit();
}
}