14 #include <unordered_map>
43 template <
class state,
class action,
class environment>
49 phi_open = ([=](
double h,
double g){
return g+h;});
53 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
54 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & );
62 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
138 if (
fequal(me.f_open, other.f_open))
140 if (
fequal(me.h, other.h))
142 return fless(me.h, other.h);
144 return fless(me.f_open, other.f_open);
175 if (
fequal(me.f_focal, other.f_focal))
177 if (
fequal(me.h, other.h))
179 return fless(me.h, other.h);
181 return fless(me.f_focal, other.f_focal);
195 auto i =
open.Peek();
196 return (*i.dataVector)[i.location];
200 auto i =
focal.Peek();
201 return (*i.dataVector)[i.location];
203 void Add(state &s,
double g,
double h,
size_t parent);
206 std::unordered_map<state, size_t>
hash;
241 template <
class state,
class action,
class environment>
244 static char name[32];
245 sprintf(name,
"Focal[%1.2f]", bound);
260 template <
class state,
class action,
class environment>
264 if (!InitializeSearch(_env, from, to, thePath))
268 while (!DoSingleSearchStep(thePath))
275 template <
class state,
class action,
class environment>
278 std::vector<state> thePath;
279 if (!InitializeSearch(_env, from, to, thePath))
284 while (!DoSingleSearchStep(thePath))
287 for (
int x = 0; x < thePath.size()-1; x++)
289 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
304 template <
class state,
class action,
class environment>
307 bestSolution = DBL_MAX;
309 if (theHeuristic == 0)
322 if (env->GoalTest(from, to))
328 Add(start, 0, theHeuristic->HCost(start, goal), states.size());
368 template <
class state,
class action,
class environment>
371 if (solution.size() > 0)
376 if (open.Size() == 0)
383 uint64_t nodeOnfocal;
388 size_t bestOpenLoc = open.Peek().location;
389 double regularF = states[bestOpenLoc].f_open;
390 if (
fless(minF, regularF))
395 if (states[i.location].where ==
kOpenList)
399 states[i.location].where = kOpenFocalList;
403 open.Iterate(minF*bound, regularF*bound, func);
407 if (focal.Size() > 0)
409 size_t bestFocalLoc = focal.Peek().location;
410 double focalF = states[bestFocalLoc].f_focal;
411 if (
flesseq(states[bestFocalLoc].f_open, bound*regularF))
418 printf(
"ERROR: Expanding open\n");
427 if (solution.size() > 0)
491 template <
class state,
class action,
class environment>
494 auto i = open.RemoveSmallest();
496 if (!states[i.location].reopened)
497 uniqueNodesExpanded++;
501 if (env->GoalTest(states[i.location].s, goal))
503 ExtractPathToStartFromID(i.location, solution);
505 reverse(solution.begin(), solution.end());
510 env->GetSuccessors(states[i.location].s, neighbors);
511 for (
unsigned int x = 0; x < neighbors.size(); x++)
512 Add(neighbors[x], states[i.location].g+env->GCost(states[i.location].s, neighbors[x]),
513 env->HCost(neighbors[x], goal), i.location);
577 template <
class state,
class action,
class environment>
580 auto i = focal.RemoveSmallest();
582 if (!states[i.location].reopened)
583 uniqueNodesExpanded++;
588 if (env->GoalTest(states[i.location].s, goal))
590 ExtractPathToStartFromID(i.location, solution);
592 reverse(solution.begin(), solution.end());
597 env->GetSuccessors(states[i.location].s, neighbors);
598 for (
unsigned int x = 0; x < neighbors.size(); x++)
599 Add(neighbors[x], states[i.location].g+env->GCost(states[i.location].s, neighbors[x]),
600 env->HCost(neighbors[x], goal), i.location);
700 template <
class state,
class action,
class environment>
702 std::vector<state> &thePath)
705 thePath.push_back(states[
node].s);
707 }
while (states[
node].parent !=
node);
708 thePath.push_back(states[
node].s);
711 template <
class state,
class action,
class environment>
728 template <
class state,
class action,
class environment>
731 printf(
"%u items in closed list\n", (
unsigned int)focal.ClosedSize());
732 printf(
"%u items in open queue\n", (
unsigned int)focal.OpenSize());
742 template <
class state,
class action,
class environment>
771 template <
class state,
class action,
class environment>
774 return states[open.Peek().location].s;
777 template <
class state,
class action,
class environment>
780 return states[focal.Peek().location].s;
784 template <
class state,
class action,
class environment>
787 auto i = hash.find(val);
792 gCost = states[i->second].g;
798 template <
class state,
class action,
class environment>
801 auto i = hash.find(val);
804 if (states[i->second].where == kOpenFocalList)
806 gCost = states[i->second].g;
827 template <
class state,
class action,
class environment>
830 auto i = hash.find(s);
836 auto item = open.Peek();
837 if (
flesseq(g+h, states[item.location].f_open*bound))
839 loc = kOpenFocalList;
843 loc = kOpenFocalList;
845 hash[s] = states.size();
861 if (
loc == kOpenFocalList)
865 focal.Add(focalItem);
868 else if (
fless(g, states[i->second].g)) {
871 states[i->second].parent = parent;
872 switch (states[i->second].where)
876 bool result = open.Remove(openItem);
881 assert(result ==
true);
883 result = focal.Remove(focalItem);
888 assert(result ==
true);
891 states[i->second].g = g;
892 states[i->second].f_open = phi_open(h, g);
893 states[i->second].f_focal = phi_focal(h, g);
896 focal.Add(focalItem);
902 bool result = open.Remove(openItem);
907 assert(result ==
true);
910 states[i->second].g = g;
911 states[i->second].f_open = phi_open(h, g);
912 states[i->second].f_focal = phi_focal(h, g);
915 auto item = open.Peek();
918 if (0&&
flesseq(g+h, states[item.location].f_open*bound))
921 focal.Add(focalItem);
922 states[i->second].where = kOpenFocalList;
929 states[i->second].g = g;
930 states[i->second].f_open = phi_open(h, g);
931 states[i->second].f_focal = phi_focal(h, g);
932 states[i->second].reopened =
true;
936 auto item = open.Peek();
937 if (
flesseq(g+h, states[item.location].f_open*bound))
940 focal.Add(focalItem);
941 states[i->second].where = kOpenFocalList;
955 template <
class state,
class action,
class environment>
960 template <
class state,
class action,
class environment>
963 double transparency = 1.0;
964 for (
unsigned int x = 0; x < states.size(); x++)
966 const auto &data = states[x].s;
967 if ((states[x].where ==
kOpenList) && (states[x].reopened))
969 env->SetColor(0.0, 0.5, 0.5, transparency);
974 env->SetColor(0.0, 1.0, 0.0, transparency);
977 else if ((states[x].where ==
kClosedList) && (states[x].reopened))
979 env->SetColor(0.5, 0.0, 0.5, transparency);
987 env->SetColor(1.0, 0.0, 0.0, transparency);
990 else if ((states[x].where == kOpenFocalList) && (states[x].reopened))
992 env->SetColor(0.5, 1.0, 0.5, transparency);
995 else if (states[x].where == kOpenFocalList)
997 env->SetColor(0.0, 0.0, 1.0, transparency);