23 #include <unordered_map>
45 template <
class state,
class action,
class environment>
51 phi_open = ([=](
double h,
double g){
return g+h;});
55 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
56 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & );
64 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
140 if (
fequal(me.f_open, other.f_open))
142 if (
fequal(me.h, other.h))
144 return fless(me.h, other.h);
146 return fless(me.f_open, other.f_open);
177 if (
fequal(me.f_focal, other.f_focal))
179 if (
fequal(me.h, other.h))
181 return fless(me.h, other.h);
183 return fless(me.f_focal, other.f_focal);
197 auto i =
open.Peek();
198 return (*i.dataVector)[i.location];
202 auto i =
focal.Peek();
203 return (*i.dataVector)[i.location];
205 void Add(state &s,
double g,
double h,
size_t parent);
208 std::unordered_map<state, size_t>
hash;
243 template <
class state,
class action,
class environment>
246 static char name[32];
247 sprintf(name,
"Focal[%1.2f]", bound);
262 template <
class state,
class action,
class environment>
266 if (!InitializeSearch(_env, from, to, thePath))
270 while (!DoSingleSearchStep(thePath))
277 template <
class state,
class action,
class environment>
280 std::vector<state> thePath;
281 if (!InitializeSearch(_env, from, to, thePath))
286 while (!DoSingleSearchStep(thePath))
289 for (
int x = 0; x < thePath.size()-1; x++)
291 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
306 template <
class state,
class action,
class environment>
309 bestSolution = DBL_MAX;
311 if (theHeuristic == 0)
324 if (env->GoalTest(from, to))
330 double init_h = theHeuristic->HCost(start, goal);
331 phi_focal = ([=](
double h,
double g){
return g+h+bound*
min(h, init_h)/init_h;});
332 Add(start, 0, theHeuristic->HCost(start, goal), states.size());
372 template <
class state,
class action,
class environment>
375 if (solution.size() > 0)
380 if (open.Size() == 0)
387 uint64_t nodeOnfocal;
392 size_t bestOpenLoc = open.Peek().location;
393 double regularF = states[bestOpenLoc].f_open;
394 if (
fless(minF, regularF))
399 if (states[i.location].where ==
kOpenList)
403 states[i.location].where = kOpenFocalList;
407 open.Iterate(minF+bound, regularF+bound, func);
411 if (focal.Size() > 0)
413 size_t bestFocalLoc = focal.Peek().location;
414 double focalF = states[bestFocalLoc].f_focal;
415 if (
flesseq(states[bestFocalLoc].f_open, bound+regularF))
422 printf(
"ERROR: Expanding open\n");
431 if (solution.size() > 0)
495 template <
class state,
class action,
class environment>
498 auto i = open.RemoveSmallest();
500 if (!states[i.location].reopened)
501 uniqueNodesExpanded++;
505 if (env->GoalTest(states[i.location].s, goal))
507 ExtractPathToStartFromID(i.location, solution);
509 reverse(solution.begin(), solution.end());
514 env->GetSuccessors(states[i.location].s, neighbors);
515 for (
unsigned int x = 0; x < neighbors.size(); x++)
516 Add(neighbors[x], states[i.location].g+env->GCost(states[i.location].s, neighbors[x]),
517 env->HCost(neighbors[x], goal), i.location);
581 template <
class state,
class action,
class environment>
584 auto i = focal.RemoveSmallest();
586 if (!states[i.location].reopened)
587 uniqueNodesExpanded++;
592 if (env->GoalTest(states[i.location].s, goal))
594 ExtractPathToStartFromID(i.location, solution);
596 reverse(solution.begin(), solution.end());
601 env->GetSuccessors(states[i.location].s, neighbors);
602 for (
unsigned int x = 0; x < neighbors.size(); x++)
603 Add(neighbors[x], states[i.location].g+env->GCost(states[i.location].s, neighbors[x]),
604 env->HCost(neighbors[x], goal), i.location);
704 template <
class state,
class action,
class environment>
706 std::vector<state> &thePath)
709 thePath.push_back(states[
node].s);
711 }
while (states[
node].parent !=
node);
712 thePath.push_back(states[
node].s);
715 template <
class state,
class action,
class environment>
732 template <
class state,
class action,
class environment>
735 printf(
"%u items in closed list\n", (
unsigned int)focal.ClosedSize());
736 printf(
"%u items in open queue\n", (
unsigned int)focal.OpenSize());
746 template <
class state,
class action,
class environment>
775 template <
class state,
class action,
class environment>
778 return states[open.Peek().location].s;
781 template <
class state,
class action,
class environment>
784 return states[focal.Peek().location].s;
788 template <
class state,
class action,
class environment>
791 auto i = hash.find(val);
796 gCost = states[i->second].g;
802 template <
class state,
class action,
class environment>
805 auto i = hash.find(val);
808 if (states[i->second].where == kOpenFocalList)
810 gCost = states[i->second].g;
831 template <
class state,
class action,
class environment>
834 auto i = hash.find(s);
840 auto item = open.Peek();
841 if (
flesseq(g+h, states[item.location].f_open+bound))
843 loc = kOpenFocalList;
847 loc = kOpenFocalList;
849 hash[s] = states.size();
865 if (
loc == kOpenFocalList)
869 focal.Add(focalItem);
872 else if (
fless(g, states[i->second].g)) {
875 states[i->second].parent = parent;
876 switch (states[i->second].where)
880 bool result = open.Remove(openItem);
885 assert(result ==
true);
887 result = focal.Remove(focalItem);
892 assert(result ==
true);
895 states[i->second].g = g;
896 states[i->second].f_open = phi_open(h, g);
897 states[i->second].f_focal = phi_focal(h, g);
900 focal.Add(focalItem);
906 bool result = open.Remove(openItem);
911 assert(result ==
true);
914 states[i->second].g = g;
915 states[i->second].f_open = phi_open(h, g);
916 states[i->second].f_focal = phi_focal(h, g);
919 auto item = open.Peek();
922 if (0&&
flesseq(g+h, states[item.location].f_open+bound))
925 focal.Add(focalItem);
926 states[i->second].where = kOpenFocalList;
933 states[i->second].g = g;
934 states[i->second].f_open = phi_open(h, g);
935 states[i->second].f_focal = phi_focal(h, g);
936 states[i->second].reopened =
true;
940 auto item = open.Peek();
941 if (
flesseq(g+h, states[item.location].f_open+bound))
944 focal.Add(focalItem);
945 states[i->second].where = kOpenFocalList;
959 template <
class state,
class action,
class environment>
964 template <
class state,
class action,
class environment>
967 double transparency = 1.0;
968 for (
unsigned int x = 0; x < states.size(); x++)
970 const auto &data = states[x].s;
971 if ((states[x].where ==
kOpenList) && (states[x].reopened))
973 env->SetColor(0.0, 0.5, 0.5, transparency);
978 env->SetColor(0.0, 1.0, 0.0, transparency);
981 else if ((states[x].where ==
kClosedList) && (states[x].reopened))
983 env->SetColor(0.5, 0.0, 0.5, transparency);
991 env->SetColor(1.0, 0.0, 0.0, transparency);
994 else if ((states[x].where == kOpenFocalList) && (states[x].reopened))
996 env->SetColor(0.5, 1.0, 0.5, transparency);
999 else if (states[x].where == kOpenFocalList)
1001 env->SetColor(0.0, 0.0, 1.0, transparency);