HOG2
GenericAStar.h
Go to the documentation of this file.
1 /*
2  * $Id: GenericAStar.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 10/30/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 
13 #ifndef GENERICSTAR_H
14 #define GENERICSTAR_H
15 
16 #define __STDC_CONSTANT_MACROS
17 #include <stdint.h>
18 // this is defined in stdint.h, but it doesn't always get defined correctly
19 // even when __STDC_CONSTANT_MACROS is defined before including stdint.h
20 // because stdint might be included elsewhere first...
21 #ifndef UINT32_MAX
22 #define UINT32_MAX 4294967295U
23 #endif
24 
25 #include "FPUtil.h"
26 #include "OpenClosedList.h"
27 #include "OldSearchEnvironment.h" // for the SearchEnvironment class
28 
29 #include <unordered_map>
30 
32 {
33  class SearchNode {
34 public:
35  SearchNode(double _fCost=0, double _gCost=0, uint32_t curr=0, uint32_t prev=0)
36  :fCost(_fCost), gCost(_gCost), currNode(curr), prevNode(prev) {}
37 
38  SearchNode(uint32_t curr)
39  :fCost(0), gCost(0), currNode(curr), prevNode(0) {}
40 
41  double fCost;
42  double gCost;
43  uint32_t currNode;
44  uint32_t prevNode;
45  };
46 
47  struct SearchNodeEqual {
48  bool operator()(const SearchNode &i1, const SearchNode &i2) const
49  { return (i1.currNode == i2.currNode); } };
50 
52  bool operator()(const SearchNode &i1, const SearchNode &i2) const
53  {
54  if (fequal(i1.fCost, i2.fCost))
55  {
56  return (fless(i1.gCost, i2.gCost));
57  }
58  return (fgreater(i1.fCost, i2.fCost));
59  } };
60 
61  struct SearchNodeHash {
62  size_t operator()(const SearchNode &x) const
63  { return (size_t)(x.currNode); }
64  };
65 
68 
69  typedef std::unordered_map<uint32_t, GenericAStarUtil::SearchNode > NodeLookupTable;
70 
71  typedef std::unordered_map<uint32_t, bool > Corridor;
72 }
73 
74 
75 typedef GenericAStarUtil::NodeLookupTable::const_iterator closedList_iterator;
76 
77 class GenericAStar {
78 public:
80  virtual ~GenericAStar() {}
81  void GetPath(OldSearchCode::SearchEnvironment *env, uint32_t from, uint32_t to,
82  std::vector<uint32_t> &thePath);
83 
84  bool InitializeSearch(OldSearchCode::SearchEnvironment *env, uint32_t from, uint32_t to,
85  std::vector<uint32_t> &thePath);
86  bool DoSingleSearchStep(std::vector<uint32_t> &thePath);
87  uint32_t CheckNextNode();
88  void ExtractPathToStart(uint32_t n, std::vector<uint32_t> &thePath);
89 
90  virtual const char *GetName();
91 
92  void PrintStats();
93  uint64_t GetNodesExpanded() { return nodesExpanded; }
94  uint64_t GetNodesTouched() { return nodesTouched; }
96  int GetMemoryUsage();
97 
99  uint32_t ClosedListIterNext(closedList_iterator&) const;
100 
101 private:
103 
104  uint32_t GetNextNode();
105  void UpdateWeight(uint32_t currOpenNode, uint32_t neighbor);
106  void AddToOpenList(uint32_t currOpenNode, uint32_t neighbor);
109  uint32_t goal, start;
110 
111  std::vector<uint32_t> neighbors;
114 };
115 
116 #endif
GenericAStar::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< uint32_t > &thePath)
Definition: GenericAStar.cpp:59
GenericAStar::goal
uint32_t goal
Definition: GenericAStar.h:109
GenericAStar::eligibleNodes
GenericAStarUtil::Corridor eligibleNodes
Definition: GenericAStar.h:113
GenericAStar::GenericAStar
GenericAStar()
Definition: GenericAStar.h:79
GenericAStarUtil::SearchNodeHash::operator()
size_t operator()(const SearchNode &x) const
Definition: GenericAStar.h:62
graphMove
Definition: GraphEnvironment.h:34
GenericAStar::PrintStats
void PrintStats()
Definition: GenericAStar.cpp:172
GenericAStar::ResetNodeCount
void ResetNodeCount()
Definition: GenericAStar.h:95
GenericAStar::start
uint32_t start
Definition: GenericAStar.h:109
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
FPUtil.h
OpenClosedList.h
GenericAStar::GetNodesTouched
uint64_t GetNodesTouched()
Definition: GenericAStar.h:94
GenericAStar::GetPath
void GetPath(OldSearchCode::SearchEnvironment *env, uint32_t from, uint32_t to, std::vector< uint32_t > &thePath)
Definition: GenericAStar.cpp:28
GenericAStarUtil::NodeLookupTable
std::unordered_map< uint32_t, GenericAStarUtil::SearchNode > NodeLookupTable
Definition: GenericAStar.h:69
GenericAStarUtil::SearchNode::SearchNode
SearchNode(double _fCost=0, double _gCost=0, uint32_t curr=0, uint32_t prev=0)
Definition: GenericAStar.h:35
GenericAStar::AddToOpenList
void AddToOpenList(uint32_t currOpenNode, uint32_t neighbor)
Definition: GenericAStar.cpp:144
OldSearchCode::SearchEnvironment
Definition: OldSearchEnvironment.h:18
GenericAStarUtil::SearchNodeCompare
Definition: GenericAStar.h:51
GenericAStar::nodesTouched
uint64_t nodesTouched
Definition: GenericAStar.h:102
OldSearchEnvironment.h
GenericAStar::GetName
virtual const char * GetName()
Definition: GenericAStar.cpp:20
GenericAStar
Definition: GenericAStar.h:77
GenericAStar::ClosedListIterNext
uint32_t ClosedListIterNext(closedList_iterator &) const
Definition: GenericAStar.cpp:188
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
GenericAStarUtil::SearchNode::SearchNode
SearchNode(uint32_t curr)
Definition: GenericAStar.h:38
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
OpenClosedList
A simple Heap class.
Definition: OpenClosedList.h:27
GenericAStarUtil::SearchNodeEqual::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: GenericAStar.h:48
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
GenericAStar::CheckNextNode
uint32_t CheckNextNode()
Definition: GenericAStar.cpp:114
GenericAStar::env
OldSearchCode::SearchEnvironment * env
Definition: GenericAStar.h:112
GenericAStar::openQueue
GenericAStarUtil::PQueue openQueue
Definition: GenericAStar.h:107
GenericAStar::nodesExpanded
uint64_t nodesExpanded
Definition: GenericAStar.h:102
GenericAStar::neighbors
std::vector< uint32_t > neighbors
Definition: GenericAStar.h:111
GenericAStarUtil::SearchNode::fCost
double fCost
Definition: GenericAStar.h:41
GenericAStarUtil::Corridor
std::unordered_map< uint32_t, bool > Corridor
Definition: GenericAStar.h:71
GenericAStar::closedList
GenericAStarUtil::NodeLookupTable closedList
Definition: GenericAStar.h:108
GenericAStar::GetNextNode
uint32_t GetNextNode()
Definition: GenericAStar.cpp:119
GenericAStarUtil::PQueue
OpenClosedList< GenericAStarUtil::SearchNode, GenericAStarUtil::SearchNodeHash, GenericAStarUtil::SearchNodeEqual, GenericAStarUtil::SearchNodeCompare > PQueue
Definition: GenericAStar.h:67
closedList_iterator
GenericAStarUtil::NodeLookupTable::const_iterator closedList_iterator
Definition: GenericAStar.h:75
GenericAStarUtil::SearchNodeHash
Definition: GenericAStar.h:61
GenericAStar::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: GenericAStar.h:93
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
GenericAStar::~GenericAStar
virtual ~GenericAStar()
Definition: GenericAStar.h:80
GenericAStarUtil::SearchNodeCompare::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: GenericAStar.h:52
GenericAStarUtil::SearchNodeEqual
Definition: GenericAStar.h:47
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