10 #ifndef IncrementalBTS_h
11 #define IncrementalBTS_h
22 template <
class state,
class action>
28 std::vector<state> &thePath);
30 std::vector<state> &thePath);
32 std::vector<action> &thePath);
49 return search.back().currState;
56 { p.clear();
for (
auto &i :
search) p.push_back(i.currState); }
60 const uint64_t
c1 = 2;
61 const uint64_t
c2 = 8;
113 std::vector<std::pair<state, double>>
history;
126 template <
class state,
class action>
130 if (InitializeSearch(e, from, to, h, thePath))
132 while (!DoSingleSearchStep(thePath))
136 template <
class state,
class action>
138 std::vector<action> &thePath)
143 template <
class state,
class action>
145 std::vector<state> &thePath)
151 thePath.push_back(from);
160 solutionPath.clear();
163 data.nodesExpanded = 0;
166 data.solutionInterval.lowerBound = h->
HCost(start, goal);
167 data.solutionInterval.upperBound = DBL_MAX;
171 solutionCost = DBL_MAX;
172 SetupIteration(h->
HCost(start, goal));
173 stage =
"IDA* ITERATION";
178 template <
class state,
class action>
182 search.back().currState = start;
183 search.back().status = kGoingDown;
184 search.back().pathCost = 0;
185 previousBound = bound;
188 printf(
"Starting iteration bound %1.1f\n", bound);
189 newNodesLastIteration = newNodeCount;
191 data.nodesExpanded = 0;
193 sd.f_above = DBL_MAX;
196 template <
class state,
class action>
199 if (env->GoalTest(search.back().currState, goal))
201 if (
fless(search.back().pathCost, solutionCost))
203 solutionPath.clear();
204 for (
size_t x = 0; x < search.size(); x++)
205 solutionPath.push_back(search[x].currState);
206 solutionCost = search.back().pathCost;
207 printf(
"Found solution cost %f!", solutionCost);
209 if (!
flesseq(solutionCost, data.solutionInterval.lowerBound))
214 if (search.back().status == kGoingDown)
216 double f = search.back().pathCost+h->HCost(search.back().currState, goal);
221 sd.f_above =
std::min(sd.f_above, f);
224 else if (
fless(f, nextBound))
236 sd.f_below =
std::max(sd.f_below, f);
244 search.back().status = kGoingAcross;
245 env->GetSuccessors(search.back().currState, search.back().succ);
247 for (
size_t x = 0; x < search.back().succ.size(); x++)
249 if (search.size() > 1 && search.back().succ[x] == search[search.size()-2].currState)
251 search.back().succ.erase(search.back().succ.begin()+x);
257 std::reverse(search.back().succ.begin(), search.back().succ.end());
261 if (search.back().status == kGoingAcross)
264 if (search.back().succ.size() == 0)
273 search.resize(search.size()+1);
274 auto &s = search[search.size()-2];
275 search.back().currState = s.succ.back();
276 search.back().status = kGoingDown;
277 search.back().pathCost = s.pathCost+env->GCost(s.currState, s.succ.back());
287 template <
class state,
class action>
290 if (
flesseq(solutionCost, data.solutionInterval.lowerBound))
292 thePath = solutionPath;
293 printf(
"Found solution cost %1.5f\n", solutionCost);
297 if (!IterationComplete() && data.nodesExpanded < data.workBound)
299 uint64_t tmp = nodesExpanded;
301 data.nodesExpanded += nodesExpanded-tmp;
302 if (IterationComplete())
305 stage =
"EXP Iter Complete";
313 if (data.nodesExpanded >= data.workBound)
318 else if (solutionCost != DBL_MAX &&
fgreatereq(sd.f_below, solutionCost))
327 data.solutionInterval &= v;
329 printf(
"--> Iteration complete - %" PRId64
" expanded; target [%" PRId64
", %" PRId64
")\n", data.nodesExpanded, c1*data.nodeLB, c2*data.nodeLB);
333 printf(
"Expanded %" PRId64
" - needed at least %" PRId64
"\n", data.nodesExpanded, c1*data.nodeLB);
334 if (data.solutionInterval.lowerBound == DBL_MAX)
335 printf(
"[HIT]--Critical f in [%1.5f, %1.5f]\n", solutionCost, solutionCost);
337 printf(
"[HIT]--Critical f in [%1.5f, ∞]\n", data.solutionInterval.lowerBound);
338 data.nodeLB = data.nodesExpanded;
339 data.solutionInterval.upperBound = DBL_MAX;
340 data.nodesExpanded = 0;
342 SetupIteration(nextBound);
348 (
fequal(data.solutionInterval.upperBound, data.solutionInterval.lowerBound) ||
349 (data.nodesExpanded >= c1*data.nodeLB && data.nodesExpanded < c2*data.nodeLB)
352 if (data.solutionInterval.upperBound == DBL_MAX)
353 printf(
" ]--Critical f in [%1.5f, ∞]\n", data.solutionInterval.lowerBound);
355 printf(
" ]--Critical f in [%1.5f, %1.5f]\n", data.solutionInterval.lowerBound, data.solutionInterval.upperBound);
358 if (data.solutionInterval.upperBound == DBL_MAX)
360 nextCost = data.solutionInterval.lowerBound+pow(gamma, data.delta);
366 nextCost = (data.solutionInterval.lowerBound+data.solutionInterval.upperBound)/2.0;
372 data.workBound = c2*data.nodeLB;
373 SetupIteration(nextCost);
374 printf(
"-> Starting new iteration with f: %f; node limit: %" PRId64
"\n", nextCost, data.workBound);
379 if (data.solutionInterval.lowerBound == DBL_MAX)
380 printf(
"[HIT]--Critical f in [∞, ∞]\n");
382 printf(
"[HIT]--Critical f in [%1.5f, ∞]\n", data.solutionInterval.lowerBound);
384 data.nodeLB =
std::max(data.nodesExpanded, c1*data.nodeLB);
385 data.solutionInterval.upperBound = DBL_MAX;
387 data.nodesExpanded = 0;
390 SetupIteration(nextBound);
391 stage =
"NEW ITERATION";
395 template <
class state,
class action>
398 for (
size_t x = 1; x < search.size(); x++)
400 env->DrawLine(display, search[x-1].currState, search[x].currState, 10);
404 template <
class state,
class action>
409 for (
size_t x = 1; x < search.size(); x++)
410 env->GLDrawLine(search[x-1].currState, search[x].currState, 10);