30 #define __STDC_CONSTANT_MACROS
36 #define UINT32_MAX 4294967295U
40 #include <unordered_map>
50 template <
class state>
65 template <
class state,
class action,
class environment>
70 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
72 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & ) { assert(
false); };
78 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
145 template <
class state,
class action,
class environment>
148 static char name[32];
149 sprintf(name,
"PEAStar[]");
164 template <
class state,
class action,
class environment>
168 if (!InitializeSearch(_env, from, to, thePath))
172 while (!DoSingleSearchStep(thePath)) {}
185 template <
class state,
class action,
class environment>
197 openClosedList.Reset();
203 if (env->GoalTest(from, to) && (stopAfterGoal))
208 openClosedList.AddOpenNode(start, env->GetStateHash(start), 0, weight*theHeuristic->HCost(start, goal));
219 template <
class state,
class action,
class environment>
222 openClosedList.AddOpenNode(newState, env->GetStateHash(newState), 0, weight*theHeuristic->HCost(start, goal));
230 template <
class state,
class action,
class environment>
233 openClosedList.AddOpenNode(newState, env->GetStateHash(newState), cost, weight*theHeuristic->HCost(start, goal));
246 template <
class state,
class action,
class environment>
249 if (openClosedList.OpenSize() == 0)
255 uint64_t nodeid = openClosedList.Peek();
256 const state &currOpenNode = openClosedList.Lookup(nodeid).data;
258 if (!openClosedList.Lookup(nodeid).reopened)
259 uniqueNodesExpanded++;
262 if ((stopAfterGoal) && (env->GoalTest(currOpenNode, goal)))
264 ExtractPathToStartFromID(nodeid, thePath);
266 reverse(thePath.begin(), thePath.end());
270 env->GetSuccessors(currOpenNode, succ);
271 double fCost = openClosedList.Lookup(nodeid).h+openClosedList.Lookup(nodeid).g;
278 for (
unsigned int x = 0; x < succ.size(); x++)
280 double edgeCost = env->GCost(currOpenNode, succ[x]);
281 double newFCost = openClosedList.Lookup(nodeid).g+edgeCost+weight*theHeuristic->HCost(succ[x], goal);
282 dataLocation loc = openClosedList.Lookup(env->GetStateHash(succ[x]), theID);
284 (
loc ==
kOpenList && !
fless(newFCost, openClosedList.Lookup(theID).g+openClosedList.Lookup(theID).h)))
293 else if (newFCost < nextBest)
299 openClosedList.Close();
303 openClosedList.Lookup(nodeid).h += nextBest-fCost;
304 openClosedList.KeyChanged(nodeid);
308 for (
unsigned int x = 0; x < succ.size(); x++)
310 double edgeCost = env->GCost(currOpenNode, succ[x]);
311 double newFCost = openClosedList.Lookup(nodeid).g+edgeCost+weight*theHeuristic->HCost(succ[x], goal);
312 if (!
fequal(fCost, newFCost))
315 switch (openClosedList.Lookup(env->GetStateHash(succ[x]), theID))
325 if (
fless(openClosedList.Lookup(nodeid).g+edgeCost, openClosedList.Lookup(theID).g))
327 openClosedList.Lookup(theID).parentID = nodeid;
328 openClosedList.Lookup(theID).g = openClosedList.Lookup(nodeid).g+edgeCost;
329 openClosedList.KeyChanged(theID);
335 if (
fequal(newFCost, fCost))
337 openClosedList.AddOpenNode(succ[x],
338 env->GetStateHash(succ[x]),
339 openClosedList.Lookup(nodeid).g+edgeCost,
340 weight*theHeuristic->HCost(succ[x], goal),
356 template <
class state,
class action,
class environment>
359 uint64_t key = openClosedList.Peek();
360 return openClosedList.Lookup(key).data;
374 template <
class state,
class action,
class environment>
376 std::vector<state> &thePath)
379 thePath.push_back(openClosedList.Lookup(
node).data);
380 node = openClosedList.Lookup(
node).parentID;
381 }
while (openClosedList.Lookup(
node).parentID !=
node);
382 thePath.push_back(openClosedList.Lookup(
node).data);
391 template <
class state,
class action,
class environment>
394 printf(
"%u items in closed list\n", (
unsigned int)openClosedList.ClosedSize());
395 printf(
"%u items in open queue\n", (
unsigned int)openClosedList.OpenSize());
405 template <
class state,
class action,
class environment>
408 return openClosedList.size();
421 template <
class state,
class action,
class environment>
425 dataLocation loc = openClosedList.Lookup(env->GetStateHash(val), theID);
428 gCost = openClosedList.Lookat(theID).g;
440 template <
class state,
class action,
class environment>
443 double transparency = 1.0;
444 if (openClosedList.size() == 0)
447 if (openClosedList.OpenSize() > 0)
448 top = openClosedList.Peek();
449 for (
unsigned int x = 0; x < openClosedList.size(); x++)
454 env->SetColor(1.0, 1.0, 0.0, transparency);
455 env->OpenGLDraw(data.
data);
459 env->SetColor(0.0, 0.5, 0.5, transparency);
460 env->OpenGLDraw(data.
data);
464 env->SetColor(0.0, 1.0, 0.0, transparency);
465 env->OpenGLDraw(data.
data);
469 env->SetColor(0.5, 0.0, 0.5, transparency);
470 env->OpenGLDraw(data.
data);
474 env->SetColor(1.0, 0.0, 0.0, transparency);
475 env->OpenGLDraw(data.
data);