16 template<
typename state>
36 template <
class state>
52 template <
class state,
class action,
class environment>
61 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
62 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & );
70 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
125 void SetPhi(std::function<
double(
double,
double)> p)
129 double Phi(
double h,
double g)
147 std::function<double(
double,
double)>
phi;
169 template <
class state,
class action,
class environment>
172 static char name[32];
173 sprintf(name,
"IOS[%1.2f, %1.2f]", bound, weight);
188 template <
class state,
class action,
class environment>
192 if (!InitializeSearch(_env, from, to, thePath))
196 while (!DoSingleSearchStep(thePath))
200 template <
class state,
class action,
class environment>
203 std::vector<state> thePath;
204 if (!InitializeSearch(_env, from, to, thePath))
209 while (!DoSingleSearchStep(thePath))
212 for (
int x = 0; x < thePath.size()-1; x++)
214 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
229 template <
class state,
class action,
class environment>
233 if (theHeuristic == 0)
240 bestSolution = DBL_MAX;
241 solutionReduction = 0;
243 internalPath.clear();
244 openClosedList.
Reset();
246 double w = GetWeight();
250 double h = theHeuristic->HCost(from, to);
251 openClosedList.
AddOpenNode(from, env->GetStateHash(from), phi(h, 0), 0, h);
252 if (env->GoalTest(from, to))
264 template <
class state,
class action,
class environment>
267 double h = theHeuristic->HCost(newState, goal);
268 openClosedList.
AddOpenNode(newState, env->GetStateHash(newState), phi(h, 0), 0, h);
276 template <
class state,
class action,
class environment>
279 double h = theHeuristic->HCost(newState, goal);
280 openClosedList.
AddOpenNode(newState, env->GetStateHash(newState), phi(h, cost), cost, h);
293 template <
class state,
class action,
class environment>
297 if (
flesseq(bestSolution-solutionReduction, GetOptimalityBound()*maxPriority))
299 ExtractPathToStart(goal, thePath);
311 if (bestSolution == DBL_MAX)
313 DoGreedyStep(internalPath);
317 DoOptimalStep(internalPath);
323 template <
class state,
class action,
class environment>
326 uint64_t nodeid = openClosedList.
Close();
327 maxPriority =
std::max(maxPriority, openClosedList.
Lookup(nodeid).f);
329 if (!openClosedList.
Lookup(nodeid).reopened)
330 uniqueNodesExpanded++;
333 if ((env->GoalTest(openClosedList.
Lookup(nodeid).data, goal)))
338 phi = [](
double x,
double y){
return x+y;};
341 openClosedList.
Lookup(env->GetStateHash(start), ID);
342 openClosedList.
Lookup(ID).f = phi(openClosedList.
Lookup(ID).h, openClosedList.
Lookup(ID).g);
343 openClosedList.
Reopen(ID);
345 ExtractPathToStartFromID(nodeid, thePath);
347 bestSolution = env->GetPathLength(thePath);
349 for (
const auto &i : thePath)
352 openClosedList.
Lookup(env->GetStateHash(i), key);
353 openClosedList.
Lookup(key).onOptimalPath =
true;
355 printf(
"IOS: Found initial solution cost %f -- C* >= %f [%f]\n", bestSolution, maxPriority, bestSolution/maxPriority);
361 neighborID.resize(0);
362 neighborLoc.resize(0);
367 env->GetSuccessors(openClosedList.
Lookup(nodeid).data, neighbors);
369 for (
unsigned int x = 0; x < neighbors.size(); x++)
372 neighborLoc.push_back(openClosedList.
Lookup(env->GetStateHash(neighbors[x]), theID));
373 neighborID.push_back(theID);
374 edgeCosts.push_back(env->GCost(openClosedList.
Lookup(nodeid).data, neighbors[x]));
378 for (
int x = 0; x < neighbors.size(); x++)
382 switch (neighborLoc[x])
388 if (
fless(openClosedList.
Lookup(nodeid).g+edgeCosts[x], openClosedList.
Lookup(neighborID[x]).g))
390 auto &i = openClosedList.
Lookup(neighborID[x]);
392 i.g = openClosedList.
Lookup(nodeid).g+edgeCosts[x];
397 i.data = neighbors[x];
403 double h = theHeuristic->HCost(neighbors[x], goal);
405 env->GetStateHash(neighbors[x]),
406 phi(h, openClosedList.
Lookup(nodeid).g+edgeCosts[x]),
407 openClosedList.
Lookup(nodeid).g+edgeCosts[x],
415 template <
class state,
class action,
class environment>
418 uint64_t nodeid = openClosedList.
Close();
419 maxPriority =
std::max(maxPriority, openClosedList.
Lookup(nodeid).f);
421 if (!openClosedList.
Lookup(nodeid).reopened)
422 uniqueNodesExpanded++;
425 if ((env->GoalTest(openClosedList.
Lookup(nodeid).data, goal)))
429 ExtractPathToStartFromID(nodeid, thePath);
430 reverse(thePath.begin(), thePath.end());
431 bestSolution = env->GetPathLength(thePath);
437 neighborID.resize(0);
438 neighborLoc.resize(0);
443 env->GetSuccessors(openClosedList.
Lookup(nodeid).data, neighbors);
445 for (
unsigned int x = 0; x < neighbors.size(); x++)
448 neighborLoc.push_back(openClosedList.
Lookup(env->GetStateHash(neighbors[x]), theID));
449 neighborID.push_back(theID);
450 edgeCosts.push_back(env->GCost(openClosedList.
Lookup(nodeid).data, neighbors[x]));
454 for (
int x = 0; x < neighbors.size(); x++)
458 switch (neighborLoc[x])
462 auto &i = openClosedList.
Lookup(neighborID[x]);
465 if ((i.g)>(openClosedList.
Lookup(nodeid).g+edgeCosts[x]))
467 printf(
"IOS: Reduced solution cost by %f\n", (i.g)-(openClosedList.
Lookup(nodeid).g+edgeCosts[x]));
468 solutionReduction =
std::max(solutionReduction,
469 (i.g)-(openClosedList.
Lookup(nodeid).g+edgeCosts[x])
471 printf(
"IOS: Current solution cost %f -- C* >= %f [%f]\n", bestSolution-solutionReduction,
472 maxPriority, (bestSolution-solutionReduction)/maxPriority);
475 if (i.reopened ==
false)
479 i.g = openClosedList.
Lookup(nodeid).g+edgeCosts[x];
481 openClosedList.
Reopen(neighborID[x]);
485 i.data = neighbors[x];
490 if (
fless(openClosedList.
Lookup(nodeid).g+edgeCosts[x], openClosedList.
Lookup(neighborID[x]).g))
492 auto &i = openClosedList.
Lookup(neighborID[x]);
494 i.g = openClosedList.
Lookup(nodeid).g+edgeCosts[x];
499 i.data = neighbors[x];
505 double h = theHeuristic->HCost(neighbors[x], goal);
507 env->GetStateHash(neighbors[x]),
508 phi(h, openClosedList.
Lookup(nodeid).g+edgeCosts[x]),
509 openClosedList.
Lookup(nodeid).g+edgeCosts[x],
525 template <
class state,
class action,
class environment>
528 uint64_t key = openClosedList.
Peek();
529 return openClosedList.
Lookup(key).data;
540 template <
class state,
class action,
class environment>
542 std::vector<state> &thePath)
545 thePath.push_back(openClosedList.
Lookup(
node).data);
548 thePath.push_back(openClosedList.
Lookup(
node).data);
551 template <
class state,
class action,
class environment>
555 openClosedList.
Lookup(env->GetStateHash(s), theID);
556 theID = openClosedList.
Lookup(theID).parentID;
557 return openClosedList.
Lookup(theID).data;
566 template <
class state,
class action,
class environment>
569 printf(
"%u items in closed list\n", (
unsigned int)openClosedList.
ClosedSize());
570 printf(
"%u items in open queue\n", (
unsigned int)openClosedList.
OpenSize());
580 template <
class state,
class action,
class environment>
583 return openClosedList.
size();
596 template <
class state,
class action,
class environment>
603 gCost = openClosedList.
Lookat(theID).g;
609 template <
class state,
class action,
class environment>
616 gCost = openClosedList.
Lookat(theID).g;
628 template <
class state,
class action,
class environment>
633 template <
class state,
class action,
class environment>
636 double transparency = 1.0;
637 if (openClosedList.
size() == 0)
643 top = openClosedList.
Peek();
645 for (
unsigned int x = 0; x < openClosedList.
size(); x++)
647 const auto &data = openClosedList.
Lookat(x);
650 env->SetColor(1.0, 1.0, 0.0, transparency);
651 env->Draw(disp, data.
data);
653 else if ((data.where ==
kOpenList) && (data.reopened))
655 env->SetColor(0.0, 0.5, 0.5, transparency);
656 env->Draw(disp, data.
data);
660 env->SetColor(0.0, 1.0, 0.0, transparency);
661 env->Draw(disp, data.
data);
663 else if ((data.where ==
kClosedList) && (data.onOptimalPath))
665 env->SetColor(0.5, 0.5, 0.5, transparency);
666 env->Draw(disp, data.
data);
668 else if ((data.where ==
kClosedList) && (data.reopened))
670 env->SetColor(0.5, 0.0, 0.5, transparency);
671 env->Draw(disp, data.
data);
680 if (data.parentID == x)
681 env->SetColor(1.0, 0.5, 0.5, transparency);
683 env->SetColor(1.0, 0.0, 0.0, transparency);
685 env->Draw(disp, data.
data);
688 env->SetColor(1.0, 0.5, 1.0, 0.5);
689 env->Draw(disp, goal);