HOG2
PEAStar.h
Go to the documentation of this file.
1 
27 #ifndef PEAStar_H
28 #define PEAStar_H
29 
30 #define __STDC_CONSTANT_MACROS
31 #include <stdint.h>
32 // this is defined in stdint.h, but it doesn't always get defined correctly
33 // even when __STDC_CONSTANT_MACROS is defined before including stdint.h
34 // because stdint might be included elsewhere first...
35 #ifndef UINT32_MAX
36 #define UINT32_MAX 4294967295U
37 #endif
38 
39 #include "FPUtil.h"
40 #include <unordered_map>
41 #include "AStarOpenClosed.h"
42 #include "BucketOpenClosed.h"
43 //#include "SearchEnvironment.h" // for the SearchEnvironment class
44 #include "float.h"
45 
46 #include <algorithm> // for vector reverse
47 
48 #include "GenericSearchAlgorithm.h"
49 
50 template <class state>
53  {
54  if (fequal(i1.g+i1.h, i2.g+i2.h))
55  {
56  return (fless(i1.g, i2.g));
57  }
58  return (fgreater(i1.g+i1.h, i2.g+i2.h));
59  }
60 };
61 
65 template <class state, class action, class environment>
66 class PEAStar : public GenericSearchAlgorithm<state,action,environment> {
67 public:
68  PEAStar() { ResetNodeCount(); env = 0; stopAfterGoal = true; weight=1; reopenNodes = false; }
69  virtual ~PEAStar() {}
70  void GetPath(environment *env, const state& from, const state& to, std::vector<state> &thePath);
71 
72  void GetPath(environment *, const state& , const state& , std::vector<action> & ) { assert(false); };
73 
75  //BucketOpenClosed<state, PEAStarCompare<state> > openClosedList;
76  state goal, start;
77 
78  bool InitializeSearch(environment *env, const state& from, const state& to, std::vector<state> &thePath);
79  bool DoSingleSearchStep(std::vector<state> &thePath);
80  void AddAdditionalStartState(state& newState);
81  void AddAdditionalStartState(state& newState, double cost);
82 
83  state CheckNextNode();
84  void ExtractPathToStart(state &node, std::vector<state> &thePath)
85  { uint64_t theID; openClosedList.Lookup(env->GetStateHash(node), theID); ExtractPathToStartFromID(theID, thePath); }
86  void ExtractPathToStartFromID(uint64_t node, std::vector<state> &thePath);
87  virtual const char *GetName();
88 
89  void PrintStats();
92  int GetMemoryUsage();
93 
94  //closedList_iterator GetClosedListIter() const;
95  // void GetClosedListIter(closedList_iterator);
96  // bool ClosedListIterNext(closedList_iterator& it, state& next) const;
97  //bool GetClosedListGCost(state &val, double &gCost) const;
98  bool GetClosedListGCost(const state &val, double &gCost) const;
99  unsigned int GetNumOpenItems() { return openClosedList.OpenSize(); }
100  inline const AStarOpenClosedData<state> &GetOpenItem(unsigned int which) { return openClosedList.Lookat(openClosedList.GetOpenItem(which)); }
101  inline const int GetNumItems() { return openClosedList.size(); }
102  inline const AStarOpenClosedData<state> &GetItem(unsigned int which) { return openClosedList.Lookat(which); }
103  bool HaveExpandedState(const state &val)
104  { uint64_t key; return openClosedList.Lookup(env->GetStateHash(val), key) != kNotFound; }
105 
106  void SetReopenNodes(bool re) { reopenNodes = re; }
107  bool GetReopenNodes() { return reopenNodes; }
108 
110 
111  uint64_t GetNodesExpanded() const { return nodesExpanded; }
112  uint64_t GetNodesTouched() const { return nodesTouched; }
113 
115 
116  void SetStopAfterGoal(bool val) { stopAfterGoal = val; }
117  bool GetStopAfterGoal() { return stopAfterGoal; }
118 
119  void OpenGLDraw() const;
120 
121  void SetWeight(double w) {weight = w;}
122 private:
123  std::vector<state> succ;
125  environment *env;
127 
128  double weight;
129 
133 };
134 
135 //static const bool verbose = false;
136 
145 template <class state, class action, class environment>
147 {
148  static char name[32];
149  sprintf(name, "PEAStar[]");
150  return name;
151 }
152 
164 template <class state, class action, class environment>
165 void PEAStar<state,action,environment>::GetPath(environment *_env, const state& from, const state& to, std::vector<state> &thePath)
166 {
167  //discardcount=0;
168  if (!InitializeSearch(_env, from, to, thePath))
169  {
170  return;
171  }
172  while (!DoSingleSearchStep(thePath)) {}
173 }
174 
185 template <class state, class action, class environment>
186 bool PEAStar<state,action,environment>::InitializeSearch(environment *_env, const state& from, const state& to, std::vector<state> &thePath)
187 {
188  theHeuristic = _env;
189  thePath.resize(0);
190  //if (useRadius)
191  //std::cout<<"Using radius\n";
192  env = _env;
193  // closedList.clear();
194  // openQueue.reset();
195  // assert(openQueue.size() == 0);
196  // assert(closedList.size() == 0);
197  openClosedList.Reset();
198  //openClosedList.Print();
199  ResetNodeCount();
200  start = from;
201  goal = to;
202 
203  if (env->GoalTest(from, to) && (stopAfterGoal)) //assumes that from and to are valid states
204  {
205  return false;
206  }
207 
208  openClosedList.AddOpenNode(start, env->GetStateHash(start), 0, weight*theHeuristic->HCost(start, goal));
209  //openClosedList.Print();
210 
211  return true;
212 }
213 
219 template <class state, class action, class environment>
221 {
222  openClosedList.AddOpenNode(newState, env->GetStateHash(newState), 0, weight*theHeuristic->HCost(start, goal));
223 }
224 
230 template <class state, class action, class environment>
232 {
233  openClosedList.AddOpenNode(newState, env->GetStateHash(newState), cost, weight*theHeuristic->HCost(start, goal));
234 }
235 
246 template <class state, class action, class environment>
248 {
249  if (openClosedList.OpenSize() == 0)
250  {
251  thePath.resize(0); // no path found!
252  //closedList.clear();
253  return true;
254  }
255  uint64_t nodeid = openClosedList.Peek();
256  const state &currOpenNode = openClosedList.Lookup(nodeid).data;
257 
258  if (!openClosedList.Lookup(nodeid).reopened)
259  uniqueNodesExpanded++;
260  nodesExpanded++;
261 
262  if ((stopAfterGoal) && (env->GoalTest(currOpenNode, goal)))
263  {
264  ExtractPathToStartFromID(nodeid, thePath);
265  // Path is backwards - reverse
266  reverse(thePath.begin(), thePath.end());
267  return true;
268  }
269 
270  env->GetSuccessors(currOpenNode, succ);
271  double fCost = openClosedList.Lookup(nodeid).h+openClosedList.Lookup(nodeid).g;
272  bool keep = false;
273 
274  nodesTouched++;
275  //double edgeCost;
276  double nextBest = 0;
277  uint64_t theID;
278  for (unsigned int x = 0; x < succ.size(); x++)
279  {
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);
283  if ((loc == kClosedList) ||
284  (loc == kOpenList && !fless(newFCost, openClosedList.Lookup(theID).g+openClosedList.Lookup(theID).h)))
285  {
286  // ignore!
287  }
288  else if (fgreater(newFCost, fCost))
289  {
290  keep = true;
291  if (nextBest == 0)
292  nextBest = newFCost;
293  else if (newFCost < nextBest)
294  nextBest = newFCost;
295  }
296  }
297  if (!keep)
298  {
299  openClosedList.Close();
300  }
301  else {
302  //printf("Increasing cost by %f\n", nextBest-fCost);
303  openClosedList.Lookup(nodeid).h += nextBest-fCost;
304  openClosedList.KeyChanged(nodeid);
305  }
306 
307 
308  for (unsigned int x = 0; x < succ.size(); x++)
309  {
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))
313  continue;
314 
315  switch (openClosedList.Lookup(env->GetStateHash(succ[x]), theID))
316  {
317  case kClosedList:
318  if (reopenNodes)
319  {
320  // do something here...
321  }
322  break;
323  case kOpenList:
324  //edgeCost = env->GCost(openClosedList.Lookup(nodeid).data, neighbors[x]);
325  if (fless(openClosedList.Lookup(nodeid).g+edgeCost, openClosedList.Lookup(theID).g))
326  {
327  openClosedList.Lookup(theID).parentID = nodeid;
328  openClosedList.Lookup(theID).g = openClosedList.Lookup(nodeid).g+edgeCost;
329  openClosedList.KeyChanged(theID);
330  //openClosedList.Print();
331  }
332  break;
333  case kNotFound:
334 
335  if (fequal(newFCost, fCost))
336  {
337  openClosedList.AddOpenNode(succ[x],
338  env->GetStateHash(succ[x]),
339  openClosedList.Lookup(nodeid).g+edgeCost,
340  weight*theHeuristic->HCost(succ[x], goal),
341  nodeid);
342  }
343  //openClosedList.Print();
344  }
345  }
346  return false;
347 }
348 
356 template <class state, class action, class environment>
358 {
359  uint64_t key = openClosedList.Peek();
360  return openClosedList.Lookup(key).data;
361  //assert(false);
362  //return openQueue.top().currNode;
363 }
364 
365 
374 template <class state, class action,class environment>
376  std::vector<state> &thePath)
377 {
378  do {
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);
383 }
384 
391 template <class state, class action, class environment>
393 {
394  printf("%u items in closed list\n", (unsigned int)openClosedList.ClosedSize());
395  printf("%u items in open queue\n", (unsigned int)openClosedList.OpenSize());
396 }
397 
405 template <class state, class action, class environment>
407 {
408  return openClosedList.size();
409 }
410 
421 template <class state, class action, class environment>
422 bool PEAStar<state, action,environment>::GetClosedListGCost(const state &val, double &gCost) const
423 {
424  uint64_t theID;
425  dataLocation loc = openClosedList.Lookup(env->GetStateHash(val), theID);
426  if (loc == kClosedList)
427  {
428  gCost = openClosedList.Lookat(theID).g;
429  return true;
430  }
431  return false;
432 }
433 
440 template <class state, class action, class environment>
442 {
443  double transparency = 1.0;
444  if (openClosedList.size() == 0)
445  return;
446  uint64_t top = -1;
447  if (openClosedList.OpenSize() > 0)
448  top = openClosedList.Peek();
449  for (unsigned int x = 0; x < openClosedList.size(); x++)
450  {
451  const AStarOpenClosedData<state> &data = openClosedList.Lookat(x);
452  if (x == top)
453  {
454  env->SetColor(1.0, 1.0, 0.0, transparency);
455  env->OpenGLDraw(data.data);
456  }
457  if ((data.where == kOpenList) && (data.reopened))
458  {
459  env->SetColor(0.0, 0.5, 0.5, transparency);
460  env->OpenGLDraw(data.data);
461  }
462  else if (data.where == kOpenList)
463  {
464  env->SetColor(0.0, 1.0, 0.0, transparency);
465  env->OpenGLDraw(data.data);
466  }
467  else if ((data.where == kClosedList) && (data.reopened))
468  {
469  env->SetColor(0.5, 0.0, 0.5, transparency);
470  env->OpenGLDraw(data.data);
471  }
472  else if (data.where == kClosedList)
473  {
474  env->SetColor(1.0, 0.0, 0.0, transparency);
475  env->OpenGLDraw(data.data);
476  }
477  }
478 }
479 
480 #endif
GenericSearchAlgorithm.h
An interface for generic search algorithms.
PEAStar::nodesTouched
uint64_t nodesTouched
Definition: PEAStar.h:124
PEAStar::nodesExpanded
uint64_t nodesExpanded
Definition: PEAStar.h:124
BucketOpenClosed.h
AStarOpenClosedData::reopened
bool reopened
Definition: AStarOpenClosed.h:68
AStarOpenClosedData::data
state data
Definition: AStarOpenClosed.h:57
Heuristic
Definition: Heuristic.h:30
PEAStar::SetReopenNodes
void SetReopenNodes(bool re)
Definition: PEAStar.h:106
AStarOpenClosed.h
PEAStar::theHeuristic
Heuristic< state > * theHeuristic
Definition: PEAStar.h:132
AStarOpenClosed::Lookat
const dataStructure & Lookat(uint64_t objKey) const
Definition: AStarOpenClosed.h:87
PEAStar::uniqueNodesExpanded
uint64_t uniqueNodesExpanded
Definition: PEAStar.h:131
PEAStar::GetOpenItem
const AStarOpenClosedData< state > & GetOpenItem(unsigned int which)
Definition: PEAStar.h:100
FPUtil.h
AStarOpenClosed::size
size_t size() const
Definition: AStarOpenClosed.h:103
PEAStarCompare::operator()
bool operator()(const AStarOpenClosedData< state > &i1, const AStarOpenClosedData< state > &i2) const
Definition: PEAStar.h:52
PEAStar::SetHeuristic
void SetHeuristic(Heuristic< state > *h)
Definition: PEAStar.h:109
AStarOpenClosedData::where
dataLocation where
Definition: AStarOpenClosed.h:69
PEAStar::start
state start
Definition: PEAStar.h:76
PEAStar::SetWeight
void SetWeight(double w)
Definition: PEAStar.h:121
PEAStar::GetNodesExpanded
uint64_t GetNodesExpanded() const
Definition: PEAStar.h:111
PEAStar::succ
std::vector< state > succ
Definition: PEAStar.h:123
AStarOpenClosedData::g
double g
Definition: AStarOpenClosed.h:64
GenericSearchAlgorithm
Definition: GenericSearchAlgorithm.h:35
PEAStar::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< state > &thePath)
Expand a single node.
Definition: PEAStar.h:247
PEAStar::GetClosedListGCost
bool GetClosedListGCost(const state &val, double &gCost) const
Get state from the closed list.
Definition: PEAStar.h:422
PEAStar::InitializeSearch
bool InitializeSearch(environment *env, const state &from, const state &to, std::vector< state > &thePath)
Initialize the A* search.
Definition: PEAStar.h:186
PEAStar::GetUniqueNodesExpanded
uint64_t GetUniqueNodesExpanded()
Definition: PEAStar.h:90
loc
Definition: MapGenerators.cpp:296
PEAStar::GetItem
const AStarOpenClosedData< state > & GetItem(unsigned int which)
Definition: PEAStar.h:102
PEAStar::GetPath
void GetPath(environment *env, const state &from, const state &to, std::vector< state > &thePath)
Perform an A* search between two states.
Definition: PEAStar.h:165
AStarOpenClosed
Definition: AStarOpenClosed.h:74
PEAStar::PrintStats
void PrintStats()
A function that prints the number of states in the closed list and open queue.
Definition: PEAStar.h:392
PEAStar::LogFinalStats
void LogFinalStats(StatCollection *)
Definition: PEAStar.h:114
PEAStar::OpenGLDraw
void OpenGLDraw() const
Draw the open/closed list.
Definition: PEAStar.h:441
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
PEAStar::PEAStar
PEAStar()
Definition: PEAStar.h:68
PEAStar::GetNumOpenItems
unsigned int GetNumOpenItems()
Definition: PEAStar.h:99
PEAStar::GetNodesTouched
uint64_t GetNodesTouched() const
Definition: PEAStar.h:112
PEAStar::weight
double weight
Definition: PEAStar.h:128
AStarOpenClosedData
Definition: AStarOpenClosed.h:52
dataLocation
dataLocation
Definition: AStarOpenClosed.h:27
PEAStar::GetNumItems
const int GetNumItems()
Definition: PEAStar.h:101
PEAStar::goal
state goal
Definition: PEAStar.h:76
PEAStar::ResetNodeCount
void ResetNodeCount()
Definition: PEAStar.h:91
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
PEAStar::GetStopAfterGoal
bool GetStopAfterGoal()
Definition: PEAStar.h:117
PEAStar::ExtractPathToStartFromID
void ExtractPathToStartFromID(uint64_t node, std::vector< state > &thePath)
Get the path from a goal state to the start state.
Definition: PEAStar.h:375
AStarOpenClosed::OpenSize
size_t OpenSize() const
Definition: AStarOpenClosed.h:101
PEAStar::env
environment * env
Definition: PEAStar.h:125
kOpenList
@ kOpenList
Definition: AStarOpenClosed.h:28
PEAStar::GetMemoryUsage
int GetMemoryUsage()
Return the amount of memory used by PEAStar.
Definition: PEAStar.h:406
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
AStarOpenClosed::GetOpenItem
uint64_t GetOpenItem(unsigned int which)
Definition: AStarOpenClosed.h:100
PEAStar::CheckNextNode
state CheckNextNode()
Returns the next state on the open list (but doesn't pop it off the queue).
Definition: PEAStar.h:357
PEAStar::ExtractPathToStart
void ExtractPathToStart(state &node, std::vector< state > &thePath)
Definition: PEAStar.h:84
PEAStar::reopenNodes
bool reopenNodes
Definition: PEAStar.h:130
AStarOpenClosed::Lookup
dataLocation Lookup(uint64_t hashKey, uint64_t &objKey) const
Returns location of object as well as object key.
Definition: AStarOpenClosed.h:263
AStarOpenClosedData::h
double h
Definition: AStarOpenClosed.h:65
PEAStar::~PEAStar
virtual ~PEAStar()
Definition: PEAStar.h:69
PEAStar::openClosedList
AStarOpenClosed< state, PEAStarCompare< state > > openClosedList
Definition: PEAStar.h:72
PEAStar::GetReopenNodes
bool GetReopenNodes()
Definition: PEAStar.h:107
PEAStarCompare
Definition: PEAStar.h:51
kNotFound
@ kNotFound
Definition: AStarOpenClosed.h:30
PEAStar::AddAdditionalStartState
void AddAdditionalStartState(state &newState)
Add additional start state to the search.
Definition: PEAStar.h:220
PEAStar
A templated version of A*, based on HOG genericAStar.
Definition: PEAStar.h:66
PEAStar::stopAfterGoal
bool stopAfterGoal
Definition: PEAStar.h:126
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
PEAStar::SetStopAfterGoal
void SetStopAfterGoal(bool val)
Definition: PEAStar.h:116
kClosedList
@ kClosedList
Definition: AStarOpenClosed.h:29
PEAStar::HaveExpandedState
bool HaveExpandedState(const state &val)
Definition: PEAStar.h:103
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
PEAStar::GetName
virtual const char * GetName()
Return the name of the algorithm.
Definition: PEAStar.h:146