-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathIBEX.h
More file actions
604 lines (528 loc) · 16.9 KB
/
IBEX.h
File metadata and controls
604 lines (528 loc) · 16.9 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//
// IBEX.h
// hog2
//
// This code contains an implementation of IBEX / BTS / BGS
// See also:
// https://webdocs.cs.ualberta.ca/~nathanst/papers/IBEX.pdf
// https://www.movingai.com/SAS/BTS/
//
#ifndef IBEX_h
#define IBEX_h
#include "vectorCache.h"
#include "AStarOpenClosed.h"
#ifndef DEBUG
#define DEBUG 0 // set debug mode
#endif
#if DEBUG
#define debug_print(fmt, ...) printf(fmt,__VA_ARGS__)
#else
#define debug_print(fmt, ...) do {} while (0)
#endif
namespace IBEX {
template <class state>
struct BFHSCompare {
bool operator()(const AStarOpenClosedData<state> &i1, const AStarOpenClosedData<state> &i2) const
{
return (fgreater(i1.g, i2.g));
}
};
const int infiniteWorkBound = -1;
template <class state, class action, class environment, bool DFS = true>
class IBEX {
public:
IBEX(uint64_t minGrow, uint64_t maxGrow, double growthRate, bool exponential)
:c1(minGrow), c2(maxGrow), gamma(growthRate), oracle(false), exponentialGrowth(exponential) {}
void GetPath(environment *env, state from, state to,
std::vector<action> &thePath);
void GetPath(environment *env, Heuristic<state> *heuristic, state from, state to,
std::vector<action> &thePath);
void Dovetail(environment *env, Heuristic<state> *heuristic, state from, state to,
std::vector<action> &thePath);
double RedoMinWork();
uint64_t GetNodesExpanded() { return totalNodesExpanded; }
uint64_t GetNodesTouched() { return totalNodesTouched; }
void ResetNodeCount() { totalNodesExpanded = totalNodesTouched = 0; }
private:
struct searchBounds {
double f_below; // value below the actual limit
double f_above; // value above the actual limit
uint64_t nodes; // nodes used to search
};
struct costInterval {
double lowerBound;
double upperBound;
costInterval &operator&=(const costInterval &i)
{
lowerBound = std::max(lowerBound, i.lowerBound);
upperBound = std::min(upperBound, i.upperBound);
return *this;
}
};
IBEX<state, action, environment, DFS>::costInterval LowLevelSearch(double costLimit, uint64_t nodeLimit, uint64_t &nodesUsed);
// Functions for DF Search
double GCost(const state &s1, const state &s2)
{ return env->GCost(s1, s2); }
double GCost(const state &s, const action &a)
{ return env->GCost(s, a); }
double HCost(const state &s)
{ return h->HCost(s, goal); }
IBEX<state, action, environment, DFS>::costInterval DFBNB(double costLimit, uint64_t nodeLimit, uint64_t &nodesUsed);
IBEX<state, action, environment, DFS>::searchBounds DFBNBHelper(state &currState, double pathCost, double costLimit,
searchBounds &sd, uint64_t nodeLimit, action forbidden);
// Functions for BFHS
IBEX<state, action, environment, DFS>::costInterval BFHS(double costLimit, uint64_t nodeLimit, uint64_t &nodesUsed);
void ExtractPathToStartFromID(uint64_t node, std::vector<state> &thePath);
uint64_t totalNodesExpanded, totalNodesTouched;
Heuristic<state> *h;
environment *env;
std::vector<action> solutionPath, currPath;
double solutionCost;
vectorCache<action> actCache;
state start, goal;
uint64_t c1, c2;
double gamma;
double dfsLowerBound;
bool oracle;
bool exponentialGrowth;
// Data for BFHS
AStarOpenClosed<state, BFHSCompare<state>> q;
std::vector<state> neighbors;
std::vector<uint64_t> neighborID;
std::vector<double> edgeCosts;
std::vector<dataLocation> neighborLoc;
std::vector<state> solutionStates;
};
template <class state, class action, class environment, bool DFS>
void IBEX<state, action, environment, DFS>::GetPath(environment *env, state from, state to,
std::vector<action> &thePath)
{
GetPath(env, env, from, to, thePath);
}
template <class state, class action, class environment, bool DFS>
void IBEX<state, action, environment, DFS>::GetPath(environment *env, Heuristic<state> *heuristic, state from, state to,
std::vector<action> &thePath)
{
this->env = env;
h = heuristic;
start = from;
goal = to;
solutionPath.clear();
solutionCost = DBL_MAX;
ResetNodeCount();
uint64_t nodeLB = 1;
costInterval solutionInterval;
uint64_t currentNodesUsed;
solutionInterval.lowerBound = HCost(from);
solutionInterval.upperBound = DBL_MAX;
while (fgreater(solutionCost, solutionInterval.lowerBound))
{
double delta = 1;
debug_print("IBEX: Base search: f: %1.5f, cost limit ∞, target [%" PRId64 ", %" PRId64 "]\n", solutionInterval.lowerBound, c1*nodeLB, c2*nodeLB);
dfsLowerBound = solutionInterval.lowerBound;
solutionInterval &= LowLevelSearch(solutionInterval.lowerBound, infiniteWorkBound, currentNodesUsed);
// Move to next iteration
if (currentNodesUsed >= c1*nodeLB)
{
nodeLB = currentNodesUsed;
solutionInterval.upperBound = DBL_MAX;
continue;
}
while (!(fequal(solutionInterval.upperBound, solutionInterval.lowerBound) ||
(currentNodesUsed >= c1*nodeLB && currentNodesUsed < c2*nodeLB)))
{
if (solutionInterval.upperBound == DBL_MAX)
{
debug_print(" ]--Critical f in [%1.5f, ∞]\n", solutionInterval.lowerBound);
}
else
{
debug_print(" ]--Critical f in [%1.5f, %1.5f]\n", solutionInterval.lowerBound, solutionInterval.upperBound);
}
double nextCost;
delta *= gamma;
if (solutionInterval.upperBound == DBL_MAX)
{
if (exponentialGrowth)
nextCost = solutionInterval.lowerBound+delta;
// nextCost = baseHCost+(solutionInterval.lowerBound-baseHCost) * gamma;
else
nextCost = solutionInterval.lowerBound * gamma;
}
else
nextCost = (solutionInterval.lowerBound+solutionInterval.upperBound)/2.0;
dfsLowerBound = solutionInterval.lowerBound;
solutionInterval &= LowLevelSearch(nextCost, c2*nodeLB, currentNodesUsed);
}
nodeLB = std::max(currentNodesUsed, c1*nodeLB);
solutionInterval.upperBound = DBL_MAX;
if (fequal(solutionInterval.lowerBound, solutionCost))
break;
}
thePath = solutionPath;
debug_print("Found solution cost %1.5f\n", solutionCost);
}
template <class state, class action, class environment, bool DFS>
typename IBEX<state, action, environment, DFS>::costInterval IBEX<state, action, environment, DFS>::DFBNB(double costLimit, uint64_t nodeLimit, uint64_t &nodesUsed)
{
state currState = start;
if (nodeLimit == infiniteWorkBound)
{
debug_print(" --+DFBnB f: %1.5f; nodes: ∞; ", costLimit);
}
else
{
debug_print(" --+DFBnB f: %1.5f; nodes: %" PRId64 "; ", costLimit, nodeLimit);
}
currPath.clear();
searchBounds sd;
sd.f_below = 0;
sd.f_above = DBL_MAX;
sd.nodes = 0;
action a;
sd = DFBNBHelper(currState, 0, costLimit, sd, nodeLimit, a);
totalNodesExpanded += sd.nodes;
costInterval v;
if (sd.nodes >= nodeLimit)
{
v.lowerBound = 0;
v.upperBound = sd.f_below;
}
else if (solutionCost != DBL_MAX && fgreatereq(sd.f_below, solutionCost))
{
v.lowerBound = solutionCost;
v.upperBound = solutionCost;
}
else {
v.lowerBound = sd.f_above;
v.upperBound = DBL_MAX;
assert(fgreater(sd.f_above, costLimit));
}
nodesUsed = sd.nodes;
if (v.upperBound == DBL_MAX)
{
debug_print("%" PRId64 " (new) %" PRId64 " (total), solution range: [%1.5f, ∞] ", nodesUsed, totalNodesExpanded, v.lowerBound);
}
else
{
debug_print("%" PRId64 " (new) %" PRId64 " (total), solution range: [%1.5f, %1.5f] ", nodesUsed, totalNodesExpanded, v.lowerBound, v.upperBound);
}
if (solutionCost != DBL_MAX)
{
debug_print("sol: %1.5f\n", solutionCost);
}
else
{
debug_print("\n",1);
}
return v;
//return sd;
}
template <class state, class action, class environment, bool DFS>
typename IBEX<state, action, environment, DFS>::searchBounds IBEX<state, action, environment, DFS>::DFBNBHelper(state &currState, double pathCost, double costLimit,
searchBounds &sd, uint64_t nodeLimit, action forbidden)
{
double currF = pathCost+HCost(currState);
// printf("-------->%f [%f]\n", currF, pathCost);
if (fequal(dfsLowerBound, solutionCost) && !oracle)
{
return sd;
}
if (fgreater(currF, costLimit))
{
sd.f_above = std::min(sd.f_above, currF);
return sd;
}
else if (fgreatereq(currF, solutionCost))
{
sd.f_below = solutionCost;
return sd;
}
else {
sd.f_below = std::max(currF, sd.f_below);
}
if (sd.nodes >= nodeLimit)
return sd;
if (env->GoalTest(currState, goal))
{
if (fless(currF, solutionCost))
{
solutionPath = currPath;
solutionCost = currF;
}
return sd;
}
std::vector<action> &acts = *actCache.getItem();
env->GetActions(currState, acts);
sd.nodes++;
totalNodesTouched+=acts.size();
for (size_t x = 0; x < acts.size(); x++)
{
if (acts[x] == forbidden && currPath.size() > 0)
continue;
double edgeCost = GCost(currState, acts[x]);
env->ApplyAction(currState, acts[x]);
currPath.push_back(acts[x]);
action rev = acts[x];
env->InvertAction(rev);
sd = DFBNBHelper(currState, pathCost+edgeCost, costLimit, sd, nodeLimit, rev);
currPath.pop_back();
env->UndoAction(currState, acts[x]);
}
actCache.returnItem(&acts);
return sd;
}
template <class state, class action, class environment, bool DFS>
double IBEX<state, action, environment, DFS>::RedoMinWork()
{
ResetNodeCount();
debug_print("IBEX Validation:\n",1);
oracle = true;
uint64_t nodesUsed;
LowLevelSearch(solutionCost, -1, nodesUsed);
oracle = false;
return solutionCost;
}
template <class state, class action, class environment, bool DFS>
typename IBEX<state, action, environment, DFS>::costInterval IBEX<state, action, environment, DFS>::BFHS(double costLimit, uint64_t nodeLimit, uint64_t &nodesUsed)
{
if (nodeLimit == -1ull && costLimit == DBL_MAX)
{
debug_print(" --+BFHS f: ∞; nodes: ∞; ",1);
}
else if (nodeLimit == -1)
{
debug_print(" --+BFHS f: %1.5f; nodes: ∞; ", costLimit);
}
else
{
debug_print(" --+BFHS f: %1.5f; nodes: %" PRId64 "; ", costLimit, nodeLimit);
}
searchBounds sd;
sd.f_below = 0;
sd.f_above = DBL_MAX;
sd.nodes = 0;
q.Reset(env->GetMaxHash());
// put start in open
q.AddOpenNode(start, env->GetStateHash(start), 0.0, 0.0, (uint64_t)0);
while (sd.nodes < nodeLimit && q.OpenSize() > 0)
{
// expand by low g until
// (1) we find the goal
// (2) we hit the node limit
// (3) we exhaust all states
uint64_t nodeid = q.Close();
sd.nodes++;
totalNodesExpanded++;
double nextf = q.Lookup(nodeid).g + HCost(q.Lookup(nodeid).data);
if (fgreater(nextf, costLimit))
{
// case shouldn't happen - pruned elsewhere
assert(false);
} else {
sd.f_below = std::max(sd.f_below, nextf);
}
if (env->GoalTest(q.Lookup(nodeid).data, goal))
{
solutionCost = q.Lookup(nodeid).g;
ExtractPathToStartFromID(nodeid, solutionStates);
// Path is backwards - reverse
reverse(solutionStates.begin(), solutionStates.end());
// f, nextF, failedF, nodes
for (int x = 0; x < solutionStates.size()-1; x++)
{
solutionPath.push_back(env->GetAction(solutionStates[x], solutionStates[x+1]));
}
// TODO: return range here
nodesUsed = sd.nodes;
return {q.Lookup(nodeid).g, q.Lookup(nodeid).g};
}
neighbors.resize(0);
edgeCosts.resize(0);
neighborID.resize(0);
neighborLoc.resize(0);
// std::cout << "Expanding: " << q.Lookup(nodeid).data << " with f:";
// std::cout << q.Lookup(nodeid).g << std::endl;
env->GetSuccessors(q.Lookup(nodeid).data, neighbors);
// 1. load all the children
for (size_t x = 0; x < neighbors.size(); x++)
{
uint64_t theID;
neighborLoc.push_back(q.Lookup(env->GetStateHash(neighbors[x]), theID));
neighborID.push_back(theID);
edgeCosts.push_back(GCost(q.Lookup(nodeid).data, neighbors[x]));
}
// iterate again updating costs and writing out to memory
for (size_t x = 0; x < neighbors.size(); x++)
{
totalNodesTouched++;
double childGCost = q.Lookup(nodeid).g+edgeCosts[x];
double childFCost = childGCost+HCost(neighbors[x]);
if (fgreater(childFCost, costLimit) || fgreatereq(childFCost, solutionCost))
{
sd.f_above = std::min(sd.f_above, childFCost);
continue;
}
switch (neighborLoc[x])
{
case kClosedList:
break;
case kOpenList:
if (fless(childGCost, q.Lookup(neighborID[x]).g))
{
q.Lookup(neighborID[x]).parentID = nodeid;
q.Lookup(neighborID[x]).g = childGCost;
q.KeyChanged(neighborID[x]);
}
break;
case kNotFound:
{
q.AddOpenNode(neighbors[x],
env->GetStateHash(neighbors[x]),
childGCost,
0.0,
nodeid);
}
}
}
}
// f, nextF, failedF, nodes
// TODO: return range here
costInterval v;
if (sd.nodes >= nodeLimit)
{
v.lowerBound = 0;
v.upperBound = sd.f_below;
}
else {
v.lowerBound = sd.f_above;
v.upperBound = DBL_MAX;
}
//v.nodes = sd.nodes;
nodesUsed = sd.nodes;
if (v.upperBound == DBL_MAX)
{
debug_print("%" PRId64 " (new) %" PRId64 " (total), solution range: [%1.5f, ∞]\n", nodesUsed, totalNodesExpanded, v.lowerBound);
}
else
{
debug_print("%" PRId64 " (new) %" PRId64 " (total), solution range: [%1.5f, %1.5f]\n", nodesUsed, totalNodesExpanded, v.lowerBound, v.upperBound);
}
return v;
}
template <class state, class action,class environment,bool DFS>
void IBEX<state, action, environment, DFS>::ExtractPathToStartFromID(uint64_t node,
std::vector<state> &thePath)
{
thePath.clear();
do {
thePath.push_back(q.Lookup(node).data);
node = q.Lookup(node).parentID;
} while (q.Lookup(node).parentID != node);
thePath.push_back(q.Lookup(node).data);
}
template <class state, class action, class environment, bool DFS>
typename IBEX<state, action, environment, DFS>::costInterval IBEX<state, action, environment, DFS>::LowLevelSearch(double costLimit, uint64_t nodeLimit, uint64_t &nodesUsed)
{
if (DFS)
return DFBNB(costLimit, nodeLimit, nodesUsed);
else
return BFHS(costLimit, nodeLimit, nodesUsed);
}
/* Node used by the UBS search */
class UBSNode {
public:
UBSNode(int k, int r) : r(r), k(k) {}
int r;
int k;
friend bool operator<(const UBSNode &n1, const UBSNode &n2) {return n1.T() > n2.T();}
int T() const {return r * pow(2, k);}
};
template <class state, class action, class environment, bool DFS>
void IBEX<state, action, environment, DFS>::Dovetail(environment *env, Heuristic<state> *heuristic, state from, state to,
std::vector<action> &thePath)
{
this->env = env;
h = heuristic;
start = from;
goal = to;
solutionPath.clear();
solutionCost = DBL_MAX;
ResetNodeCount();
//Graph &graph, int alpha, float gamma, Data &data)
int alpha=c2;
/* lookup table maps programs k to uppder bounds.
Lower bound is tracked globally */
std::map<int,double> lookup;
/* priority queue for storing the UBS nodes */
std::priority_queue<UBSNode> q;
q.push(UBSNode(1,1));
/* b_low is a lower bound on the optimal budget */
uint64_t b_low = 0;
double low = HCost(from);
while (!q.empty()) {
/* get top element from queue */
auto n = q.top();
q.pop();
int k = n.k;
int r = n.r;
/* set budget */
int b = pow(alpha,k);
/* update the UBS */
if (n.r == 1) {
q.push(UBSNode(n.k+1,1));
}
/* skip the query if the budget is known to be insufficient
to find a solution */
if (b <= b_low) {
continue;
}
/* check to see if upper bound has been found yet for this program */
if (lookup.find(k) == lookup.end()) {
lookup[k] = DBL_MAX;//std::numeric_limits<float>::max();
}
double high = lookup[k];
if (high != DBL_MAX)
{
debug_print("Running (k=%d, r=%d) with budget %d; [global] low = %f, [loca] high = %f\n", k, r, b, low, high);
}
else
{
debug_print("Running (k=%d, r=%d) with budget %d; [global] low = %f, [loca] high = ∞\n", k, r, b, low);
}
/* compute the cost threshhold for the query */
float C;
if (exponentialGrowth)
C = low + (1<<(r-1));
else
C = gamma * low; /* by default, in the binary search mode */
if (r == 1)
{ /* start with an infinite budget search */
C = low;
b = infiniteWorkBound;//std::numeric_limits<int>::max();
}
else if (high != DBL_MAX) { /* exponential search */
C = (low + high) / 2.0;
}
/* make the query */
uint64_t nodesUsed;
costInterval i = LowLevelSearch(C, b, nodesUsed);
/* perform the intersection */
lookup[k] = std::min(i.upperBound, high);
low = std::max(low, i.lowerBound);
/* the budget was sufficient and no solution found, so update the minimal budget */
if (i.upperBound == DBL_MAX) {
b_low = nodesUsed;
}
/* update the UBS */
q.push(UBSNode(n.k, n.r+1));
if (flesseq(solutionCost, low))
{
thePath = solutionPath;
debug_print("proven solution cost %f\n", solutionCost);
break;
}
}
}
}
#endif /* IBEX_h */