10 #define AStarEpsilon_h
15 #include <unordered_map>
24 template <
class state>
40 template <
class state,
class action,
class environment>
45 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
46 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & );
54 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
84 { uint64_t key;
return focal.
Lookup(
env->GetStateHash(val), key); }
128 template <
class state,
class action,
class environment>
131 static char name[32];
132 sprintf(name,
"AStarEpsilon[%1.2f]", bound);
147 template <
class state,
class action,
class environment>
151 if (!InitializeSearch(_env, from, to, thePath))
155 while (!DoSingleSearchStep(thePath))
162 template <
class state,
class action,
class environment>
165 std::vector<state> thePath;
166 if (!InitializeSearch(_env, from, to, thePath))
171 while (!DoSingleSearchStep(thePath))
174 for (
int x = 0; x < thePath.size()-1; x++)
176 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
191 template <
class state,
class action,
class environment>
194 bestSolution = DBL_MAX;
196 if (theHeuristic == 0)
200 focal.Reset(env->GetMaxHash());
201 f.Reset(env->GetMaxHash());
207 if (env->GoalTest(from, to))
212 focal.AddOpenNode(start, env->GetStateHash(start), 0, theHeuristic->HCost(start, goal));
213 f.AddOpenNode(start, env->GetStateHash(start), 0, theHeuristic->HCost(start, goal));
252 template <
class state,
class action,
class environment>
255 if (solution.size() > 0)
260 if (focal.OpenSize() == 0)
267 uint64_t nodeOnfocal;
272 double regularF = f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h;
273 double focalF = focal.Lookat(focal.Peek()).g+focal.Lookat(focal.Peek()).h;
274 printf(
"Min f: %1.2f; next focal f: %1.2f. Max allowable: %1.2f.", regularF, focalF, regularF*bound);
275 if (
flesseq(focalF, bound*regularF))
285 if (solution.size() > 0)
349 template <
class state,
class action,
class environment>
352 uint64_t nodeid = f.Close();
353 if (!f.Lookup(nodeid).reopened)
354 uniqueNodesExpanded++;
357 if (env->GoalTest(f.Lookup(nodeid).data, goal))
359 ExtractPathToStartFromID(nodeid, solution);
361 reverse(solution.begin(), solution.end());
367 neighborID.resize(0);
368 neighborLoc.resize(0);
373 env->GetSuccessors(f.Lookup(nodeid).data, neighbors);
374 double bestH = f.Lookup(nodeid).h;
375 double lowHC = DBL_MAX;
377 for (
unsigned int x = 0; x < neighbors.size(); x++)
380 neighborLoc.push_back(f.Lookup(env->GetStateHash(neighbors[x]), theID));
381 neighborID.push_back(theID);
382 edgeCosts.push_back(env->GCost(f.Lookup(nodeid).data, neighbors[x]));
386 for (
int x = 0; x < neighbors.size(); x++)
390 switch (neighborLoc[x])
409 if (
fless(f.Lookup(nodeid).g+edgeCosts[x], f.Lookup(neighborID[x]).g))
411 f.Lookup(neighborID[x]).parentID = nodeid;
412 f.Lookup(neighborID[x]).g = f.Lookup(nodeid).g+edgeCosts[x];
416 f.Lookup(neighborID[x]).data = neighbors[x];
417 f.KeyChanged(neighborID[x]);
427 f.AddOpenNode(neighbors[x],
428 env->GetStateHash(neighbors[x]),
429 f.Lookup(nodeid).g+edgeCosts[x],
430 theHeuristic->HCost(neighbors[x], goal),
437 template <
class state,
class action,
class environment>
440 uint64_t nodeid = focal.Close();
441 if (!focal.Lookup(nodeid).reopened)
442 uniqueNodesExpanded++;
445 if (env->GoalTest(focal.Lookup(nodeid).data, goal))
447 ExtractPathToStartFromID(nodeid, solution);
449 reverse(solution.begin(), solution.end());
455 neighborID.resize(0);
456 neighborLoc.resize(0);
458 std::cout <<
"Expanding: " << focal.Lookup(nodeid).data <<
" with f:";
459 std::cout << focal.Lookup(nodeid).g+focal.Lookup(nodeid).h << std::endl;
461 env->GetSuccessors(focal.Lookup(nodeid).data, neighbors);
462 double bestH = focal.Lookup(nodeid).h;
463 double lowHC = DBL_MAX;
465 for (
unsigned int x = 0; x < neighbors.size(); x++)
468 neighborLoc.push_back(focal.Lookup(env->GetStateHash(neighbors[x]), theID));
469 neighborID.push_back(theID);
470 edgeCosts.push_back(env->GCost(focal.Lookup(nodeid).data, neighbors[x]));
474 for (
int x = 0; x < neighbors.size(); x++)
478 switch (neighborLoc[x])
484 if (
fless(focal.Lookup(nodeid).g+edgeCosts[x], focal.Lookup(neighborID[x]).g))
486 focal.Lookup(neighborID[x]).parentID = nodeid;
487 focal.Lookup(neighborID[x]).g = focal.Lookup(nodeid).g+edgeCosts[x];
491 focal.Lookup(neighborID[x]).data = neighbors[x];
492 focal.KeyChanged(neighborID[x]);
502 focal.AddOpenNode(neighbors[x],
503 env->GetStateHash(neighbors[x]),
504 focal.Lookup(nodeid).g+edgeCosts[x],
505 theHeuristic->HCost(neighbors[x], goal),
537 template <
class state,
class action,
class environment>
539 std::vector<state> &thePath)
542 thePath.push_back(focal.Lookup(
node).data);
544 }
while (focal.Lookup(
node).parentID !=
node);
545 thePath.push_back(focal.Lookup(
node).data);
548 template <
class state,
class action,
class environment>
552 focal.Lookup(env->GetStateHash(s), theID);
553 theID = focal.Lookup(theID).parentID;
554 return focal.Lookup(theID).data;
563 template <
class state,
class action,
class environment>
566 printf(
"%u items in closed list\n", (
unsigned int)focal.ClosedSize());
567 printf(
"%u items in open queue\n", (
unsigned int)focal.OpenSize());
577 template <
class state,
class action,
class environment>
593 template <
class state,
class action,
class environment>
600 gCost = focal.Lookat(theID).g;
606 template <
class state,
class action,
class environment>
609 uint64_t key = f.Peek();
610 return f.Lookup(key).data;
613 template <
class state,
class action,
class environment>
616 uint64_t key = focal.Peek();
617 return focal.Lookup(key).data;
621 template <
class state,
class action,
class environment>
628 gCost = f.Lookat(theID).g;
634 template <
class state,
class action,
class environment>
641 gCost = focal.Lookat(theID).g;
648 template <
class state,
class action,
class environment>
655 result = focal.Lookat(theID);
669 template <
class state,
class action,
class environment>
672 double transparency = 1.0;
673 if (focal.size() == 0)
676 double bound = DBL_MAX;
679 if (focal.OpenSize() > 0)
682 const auto &i = f.Lookat(f.Peek());
684 printf(
"Lowest f on open: %f\n", bound);
686 for (
unsigned int x = 0; x < focal.size(); x++)
688 const auto &data = focal.Lookat(x);
691 env->SetColor(1.0, 1.0, 0.0, transparency);
692 env->OpenGLDraw(data.data);
699 else if ((data.where ==
kOpenList) && (data.reopened))
701 env->SetColor(0.0, 0.5, 0.5, transparency);
702 env->OpenGLDraw(data.data);
706 env->SetColor(0.0, 1.0, 0.0, transparency);
707 env->OpenGLDraw(data.data);
709 else if ((data.where ==
kClosedList) && (data.reopened))
711 env->SetColor(0.5, 0.0, 0.5, transparency);
712 env->OpenGLDraw(data.data);
716 if (data.parentID == x)
717 env->SetColor(1.0, 0.5, 0.5, transparency);
719 env->SetColor(1.0, 0.0, 0.0, transparency);
721 env->OpenGLDraw(data.data);
724 env->SetColor(1.0, 0.5, 1.0, 0.5);
725 env->OpenGLDraw(goal);
728 template <
class state,
class action,
class environment>
733 env->SetColor(1.0, 0.5, 1.0, 0.5);
737 template <
class state,
class action,
class environment>
740 double transparency = 1.0;
744 double bound = DBL_MAX;
747 if (f.OpenSize() > 0)
750 const auto &i = f.Lookat(f.Peek());
754 for (
unsigned int x = 0; x < f.size(); x++)
756 const auto &data = f.Lookat(x);
759 env->SetColor(1.0, 1.0, 0.0, transparency);
760 env->Draw(
d, data.data);
767 if ((data.where ==
kOpenList) && (data.reopened))
769 env->SetColor(0.0, 0.5, 0.5, transparency);
770 env->Draw(
d, data.data);
774 env->SetColor(0.0, 1.0, 0.0, transparency);
775 env->Draw(
d, data.data);
777 else if ((data.where ==
kClosedList) && (data.reopened))
779 env->SetColor(0.5, 0.0, 0.5, transparency);
780 env->Draw(
d, data.data);
784 if (data.parentID == x)
785 env->SetColor(1.0, 0.5, 0.5, transparency);
787 env->SetColor(0.0, 0.0, 1.0, transparency);
788 env->Draw(
d, data.data);
793 template <
class state,
class action,
class environment>
796 double transparency = 1.0;
797 if (focal.size() == 0)
800 double bound = DBL_MAX;
803 if (focal.OpenSize() > 0)
806 const auto &i = focal.Lookat(focal.Peek());
810 for (
unsigned int x = 0; x < focal.size(); x++)
812 const auto &data = focal.Lookat(x);
815 env->SetColor(1.0, 1.0, 0.0, transparency);
816 env->Draw(
d, data.data);
823 if ((data.where ==
kOpenList) && (data.reopened))
825 env->SetColor(0.0, 0.5, 0.5, transparency);
826 env->Draw(
d, data.data);
830 env->SetColor(0.0, 1.0, 0.0, transparency);
831 env->Draw(
d, data.data);
833 else if ((data.where ==
kClosedList) && (data.reopened))
835 env->SetColor(0.5, 0.0, 0.5, transparency);
836 env->Draw(
d, data.data);
840 if (data.parentID == x)
841 env->SetColor(1.0, 0.5, 0.5, transparency);
843 env->SetColor(1.0, 0.0, 0.0, transparency);
844 env->Draw(
d, data.data);