29 #ifndef OldTemplateAStar_H
30 #define OldTemplateAStar_H
32 #define __STDC_CONSTANT_MACROS
38 #define UINT32_MAX 4294967295U
42 #include <unordered_map>
57 template <
class state>
60 SearchNode(state &curr, state &prev,
double _fCost,
double _gCost, uint64_t key)
78 template <
class state>
83 template <
class state>
94 template <
class state>
105 template <
class state,
class action,
class environment>
110 void GetPath(environment *
env, state& from, state& to, std::vector<state> &thePath);
112 void GetPath(environment *, state& , state& , std::vector<action> & ) { assert(
false); };
125 bool InitializeSearch(environment *
env, state& from, state& to, std::vector<state> &thePath);
204 template <
class state,
class action,
class environment>
207 static char name[32];
208 sprintf(name,
"OldTemplateAStar[]");
223 template <
class state,
class action,
class environment>
227 if (!InitializeSearch(_env, from, to, thePath))
231 while (!DoSingleSearchStep(thePath)) {}
244 template <
class state,
class action,
class environment>
256 assert(openQueue.size() == 0);
257 assert(closedList.size() == 0);
258 nodesTouched = nodesExpanded = 0;
262 if ((from == to) && (stopAfterGoal))
268 SearchNode<state> first(start, start, weight*env->HCost(start, goal), 0, env->GetStateHash(start));
269 openQueue.Add(first);
279 template <
class state,
class action,
class environment>
282 SearchNode<state> first(newState, newState, weight*env->HCost(goal, newState), 0,env->GetStateHash(newState));
283 openQueue.Add(first);
296 template <
class state,
class action,
class environment>
299 state currentOpenNode;
301 if (openQueue.size() == 0)
309 if (!GetNextNode(currentOpenNode))
311 printf(
"Oh no! No more open nodes!\n");
315 if ((stopAfterGoal) && (env->GoalTest(currentOpenNode, goal)))
317 ExtractPathToStart(currentOpenNode, thePath);
319 reverse(thePath.begin(), thePath.end());
327 env->GetSuccessors(currentOpenNode, neighbors);
332 double oldf = currNode.
fCost;
333 for (
unsigned int x = 0; x < neighbors.size(); x++)
335 double newh = env->HCost(neighbors[x], goal)-env->GCost(currentOpenNode, neighbors[x]);
340 closedList[env->GetStateHash(currentOpenNode)].fCost = currNode.
fCost;
345 for (
unsigned int x = 0; x < neighbors.size(); x++)
350 if (closedList.find(env->GetStateHash(
neighbor)) != closedList.end())
356 UpdateWeight(env, currentOpenNode,
neighbor);
358 else if (useRadius && useOccupancyInfo && env->GetOccupancyInfo() && radEnv && (radEnv->HCost(start,
neighbor) < radius) &&(env->GetOccupancyInfo()->GetStateOccupied(
neighbor)) && ((!(radEnv->GoalTest(
neighbor, goal)))))
361 closedList[env->GetStateHash(
neighbor)] = sn;
364 AddToOpenList(env, currentOpenNode,
neighbor);
370 if ((openQueue.size() == 0) && (stopAfterGoal ==
false))
372 ExtractPathToStart(currentOpenNode, thePath);
374 reverse(thePath.begin(), thePath.end());
389 template <
class state,
class action,
class environment>
392 return openQueue.top().currNode;
404 template <
class state,
class action,
class environment>
408 if (openQueue.Empty())
419 closedList[env->GetStateHash(next)] = it;
432 template <
class state,
class action,
class environment>
439 double edgeWeight = e->GCost(currOpenNode,
neighbor);
445 prev.
fCost = altCost;
451 closedList.erase(e->GetStateHash(
neighbor));
452 assert(closedList.find(e->GetStateHash(
neighbor)) == closedList.end());
465 template <
class state,
class action,
class environment>
471 double edgeWeight = e->GCost(currOpenNode,
neighbor);
477 prev.
fCost = altCost;
483 openQueue.DecreaseKey(prev);
495 template <
class state,
class action,
class environment>
500 double edgeWeight = e->GCost(currOpenNode,
neighbor);
502 double oldfCost = closedList[e->GetStateHash(currOpenNode)].fCost;
503 double oldgCost = closedList[e->GetStateHash(currOpenNode)].gCost;
504 double gCost = oldgCost+edgeWeight;
505 double fCost = gCost+weight*e->HCost(
neighbor, goal);
529 template <
class state,
class action,
class environment>
531 std::vector<state> &thePath)
534 if (closedList.find(env->GetStateHash(goalNode)) != closedList.end())
536 n = closedList[env->GetStateHash(goalNode)];
538 else n = openQueue.find(
SearchNode<state>(goalNode, env->GetStateHash(goalNode)));
544 if (closedList.find(env->GetStateHash(n.
prevNode)) != closedList.end())
546 n = closedList[env->GetStateHash(n.
prevNode)];
549 printf(
"No backward path found!\n");
562 template <
class state,
class action,
class environment>
565 printf(
"%u items in closed list\n", (
unsigned int)closedList.size());
566 printf(
"%u items in open queue\n", (
unsigned int)openQueue.size());
576 template <
class state,
class action,
class environment>
579 return closedList.size()+openQueue.size();
589 template <
class state,
class action,
class environment>
593 return closedList.begin();
606 template <
class state,
class action,
class environment>
609 if (it == closedList.end())
626 template <
class state,
class action,
class environment>
629 if (closedList.find(env->GetStateHash(val)) != closedList.end())
631 gCost = closedList.find(env->GetStateHash(val))->second.gCost;
643 template <
class state,
class action,
class environment>
652 env->OpenGLDraw((*it).second.currNode);