HOG2
AStar.h
Go to the documentation of this file.
1 /*
2  * $Id: aStar.h
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 
13 #ifndef ASTAR3_H
14 #define ASTAR3_H
15 
16 #include "SearchAlgorithm.h"
17 #include "Path.h"
18 #include "GraphAbstraction.h"
19 #include "FPUtil.h"
20 #include "OpenClosedList.h"
21 
22 #include <unordered_map>
23 
24 namespace AStar3Util
25 {
26  class SearchNode {
27 public:
28  SearchNode(double _fCost=0, double _gCost=0, edge *_e=0, node *curr=0, node *prev=0)
29  :fCost(_fCost), gCost(_gCost), e(_e), currNode(curr), prevNode(prev) {}
30 
32  :fCost(0), gCost(0), e(0), currNode(curr), prevNode(0) {}
33 
34  double fCost;
35  double gCost;
36 // double steps;
37  edge *e;
40  };
41 
42 
43  struct NodeEqual {
44  bool operator()(const node *i1, const node *i2) const
45  { return (i1->getUniqueID() == i2->getUniqueID()); } };
46 
47  struct SearchNodeEqual {
48  bool operator()(const SearchNode &i1, const SearchNode &i2) const
49  { return (i1.currNode->getUniqueID() == i2.currNode->getUniqueID()); } };
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 NodeHash {
62  size_t operator()(const node *x) const
63  { return (size_t)(x->getUniqueID()); }
64  };
65 
66  struct SearchNodeHash {
67  size_t operator()(const SearchNode &x) const
68  { return (size_t)(x.currNode->getUniqueID()); }
69  };
70 
73 
74  typedef std::unordered_map<node*, AStar3Util::SearchNode,
76 
77  typedef std::unordered_map<node*, bool,
79 }
80 
81 class aStar : public SearchAlgorithm {
82 public:
83  aStar() {}
84  virtual ~aStar() {}
85  path *GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp = 0);
86  virtual const char *GetName();
87 
88  double getHVal(node *whence);
89  void setCorridor(path *corridor, int width);
90 
91  void printStats();
92  uint64_t GetNodesExpanded() { return nodesExpanded; }
93  uint64_t GetNodesTouched() { return nodesTouched; }
95  int getMemoryUsage();
96 private:
97  // long nodesTouched, nodesExpanded;
98  inline node *ABSNode(node *n) { return abstr->GetNthParent(n, absLevel); }
101  node *getNextNode();
102  void updateWeight(node *currOpenNode, node *neighbor, edge *e);
103  void addToOpenList(node *currOpenNode, node *neighbor, edge *e);
104  bool nodeInCorridor(node *n);
105  void addNeighborsToCorridor(Graph *g, node *n, int windowSize);
106  void buildCorridor(path *p, int windowSize);
107  double internalHeuristic(node *from, node *to);
114  int absLevel;
115  // AStarHeuristic *abstraction;
116 };
117 
118 #endif
aStar::buildCorridor
void buildCorridor(path *p, int windowSize)
Definition: AStar.cpp:221
aStar::goal
node * goal
Definition: AStar.h:110
GraphAbstraction
A generic class for basic operations on Graph abstractions.
Definition: GraphAbstraction.h:63
AStar3Util::SearchNodeCompare::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: AStar.h:52
AStar3Util
Definition: AStar.h:24
aStar::addNeighborsToCorridor
void addNeighborsToCorridor(Graph *g, node *n, int windowSize)
Definition: AStar.cpp:228
aStar::ABSNode
node * ABSNode(node *n)
Definition: AStar.h:98
AStar3Util::SearchNode::SearchNode
SearchNode(double _fCost=0, double _gCost=0, edge *_e=0, node *curr=0, node *prev=0)
Definition: AStar.h:28
graphMove
Definition: GraphEnvironment.h:34
aStar::start
node * start
Definition: AStar.h:110
AStar3Util::SearchNode
Definition: AStar.h:26
SearchAlgorithm.h
Path.h
AStar3Util::SearchNodeEqual::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: AStar.h:48
AStar3Util::NodeEqual
Definition: AStar.h:43
AStar3Util::SearchNode::prevNode
node * prevNode
Definition: AStar.h:39
FPUtil.h
aStar::GetNodesTouched
uint64_t GetNodesTouched()
Definition: AStar.h:93
width
int width
Definition: SFML_HOG.cpp:54
aStar::getMemoryUsage
int getMemoryUsage()
Definition: AStar.cpp:216
SearchAlgorithm::nodesTouched
uint32_t nodesTouched
Definition: SearchAlgorithm.h:37
OpenClosedList.h
AStar3Util::SearchNode::currNode
node * currNode
Definition: AStar.h:38
aStar::absLevel
int absLevel
Definition: AStar.h:114
aStar
Definition: AStar.h:81
Graph
A generic Graph class.
Definition: Graph.h:66
aStar::GetName
virtual const char * GetName()
Definition: AStar.cpp:21
AStar3Util::SearchNodeHash
Definition: AStar.h:66
aStar::g
Graph * g
Definition: AStar.h:111
aStar::openQueue
AStar3Util::PQueue openQueue
Definition: AStar.h:108
AStar3Util::NodeEqual::operator()
bool operator()(const node *i1, const node *i2) const
Definition: AStar.h:44
aStar::resetNodeCount
void resetNodeCount()
Definition: AStar.h:94
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
AStar3Util::SearchNode::e
edge * e
Definition: AStar.h:37
SearchAlgorithm::nodesExpanded
uint32_t nodesExpanded
Definition: SearchAlgorithm.h:36
aStar::abstr
GraphAbstraction * abstr
Definition: AStar.h:112
aStar::getNextNode
node * getNextNode()
Definition: AStar.cpp:124
node::getUniqueID
int getUniqueID() const
Definition: Graph.h:177
aStar::updateWeight
void updateWeight(node *currOpenNode, node *neighbor, edge *e)
Definition: AStar.cpp:147
aStar::internalHeuristic
double internalHeuristic(node *from, node *to)
Definition: AStar.cpp:256
OpenClosedList
A simple Heap class.
Definition: OpenClosedList.h:27
aStar::printStats
void printStats()
Definition: AStar.cpp:209
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
aStar::getPathToNode
path * getPathToNode(node *target, reservationProvider *rp)
Definition: AStar.cpp:58
AStar3Util::SearchNodeCompare
Definition: AStar.h:51
aStar::nodeInCorridor
bool nodeInCorridor(node *n)
Definition: AStar.cpp:250
aStar::addToOpenList
void addToOpenList(node *currOpenNode, node *neighbor, edge *e)
Definition: AStar.cpp:169
aStar::setCorridor
void setCorridor(path *corridor, int width)
Definition: AStar.cpp:52
AStar3Util::SearchNode::SearchNode
SearchNode(node *curr)
Definition: AStar.h:31
AStar3Util::Corridor
std::unordered_map< node *, bool, AStar3Util::NodeHash, AStar3Util::NodeEqual > Corridor
Definition: AStar.h:78
AStar3Util::SearchNode::fCost
double fCost
Definition: AStar.h:34
reservationProvider
Definition: ReservationProvider.h:33
AStar3Util::NodeLookupTable
std::unordered_map< node *, AStar3Util::SearchNode, AStar3Util::NodeHash, AStar3Util::NodeEqual > NodeLookupTable
Definition: AStar.h:75
SearchAlgorithm
A generic algorithm which can be used for pathfinding.
Definition: SearchAlgorithm.h:25
aStar::eligibleNodes
AStar3Util::Corridor eligibleNodes
Definition: AStar.h:113
aStar::aStar
aStar()
Definition: AStar.h:83
AStar3Util::SearchNodeEqual
Definition: AStar.h:47
aStar::closedList
AStar3Util::NodeLookupTable closedList
Definition: AStar.h:109
AStar3Util::NodeHash
Definition: AStar.h:61
aStar::GetPath
path * GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp=0)
Definition: AStar.cpp:31
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
AStar3Util::SearchNodeHash::operator()
size_t operator()(const SearchNode &x) const
Definition: AStar.h:67
aStar::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: AStar.h:92
AStar3Util::SearchNode::gCost
double gCost
Definition: AStar.h:35
AStar3Util::PQueue
OpenClosedList< AStar3Util::SearchNode, AStar3Util::SearchNodeHash, AStar3Util::SearchNodeEqual, AStar3Util::SearchNodeCompare > PQueue
Definition: AStar.h:72
AStar3Util::NodeHash::operator()
size_t operator()(const node *x) const
Definition: AStar.h:62
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
aStar::extractPathToStart
path * extractPathToStart(Graph *g, node *n)
Definition: AStar.cpp:195
aStar::getHVal
double getHVal(node *whence)
GraphAbstraction::GetNthParent
node * GetNthParent(node *which, int n)
return nth level parent of which or null if it doesn't exist
Definition: GraphAbstraction.cpp:67
aStar::~aStar
virtual ~aStar()
Definition: AStar.h:84
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
GraphAbstraction.h
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129