HOG2
GenericAStar.cpp
Go to the documentation of this file.
1 /*
2  * $Id: GenericAStar.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 3/22/06.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "GenericAStar.h"
13 #include "float.h"
14 #include "FPUtil.h"
15 
16 using namespace GenericAStarUtil;
17 static const bool verbose = false;
18 using namespace OldSearchCode;
19 
20 const char *GenericAStar::GetName()
21 {
22  static char name[32];
23  sprintf(name, "GenericAStar[]");
24  return name;
25 }
26 
27 
28 void GenericAStar::GetPath(SearchEnvironment *_env, uint32_t from, uint32_t to,
29  std::vector<uint32_t> &thePath)
30 {
31  thePath.resize(0);
32  if (!InitializeSearch(_env, from, to, thePath))
33  return;
34  while (!DoSingleSearchStep(thePath)) {}
35 }
36 
37 bool GenericAStar::InitializeSearch(SearchEnvironment *_env, uint32_t from, uint32_t to,
38  std::vector<uint32_t> &thePath)
39 {
40  env = _env;
41  assert(openQueue.size() == 0);
42  assert(closedList.size() == 0);
43  nodesTouched = nodesExpanded = 0;
44  start = from;
45  goal = to;
46 
47  if ((from == UINT32_MAX) || (to == UINT32_MAX) || (from == to))
48  {
49  thePath.resize(0);
50  return false;
51  }
52 
53  SearchNode first(env->heuristic(goal, start), 0, start, start);
54  openQueue.Add(first);
55 
56  return true;
57 }
58 
59 bool GenericAStar::DoSingleSearchStep(std::vector<uint32_t> &thePath)
60 {
61  uint32_t currentOpenNode = UINT32_MAX;
62 
63  if (openQueue.size() == 0)
64  {
65  thePath.resize(0); // no path found!
66  return true;
67  }
68 
69  // get top of queue
70  currentOpenNode = GetNextNode();
71 
72  if (env->goalTest(currentOpenNode, goal))
73  {
74  ExtractPathToStart(currentOpenNode, thePath);
75  closedList.clear();
76  openQueue.reset();
77  env = 0;
78  return true;
79  }
80 
81  if (verbose) { printf("Opening %d\n", currentOpenNode); }
82 
83  if (currentOpenNode == UINT32_MAX)
84  printf("Oh no! The current open node is NULL\n");
85 
86  neighbors.resize(0);
87  env->getNeighbors(currentOpenNode, neighbors);
88 
89  // iterate over all the children
90  for (unsigned int x = 0; x < neighbors.size(); x++)
91  {
92  nodesTouched++;
93  uint32_t neighbor = neighbors[x];
94  assert(neighbor != UINT32_MAX);
95 
96  if (closedList.find(neighbor) != closedList.end())
97  {
98  if (verbose) { printf("skipping node %d\n", neighbor); }
99  continue;
100  }
101  else if (openQueue.IsIn(SearchNode(neighbor)))
102  {
103  if (verbose) { printf("updating node %d\n", neighbor); }
104  UpdateWeight(currentOpenNode, neighbor);
105  }
106  else {
107  if (verbose) { printf("addinging node %d\n", neighbor); }
108  AddToOpenList(currentOpenNode, neighbor);
109  }
110  }
111  return false;
112 }
113 
115 {
116  return openQueue.top().currNode;
117 }
118 
120 {
121  nodesExpanded++;
122  uint32_t next;
123  SearchNode it = openQueue.Remove();
124  next = it.currNode;
125  closedList[next] = it;
126  return next;
127 }
128 
129 void GenericAStar::UpdateWeight(uint32_t currOpenNode, uint32_t neighbor)
130 {
131  SearchNode prev = openQueue.find(SearchNode(neighbor));
132  SearchNode alt = closedList[currOpenNode];
133  double edgeWeight = env->gcost(currOpenNode, neighbor);
134  double altCost = alt.gCost+edgeWeight+(prev.fCost-prev.gCost);
135  if (fgreater(prev.fCost, altCost))
136  {
137  prev.fCost = altCost;
138  prev.gCost = alt.gCost+edgeWeight;
139  prev.prevNode = currOpenNode;
140  openQueue.DecreaseKey(prev);
141  }
142 }
143 
144 void GenericAStar::AddToOpenList(uint32_t currOpenNode, uint32_t neighbor)
145 {
146  double edgeWeight = env->gcost(currOpenNode, neighbor);
147  SearchNode n(closedList[currOpenNode].gCost+edgeWeight+env->heuristic(neighbor, goal),
148  closedList[currOpenNode].gCost+edgeWeight,
149  neighbor, currOpenNode);
150  if (verbose)
151  { printf("Adding %u to openQueue, old size %u\n", neighbor, openQueue.size()); }
152  openQueue.Add(n);
153 }
154 
155 void GenericAStar::ExtractPathToStart(uint32_t goalNode,
156  std::vector<uint32_t> &thePath)
157 {
158  SearchNode n;
159  if (closedList.find(goalNode) != closedList.end())
160  {
161  n = closedList[goalNode];
162  }
163  else n = openQueue.find(SearchNode(goalNode));
164 
165  do {
166  thePath.push_back(n.currNode);
167  n = closedList[n.prevNode];
168  } while (n.currNode != n.prevNode);
169  thePath.push_back(n.currNode);
170 }
171 
173 {
174  printf("%u items in closed list\n", (unsigned int)closedList.size());
175  printf("%u items in open queue\n", (unsigned int)openQueue.size());
176 }
177 
179 {
180  return closedList.size()+openQueue.size();
181 }
182 
184 {
185  return closedList.begin();
186 }
187 
189 {
190  if (it == closedList.end())
191  return UINT32_MAX;
192  uint32_t val = (*it).first;
193  it++;
194  return val;
195 }
GenericAStar::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< uint32_t > &thePath)
Definition: GenericAStar.cpp:59
graphMove
Definition: GraphEnvironment.h:34
GenericAStar::PrintStats
void PrintStats()
Definition: GenericAStar.cpp:172
GenericAStar::ExtractPathToStart
void ExtractPathToStart(uint32_t n, std::vector< uint32_t > &thePath)
Definition: GenericAStar.cpp:155
GenericAStarUtil::SearchNode::gCost
double gCost
Definition: GenericAStar.h:42
GenericAStar::GetMemoryUsage
int GetMemoryUsage()
Definition: GenericAStar.cpp:178
GenericAStar::UpdateWeight
void UpdateWeight(uint32_t currOpenNode, uint32_t neighbor)
Definition: GenericAStar.cpp:129
GenericAStarUtil
Definition: GenericAStar.h:31
OldSearchCode
Definition: OldSearchEnvironment.cpp:12
FPUtil.h
GenericAStar::GetPath
void GetPath(OldSearchCode::SearchEnvironment *env, uint32_t from, uint32_t to, std::vector< uint32_t > &thePath)
Definition: GenericAStar.cpp:28
GenericAStar::AddToOpenList
void AddToOpenList(uint32_t currOpenNode, uint32_t neighbor)
Definition: GenericAStar.cpp:144
OldSearchCode::SearchEnvironment
Definition: OldSearchEnvironment.h:18
verbose
static const bool verbose
Definition: GenericAStar.cpp:17
GenericAStar::GetName
virtual const char * GetName()
Definition: GenericAStar.cpp:20
GenericAStar.h
GenericAStar::ClosedListIterNext
uint32_t ClosedListIterNext(closedList_iterator &) const
Definition: GenericAStar.cpp:188
GenericAStarUtil::SearchNode::prevNode
uint32_t prevNode
Definition: GenericAStar.h:44
GenericAStar::InitializeSearch
bool InitializeSearch(OldSearchCode::SearchEnvironment *env, uint32_t from, uint32_t to, std::vector< uint32_t > &thePath)
Definition: GenericAStar.cpp:37
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
GenericAStar::CheckNextNode
uint32_t CheckNextNode()
Definition: GenericAStar.cpp:114
UINT32_MAX
#define UINT32_MAX
Definition: GenericAStar.h:22
GenericAStarUtil::SearchNode::fCost
double fCost
Definition: GenericAStar.h:41
GenericAStar::GetNextNode
uint32_t GetNextNode()
Definition: GenericAStar.cpp:119
closedList_iterator
GenericAStarUtil::NodeLookupTable::const_iterator closedList_iterator
Definition: GenericAStar.h:75
GenericAStarUtil::SearchNode
Definition: GenericAStar.h:33
GenericAStar::GetClosedListIter
closedList_iterator GetClosedListIter() const
Definition: GenericAStar.cpp:183
GenericAStarUtil::SearchNode::currNode
uint32_t currNode
Definition: GenericAStar.h:43