10 #ifndef IncrementalBGS_h
11 #define IncrementalBGS_h
16 template <
class state,
class action>
22 std::vector<state> &thePath);
24 std::vector<state> &thePath);
26 std::vector<action> &thePath);
43 return q.Lookup(
q.Peek()).data;
55 const uint64_t
c1 = 2;
56 const uint64_t
c2 = 8;
108 std::vector<std::pair<state, double>>
history;
136 template <
class state,
class action>
140 if (InitializeSearch(e, from, to, h, thePath))
142 while (!DoSingleSearchStep(thePath))
146 template <
class state,
class action>
148 std::vector<action> &thePath)
153 template <
class state,
class action>
155 std::vector<state> &thePath)
161 thePath.push_back(from);
170 solutionPath.clear();
173 data.nodesExpanded = 0;
176 data.solutionInterval.lowerBound = h->
HCost(start, goal);
177 data.solutionInterval.upperBound = DBL_MAX;
180 solutionCost = DBL_MAX;
181 SetupIteration(h->
HCost(start, goal));
182 stage =
"NEW ITERATION";
186 template <
class state,
class action>
189 q.Reset(env->GetMaxHash());
191 q.AddOpenNode(start, env->GetStateHash(start), 0.0, 0.0);
197 previousBound = bound;
200 printf(
"Starting iteration bound %1.1f\n", bound);
201 newNodesLastIteration = newNodeCount;
203 data.nodesExpanded = 0;
205 sd.f_above = DBL_MAX;
208 template <
class state,
class action>
213 thePath.push_back(q.Lookup(
node).data);
215 }
while (q.Lookup(
node).parentID !=
node);
216 thePath.push_back(q.Lookup(
node).data);
219 template <
class state,
class action>
222 uint64_t nodeid = q.Close();
225 if (env->GoalTest(q.Lookup(nodeid).data, goal))
227 ExtractPathToStartFromID(nodeid, solutionPath);
228 std::reverse(solutionPath.begin(), solutionPath.end());
229 solutionCost = env->GetPathLength(solutionPath);
236 neighborID.resize(0);
237 neighborLoc.resize(0);
242 env->GetSuccessors(q.Lookup(nodeid).data, neighbors);
246 for (
unsigned int x = 0; x < neighbors.size(); x++)
249 neighborLoc.push_back(q.Lookup(env->GetStateHash(neighbors[x]), theID));
250 neighborID.push_back(theID);
251 edgeCosts.push_back(env->GCost(q.Lookup(nodeid).data, neighbors[x]));
255 for (
int x = 0; x < neighbors.size(); x++)
257 double childGCost = q.Lookup(nodeid).g+edgeCosts[x];
258 double childFCost = childGCost+h->HCost(neighbors[x], goal);
262 nextBound = childFCost;
263 else if (
fless(childFCost, nextBound))
264 nextBound = childFCost;
266 sd.f_above =
std::min(sd.f_above, childFCost);
270 switch (neighborLoc[x])
275 if (
fless(childGCost, q.Lookup(neighborID[x]).g))
277 q.Lookup(neighborID[x]).parentID = nodeid;
278 q.Lookup(neighborID[x]).g = childGCost;
279 q.KeyChanged(neighborID[x]);
284 q.AddOpenNode(neighbors[x],
285 env->GetStateHash(neighbors[x]),
367 template <
class state,
class action>
370 if (
flesseq(solutionCost, data.solutionInterval.lowerBound))
372 thePath = solutionPath;
373 printf(
"Found solution cost %1.5f\n", solutionCost);
377 if (!IterationComplete() && data.nodesExpanded < data.workBound)
379 uint64_t tmp = nodesExpanded;
381 data.nodesExpanded += nodesExpanded-tmp;
388 if (data.nodesExpanded >= data.workBound)
393 else if (solutionCost != DBL_MAX &&
fgreatereq(sd.f_below, solutionCost))
402 data.solutionInterval &= v;
404 printf(
"--> Iteration complete - %" PRId64
" expanded; target [%" PRId64
", %" PRId64
")\n", data.nodesExpanded, c1*data.nodeLB, c2*data.nodeLB);
408 printf(
"Expanded %" PRId64
" - needed at least %" PRId64
"\n", data.nodesExpanded, c1*data.nodeLB);
409 if (data.solutionInterval.lowerBound == DBL_MAX)
410 printf(
"[HIT]--Critical f in [%1.5f, %1.5f]\n", solutionCost, solutionCost);
412 printf(
"[HIT]--Critical f in [%1.5f, ∞]\n", data.solutionInterval.lowerBound);
413 data.nodeLB = data.nodesExpanded;
414 data.solutionInterval.upperBound = DBL_MAX;
415 data.nodesExpanded = 0;
417 SetupIteration(nextBound);
423 (
fequal(data.solutionInterval.upperBound, data.solutionInterval.lowerBound) ||
424 (data.nodesExpanded >= c1*data.nodeLB && data.nodesExpanded < c2*data.nodeLB)
427 if (data.solutionInterval.upperBound == DBL_MAX)
428 printf(
" ]--Critical f in [%1.5f, ∞]\n", data.solutionInterval.lowerBound);
430 printf(
" ]--Critical f in [%1.5f, %1.5f]\n", data.solutionInterval.lowerBound, data.solutionInterval.upperBound);
433 if (data.solutionInterval.upperBound == DBL_MAX)
435 nextCost = data.solutionInterval.lowerBound+pow(gamma, data.delta);
440 nextCost = (data.solutionInterval.lowerBound+data.solutionInterval.upperBound)/2.0;
445 data.workBound = c2*data.nodeLB;
446 SetupIteration(nextCost);
447 printf(
"-> Starting new iteration with f: %f; node limit: %" PRId64
"\n", nextCost, data.workBound);
452 if (data.solutionInterval.lowerBound == DBL_MAX)
453 printf(
"[HIT]--Critical f in [∞, ∞]\n");
455 printf(
"[HIT]--Critical f in [%1.5f, ∞]\n", data.solutionInterval.lowerBound);
457 data.nodeLB =
std::max(data.nodesExpanded, c1*data.nodeLB);
458 data.solutionInterval.upperBound = DBL_MAX;
460 data.nodesExpanded = 0;
463 SetupIteration(nextBound);
464 stage =
"NEW ITERATION";
468 template <
class state,
class action>
471 double transparency = 1.0;
476 if (q.OpenSize() > 0)
480 for (
unsigned int x = 0; x < q.size(); x++)
482 const auto &data = q.Lookat(x);
485 env->SetColor(1.0, 1.0, 0.0, transparency);
486 env->Draw(disp, data.
data);
488 else if ((data.where ==
kOpenList) && (data.reopened))
490 env->SetColor(0.0, 0.5, 0.5, transparency);
491 env->Draw(disp, data.
data);
495 env->SetColor(0.0, 1.0, 0.0, transparency);
496 env->Draw(disp, data.
data);
498 else if ((data.where ==
kClosedList) && (data.reopened))
500 env->SetColor(0.5, 0.0, 0.5, transparency);
501 env->Draw(disp, data.
data);
510 if (data.parentID == x)
511 env->SetColor(1.0, 0.5, 0.5, transparency);
513 env->SetColor(1.0, 0.0, 0.0, transparency);
515 env->Draw(disp, data.
data);
518 env->SetColor(1.0, 0.5, 1.0, 0.5);
519 env->Draw(disp, goal);}
521 template <
class state,
class action>