HOG2
EPEAStar.h
Go to the documentation of this file.
1 
27 #ifndef EPEAStar_H
28 #define EPEAStar_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<typename state>
52 public:
54  EPEAOpenClosedData(const state &theData, double gCost, double hCost, uint64_t parent, uint64_t openLoc, dataLocation location)
55  :data(theData), g(gCost), h(hCost), parentID(parent), openLocation(openLoc), where(location), special(0) { reopened = false; }
56  state data;
57  double g;
58  double h;
59  uint64_t parentID;
60  uint64_t openLocation;
61  bool reopened;
63  uint64_t special;
64 };
65 
66 template <class state>
69  {
70  if (fequal(i1.g+i1.h, i2.g+i2.h))
71  {
72  return (fless(i1.g, i2.g));
73  }
74  return (fgreater(i1.g+i1.h, i2.g+i2.h));
75  }
76 };
77 
81 template <class state, class action, class environment>
82 class EPEAStar : public GenericSearchAlgorithm<state,action,environment> {
83 public:
84  EPEAStar() { ResetNodeCount(); env = 0; stopAfterGoal = true; weight=1; reopenNodes = false; }
85  virtual ~EPEAStar() {}
86  void GetPath(environment *env, const state& from, const state& to, std::vector<state> &thePath);
87 
88  void GetPath(environment *, const state& , const state& , std::vector<action> & ) { assert(false); };
89 
91  //BucketOpenClosed<state, EPEAStarCompare<state>, EPEAOpenClosedData<xyLoc> > openClosedList;
92  state goal, start;
93 
94  bool InitializeSearch(environment *env, const state& from, const state& to, std::vector<state> &thePath);
95  bool DoSingleSearchStep(std::vector<state> &thePath);
96  void AddAdditionalStartState(state& newState);
97  void AddAdditionalStartState(state& newState, double cost);
98 
99  state CheckNextNode();
100  void ExtractPathToStart(state &node, std::vector<state> &thePath)
101  { uint64_t theID; openClosedList.Lookup(env->GetStateHash(node), theID); ExtractPathToStartFromID(theID, thePath); }
102  void ExtractPathToStartFromID(uint64_t node, std::vector<state> &thePath);
103  virtual const char *GetName();
104 
105  void PrintStats();
108  int GetMemoryUsage();
109 
110  //closedList_iterator GetClosedListIter() const;
111  // void GetClosedListIter(closedList_iterator);
112  // bool ClosedListIterNext(closedList_iterator& it, state& next) const;
113  //bool GetClosedListGCost(state &val, double &gCost) const;
114  bool GetClosedListGCost(const state &val, double &gCost) const;
115  unsigned int GetNumOpenItems() { return openClosedList.OpenSize(); }
116  inline const EPEAOpenClosedData<state> &GetOpenItem(unsigned int which) { return openClosedList.Lookat(openClosedList.GetOpenItem(which)); }
117  inline const int GetNumItems() { return openClosedList.size(); }
118  inline const EPEAOpenClosedData<state> &GetItem(unsigned int which) { return openClosedList.Lookat(which); }
119  bool HaveExpandedState(const state &val)
120  { uint64_t key; return openClosedList.Lookup(env->GetStateHash(val), key) != kNotFound; }
121 
122  void SetReopenNodes(bool re) { reopenNodes = re; }
123  bool GetReopenNodes() { return reopenNodes; }
124 
126 
127  uint64_t GetNodesExpanded() const { return nodesExpanded; }
128  uint64_t GetNodesTouched() const { return nodesTouched; }
129 
131 
132  void SetStopAfterGoal(bool val) { stopAfterGoal = val; }
133  bool GetStopAfterGoal() { return stopAfterGoal; }
134 
135  void OpenGLDraw() const;
136 
137  void SetWeight(double w) {weight = w;}
138 private:
140  environment *env;
142 
143  double weight;
144 
148 };
149 
150 //static const bool verbose = false;
151 
160 template <class state, class action, class environment>
162 {
163  static char name[32];
164  sprintf(name, "EPEAStar[]");
165  return name;
166 }
167 
179 template <class state, class action, class environment>
180 void EPEAStar<state,action,environment>::GetPath(environment *_env, const state& from, const state& to, std::vector<state> &thePath)
181 {
182  //discardcount=0;
183  if (!InitializeSearch(_env, from, to, thePath))
184  {
185  return;
186  }
187  while (!DoSingleSearchStep(thePath)) {}
188 }
189 
200 template <class state, class action, class environment>
201 bool EPEAStar<state,action,environment>::InitializeSearch(environment *_env, const state& from, const state& to, std::vector<state> &thePath)
202 {
203  theHeuristic = _env;
204  thePath.resize(0);
205  //if (useRadius)
206  //std::cout<<"Using radius\n";
207  env = _env;
208  // closedList.clear();
209  // openQueue.reset();
210  // assert(openQueue.size() == 0);
211  // assert(closedList.size() == 0);
212  openClosedList.Reset();
213  //openClosedList.Print();
214  ResetNodeCount();
215  start = from;
216  goal = to;
217 
218  if (env->GoalTest(from, to) && (stopAfterGoal)) //assumes that from and to are valid states
219  {
220  return false;
221  }
222 
223  openClosedList.AddOpenNode(start, env->GetStateHash(start), 0, weight*theHeuristic->HCost(start, goal));
224  //openClosedList.Print();
225 
226  return true;
227 }
228 
234 template <class state, class action, class environment>
236 {
237  openClosedList.AddOpenNode(newState, env->GetStateHash(newState), 0, weight*theHeuristic->HCost(start, goal));
238 }
239 
245 template <class state, class action, class environment>
247 {
248  openClosedList.AddOpenNode(newState, env->GetStateHash(newState), cost, weight*theHeuristic->HCost(start, goal));
249 }
250 
261 template <class state, class action, class environment>
263 {
264  if (openClosedList.OpenSize() == 0)
265  {
266  thePath.resize(0); // no path found!
267  //closedList.clear();
268  return true;
269  }
270  uint64_t nodeid = openClosedList.Peek();
271  const state &currOpenNode = openClosedList.Lookup(nodeid).data;
272 
273  if (!openClosedList.Lookup(nodeid).reopened)
274  uniqueNodesExpanded++;
275  nodesExpanded++;
276 
277  if ((stopAfterGoal) && (env->GoalTest(currOpenNode, goal)))
278  {
279  ExtractPathToStartFromID(nodeid, thePath);
280  // Path is backwards - reverse
281  reverse(thePath.begin(), thePath.end());
282  return true;
283  }
284  state next;
285 
286  bool validMove;
287  bool moreMoves = env->GetNextSuccessor(currOpenNode, goal, next,
288  openClosedList.Lookup(nodeid).h,
289  openClosedList.Lookup(nodeid).special,
290  validMove);
291  if (!moreMoves)
292  {
293  openClosedList.Close();
294  //openClosedList.Print();
295  }
296  if (!validMove)
297  {
298  return false;
299  }
300  else if (moreMoves) {
301  openClosedList.KeyChanged(nodeid);
302  //openClosedList.Print();
303  }
304 
305  double edgeCost = env->GCost(currOpenNode, next);
306 
307  nodesTouched++;
308  //double edgeCost;
309 
310  uint64_t theID;
311  switch (openClosedList.Lookup(env->GetStateHash(next), theID))
312  {
313  case kClosedList:
314  if (reopenNodes)
315  {
316  // do something here...
317  }
318  break;
319  case kOpenList:
320  //edgeCost = env->GCost(openClosedList.Lookup(nodeid).data, neighbors[x]);
321  if (fless(openClosedList.Lookup(nodeid).g+edgeCost, openClosedList.Lookup(theID).g))
322  {
323  openClosedList.Lookup(theID).parentID = nodeid;
324  openClosedList.Lookup(theID).g = openClosedList.Lookup(nodeid).g+edgeCost;
325  openClosedList.KeyChanged(theID);
326  //openClosedList.Print();
327  }
328  break;
329  case kNotFound:
330  openClosedList.AddOpenNode(next,
331  env->GetStateHash(next),
332  openClosedList.Lookup(nodeid).g+edgeCost,
333  weight*theHeuristic->HCost(next, goal),
334  nodeid);
335  //openClosedList.Print();
336  }
337 
338  return false;
339 }
340 
348 template <class state, class action, class environment>
350 {
351  uint64_t key = openClosedList.Peek();
352  return openClosedList.Lookup(key).data;
353  //assert(false);
354  //return openQueue.top().currNode;
355 }
356 
357 
366 template <class state, class action,class environment>
368  std::vector<state> &thePath)
369 {
370  do {
371  thePath.push_back(openClosedList.Lookup(node).data);
372  node = openClosedList.Lookup(node).parentID;
373  } while (openClosedList.Lookup(node).parentID != node);
374  thePath.push_back(openClosedList.Lookup(node).data);
375 }
376 
383 template <class state, class action, class environment>
385 {
386  printf("%u items in closed list\n", (unsigned int)openClosedList.ClosedSize());
387  printf("%u items in open queue\n", (unsigned int)openClosedList.OpenSize());
388 }
389 
397 template <class state, class action, class environment>
399 {
400  return openClosedList.size();
401 }
402 
413 template <class state, class action, class environment>
414 bool EPEAStar<state, action,environment>::GetClosedListGCost(const state &val, double &gCost) const
415 {
416  uint64_t theID;
417  dataLocation loc = openClosedList.Lookup(env->GetStateHash(val), theID);
418  if (loc == kClosedList)
419  {
420  gCost = openClosedList.Lookat(theID).g;
421  return true;
422  }
423  return false;
424 }
425 
432 template <class state, class action, class environment>
434 {
435  double transparency = 1.0;
436  if (openClosedList.size() == 0)
437  return;
438  uint64_t top = -1;
439  if (openClosedList.OpenSize() > 0)
440  top = openClosedList.Peek();
441  for (unsigned int x = 0; x < openClosedList.size(); x++)
442  {
443  const EPEAOpenClosedData<state> &data = openClosedList.Lookat(x);
444  if (x == top)
445  {
446  env->SetColor(1.0, 1.0, 0.0, transparency);
447  env->OpenGLDraw(data.data);
448  }
449  if ((data.where == kOpenList) && (data.reopened))
450  {
451  env->SetColor(0.0, 0.5, 0.5, transparency);
452  env->OpenGLDraw(data.data);
453  }
454  else if (data.where == kOpenList)
455  {
456  env->SetColor(0.0, 1.0, 0.0, transparency);
457  env->OpenGLDraw(data.data);
458  }
459  else if ((data.where == kClosedList) && (data.reopened))
460  {
461  env->SetColor(0.5, 0.0, 0.5, transparency);
462  env->OpenGLDraw(data.data);
463  }
464  else if (data.where == kClosedList)
465  {
466  env->SetColor(1.0, 0.0, 0.0, transparency);
467  env->OpenGLDraw(data.data);
468  }
469  }
470 }
471 
472 #endif
EPEAStar::SetWeight
void SetWeight(double w)
Definition: EPEAStar.h:137
GenericSearchAlgorithm.h
An interface for generic search algorithms.
EPEAStar::env
environment * env
Definition: EPEAStar.h:140
EPEAStar::OpenGLDraw
void OpenGLDraw() const
Draw the open/closed list.
Definition: EPEAStar.h:433
EPEAOpenClosedData::EPEAOpenClosedData
EPEAOpenClosedData()
Definition: EPEAStar.h:53
BucketOpenClosed.h
EPEAStar::ResetNodeCount
void ResetNodeCount()
Definition: EPEAStar.h:107
EPEAStar::CheckNextNode
state CheckNextNode()
Returns the next state on the open list (but doesn't pop it off the queue).
Definition: EPEAStar.h:349
EPEAStar::goal
state goal
Definition: EPEAStar.h:92
EPEAOpenClosedData::data
state data
Definition: EPEAStar.h:56
EPEAStar::GetNodesTouched
uint64_t GetNodesTouched() const
Definition: EPEAStar.h:128
Heuristic
Definition: Heuristic.h:30
EPEAStar::nodesTouched
uint64_t nodesTouched
Definition: EPEAStar.h:139
AStarOpenClosed.h
EPEAStar::GetNumOpenItems
unsigned int GetNumOpenItems()
Definition: EPEAStar.h:115
AStarOpenClosed::Lookat
const dataStructure & Lookat(uint64_t objKey) const
Definition: AStarOpenClosed.h:87
EPEAStar::~EPEAStar
virtual ~EPEAStar()
Definition: EPEAStar.h:85
FPUtil.h
EPEAStar::weight
double weight
Definition: EPEAStar.h:143
EPEAStar::ExtractPathToStart
void ExtractPathToStart(state &node, std::vector< state > &thePath)
Definition: EPEAStar.h:100
EPEAStar::PrintStats
void PrintStats()
A function that prints the number of states in the closed list and open queue.
Definition: EPEAStar.h:384
EPEAStar::SetStopAfterGoal
void SetStopAfterGoal(bool val)
Definition: EPEAStar.h:132
EPEAOpenClosedData::h
double h
Definition: EPEAStar.h:58
EPEAStar::reopenNodes
bool reopenNodes
Definition: EPEAStar.h:145
AStarOpenClosed::size
size_t size() const
Definition: AStarOpenClosed.h:103
EPEAStar::SetReopenNodes
void SetReopenNodes(bool re)
Definition: EPEAStar.h:122
EPEAOpenClosedData::openLocation
uint64_t openLocation
Definition: EPEAStar.h:60
EPEAStar::HaveExpandedState
bool HaveExpandedState(const state &val)
Definition: EPEAStar.h:119
EPEAStar
A templated version of A*, based on HOG genericAStar.
Definition: EPEAStar.h:82
EPEAStar::GetNodesExpanded
uint64_t GetNodesExpanded() const
Definition: EPEAStar.h:127
EPEAStar::GetName
virtual const char * GetName()
Return the name of the algorithm.
Definition: EPEAStar.h:161
EPEAStar::GetUniqueNodesExpanded
uint64_t GetUniqueNodesExpanded()
Definition: EPEAStar.h:106
GenericSearchAlgorithm
Definition: GenericSearchAlgorithm.h:35
EPEAStar::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< state > &thePath)
Expand a single node.
Definition: EPEAStar.h:262
EPEAOpenClosedData::EPEAOpenClosedData
EPEAOpenClosedData(const state &theData, double gCost, double hCost, uint64_t parent, uint64_t openLoc, dataLocation location)
Definition: EPEAStar.h:54
EPEAStar::InitializeSearch
bool InitializeSearch(environment *env, const state &from, const state &to, std::vector< state > &thePath)
Initialize the A* search.
Definition: EPEAStar.h:201
loc
Definition: MapGenerators.cpp:296
EPEAOpenClosedData::g
double g
Definition: EPEAStar.h:57
EPEAOpenClosedData::where
dataLocation where
Definition: EPEAStar.h:62
AStarOpenClosed
Definition: AStarOpenClosed.h:74
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
dataLocation
dataLocation
Definition: AStarOpenClosed.h:27
EPEAStar::start
state start
Definition: EPEAStar.h:92
EPEAStar::GetReopenNodes
bool GetReopenNodes()
Definition: EPEAStar.h:123
EPEAStar::GetClosedListGCost
bool GetClosedListGCost(const state &val, double &gCost) const
Get state from the closed list.
Definition: EPEAStar.h:414
EPEAStar::nodesExpanded
uint64_t nodesExpanded
Definition: EPEAStar.h:139
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
EPEAStarCompare
Definition: EPEAStar.h:67
EPEAStar::GetPath
void GetPath(environment *env, const state &from, const state &to, std::vector< state > &thePath)
Perform an A* search between two states.
Definition: EPEAStar.h:180
EPEAStar::SetHeuristic
void SetHeuristic(Heuristic< state > *h)
Definition: EPEAStar.h:125
AStarOpenClosed::OpenSize
size_t OpenSize() const
Definition: AStarOpenClosed.h:101
EPEAStarCompare::operator()
bool operator()(const EPEAOpenClosedData< state > &i1, const EPEAOpenClosedData< state > &i2) const
Definition: EPEAStar.h:68
kOpenList
@ kOpenList
Definition: AStarOpenClosed.h:28
EPEAStar::AddAdditionalStartState
void AddAdditionalStartState(state &newState)
Add additional start state to the search.
Definition: EPEAStar.h:235
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
EPEAStar::ExtractPathToStartFromID
void ExtractPathToStartFromID(uint64_t node, std::vector< state > &thePath)
Get the path from a goal state to the start state.
Definition: EPEAStar.h:367
EPEAStar::LogFinalStats
void LogFinalStats(StatCollection *)
Definition: EPEAStar.h:130
EPEAStar::theHeuristic
Heuristic< state > * theHeuristic
Definition: EPEAStar.h:147
AStarOpenClosed::Lookup
dataLocation Lookup(uint64_t hashKey, uint64_t &objKey) const
Returns location of object as well as object key.
Definition: AStarOpenClosed.h:263
EPEAStar::GetMemoryUsage
int GetMemoryUsage()
Return the amount of memory used by PEAStar.
Definition: EPEAStar.h:398
EPEAStar::GetItem
const EPEAOpenClosedData< state > & GetItem(unsigned int which)
Definition: EPEAStar.h:118
EPEAStar::uniqueNodesExpanded
uint64_t uniqueNodesExpanded
Definition: EPEAStar.h:146
EPEAStar::stopAfterGoal
bool stopAfterGoal
Definition: EPEAStar.h:141
kNotFound
@ kNotFound
Definition: AStarOpenClosed.h:30
EPEAStar::GetOpenItem
const EPEAOpenClosedData< state > & GetOpenItem(unsigned int which)
Definition: EPEAStar.h:116
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
EPEAStar::GetNumItems
const int GetNumItems()
Definition: EPEAStar.h:117
EPEAStar::GetStopAfterGoal
bool GetStopAfterGoal()
Definition: EPEAStar.h:133
EPEAOpenClosedData::reopened
bool reopened
Definition: EPEAStar.h:61
EPEAStar::EPEAStar
EPEAStar()
Definition: EPEAStar.h:84
EPEAStar::openClosedList
AStarOpenClosed< state, EPEAStarCompare< state >, EPEAOpenClosedData< xyLoc > > openClosedList
Definition: EPEAStar.h:88
EPEAOpenClosedData::special
uint64_t special
Definition: EPEAStar.h:63
EPEAOpenClosedData::parentID
uint64_t parentID
Definition: EPEAStar.h:59
EPEAOpenClosedData
Definition: EPEAStar.h:51
kClosedList
@ kClosedList
Definition: AStarOpenClosed.h:29
node
Nodes to be stored within a Graph.
Definition: Graph.h:170