HOG2
AStarDelay.h
Go to the documentation of this file.
1 /*
2  * AStarDelay.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 9/27/07.
6  * Copyright 2007 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 #ifndef ASTARDELAY_H
11 #define ASTARDELAY_H
12 
13 #include <math.h>
14 #include "GraphAlgorithm.h"
15 #include "SearchEnvironment.h"
16 #include "GraphEnvironment.h"
17 #include <unordered_map>
18 #include "FPUtil.h"
19 #include "OpenListB.h"
20 #include <deque>
21 
22 #ifndef UINT32_MAX
23 #define UINT32_MAX 4294967295U
24 #endif
25 
26 
27 #define D_ONE 1
28 #define D_TWO 2
29 #define D_THREE 3
30 #define D_FOUR 4
31 #define D_LOG2 5
32 #define D_SQRT 6
33 //const double pi = 3.141592654;
34 
35 //typedef std::unordered_map<uint64_t, double> NodeHashTable;
36 
37 namespace AStarDelayUtil
38 {
39  class SearchNode {
40  public:
41  SearchNode(double _fCost=0, double _gCost=0, graphState curr=UINT32_MAX, graphState prev=UINT32_MAX)
42  :fCost(_fCost), gCost(_gCost), currNode(curr), prevNode(prev),isGoal(false) {}
43 
45  :fCost(0), gCost(0), currNode(curr), prevNode(curr),isGoal(false) {}
46 
47  void copy(double f, double g, graphState curr, graphState prev)
48  {
49  fCost = f;
50  gCost = g;
51  currNode = curr;
52  prevNode = prev;
53  }
54 
55  double fCost;
56  double gCost;
59  bool isGoal;
60 
61  //bool rxp;
62  };
63 
64  struct SearchNodeEqual {
65  bool operator()(const SearchNode &i1, const SearchNode &i2) const
66  { return (i1.currNode == i2.currNode); }
67  };
68 
69  struct SearchNodeCompare { // true means i2 is preferable over i1
70  // prefering larger g, i.e. smaller h is also in favor of goal nodes
71  bool operator()(const SearchNode &i1, const SearchNode &i2) const
72  {
73  if (fequal(i1.fCost, i2.fCost))
74  {
75  if (i2.isGoal) // always prefer a goal node in tie
76  return true;
77  if (i1.isGoal)
78  return false;
79  return (fless(i1.gCost, i2.gCost));
80  }
81  return (fgreater(i1.fCost, i2.fCost));
82  }
83  };
84 
85  struct GGreater {
86  bool operator()(const SearchNode &i1, const SearchNode &i2) const
87  {
88  if (fequal(i1.gCost,i2.gCost)) {
89  //if (i2.isGoal) // always prefer a goal node in tie
90  // return true;
91  //if (i1.isGoal)
92  // return false;
93 
94  return fgreater(i1.fCost,i2.fCost);
95  }
96 
97  return fgreater(i1.gCost,i2.gCost);
98  }
99  };
100 
101  struct FExtract {
102  double operator()(const SearchNode &i) const {
103  return i.fCost;
104  }
105  };
106 
107  struct SearchNodeHash {
108  size_t operator()(const SearchNode &x) const
109  { return (size_t)(x.currNode); }
110  };
111 
114 
115  typedef std::unordered_map<graphState, AStarDelayUtil::SearchNode > NodeLookupTable;
116 
119 }
120 
121 class AStarDelay : public GraphAlgorithm {
122 public:
123  AStarDelay() { verID=6; bpmxLevel = 0; fD=2;}
124  AStarDelay(int lev) { verID=6; bpmxLevel = lev; fD=2;}
125  virtual ~AStarDelay() {}
126  void GetPath(GraphEnvironment *env, Graph* _g, graphState from, graphState to, std::vector<graphState> &thePath);
127 
128  uint64_t GetNodesExpanded() { return (uint64_t)nodesExpanded; }
129  uint64_t GetNodesTouched() { return nodesTouched; }
130 
131  uint64_t GetNodesFirstExpanded() {return closedSize;}
132  uint64_t GetNodesMetaExpanded() {return metaexpanded;}
133  uint64_t GetNodesReopened() { return nodesReopened; }
134 
135  bool InitializeSearch(GraphEnvironment *env, Graph* g, graphState from, graphState to, std::vector<graphState> &thePath);
136  bool DoSingleSearchStep(std::vector<graphState> &thePath);
137 
138  void ExtractPathToStart(graphState goalNode, std::vector<graphState> &thePath);
139  //void OpenGLDraw() const;
140  void OpenGLDraw() const;
141  void DrawText(double x, double y, double z, float r, float g, float b, char* str);
142  void DrawEdge(unsigned int from, unsigned int to, double weight);
143 
144  double GetSolutionCost() {return solutionCost;}
145  const char* GetName() {return "AStarDelay";}
146  int GetSolutionEdges() {return pathSize;}
147 
149  void Broadcast(int level, int levelcount);
150 
151  double solutionCost;
152  int verID;
153  int fD;
154 
155 
156  //unsigned long tickNewExp;
157  //unsigned long tickReExp;
158  //unsigned long tickBPMX;
159 
160  //unsigned long nNewExp;
161  //unsigned long nReExp;
162  //unsigned long nBPMX;
163 
165 
168 
169  uint64_t closedSize;
170 
171 private:
173  std::vector<graphState> &thePath);
175  AStarDelayUtil::SearchNode &topNode);
177  AStarDelayUtil::SearchNode &topNode);
178 // double UpdateLowGNode(graphState neighbor,
179 // AStarDelayUtil::SearchNode &topNode);
181  AStarDelayUtil::SearchNode &topNode);
183  AStarDelayUtil::SearchNode &topNode);
185  AStarDelayUtil::SearchNode &topNode);
187  AStarDelayUtil::SearchNode &topNode);
188 
189  double F;
190 
191  double fDelay(double N)
192  {
193  switch (fD)
194  {
195  case D_ONE: return 1;
196  case D_TWO: return 2;
197  case D_THREE: return 3;
198  case D_FOUR: return 4;
199  case D_LOG2: return log2(N);
200  case D_SQRT: return sqrt(N);
201  default: return 2;
202  }
203  }
204 
207  std::vector<graphState> neighbors;
210 
212 
213  Graph *g; // for OpenGL drawing only
214 
215 
216  int pathSize;
217 
219 
220  std::deque<graphState> fifo;
221  std::vector<graphState> myneighbors;
222 
223 };
224 
225 #endif
D_THREE
#define D_THREE
Definition: AStarDelay.h:29
AStarDelayUtil::SearchNode::currNode
graphState currNode
Definition: AStarDelay.h:57
AStarDelay::GetNodesReopened
uint64_t GetNodesReopened()
Definition: AStarDelay.h:133
D_SQRT
#define D_SQRT
Definition: AStarDelay.h:32
AStarDelayUtil::SearchNodeEqual::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: AStarDelay.h:65
graphMove
Definition: GraphEnvironment.h:34
AStarDelay::UpdateDelayedNode
double UpdateDelayedNode(graphState neighbor, AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:615
graphState
unsigned long graphState
Definition: GraphEnvironment.h:32
AStarDelayUtil
Definition: AStarDelay.h:37
AStarDelay::AStarDelay
AStarDelay(int lev)
Definition: AStarDelay.h:124
AStarDelayUtil::SearchNode::gCost
double gCost
Definition: AStarDelay.h:56
UINT32_MAX
#define UINT32_MAX
Definition: AStarDelay.h:23
AStarDelay::goal
graphState goal
Definition: AStarDelay.h:208
AStarDelay::GetSolutionCost
double GetSolutionCost()
Definition: AStarDelay.h:144
AStarDelay::AddNewNode
double AddNewNode(graphState neighbor, AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:523
AStarDelay::nodesTouched
uint64_t nodesTouched
Definition: AStarDelay.h:206
AStarDelay::openQueue
AStarDelayUtil::PQueue openQueue
Definition: AStarDelay.h:166
AStarDelay::verID
int verID
Definition: AStarDelay.h:152
AStarDelay::bpmxLevel
int bpmxLevel
Definition: AStarDelay.h:218
FPUtil.h
AStarDelayUtil::FExtract
Definition: AStarDelay.h:101
AStarDelay::myneighbors
std::vector< graphState > myneighbors
Definition: AStarDelay.h:221
AStarDelayUtil::SearchNode::SearchNode
SearchNode(double _fCost=0, double _gCost=0, graphState curr=UINT32_MAX, graphState prev=UINT32_MAX)
Definition: AStarDelay.h:41
Graph
A generic Graph class.
Definition: Graph.h:66
AStarDelayUtil::SearchNode::copy
void copy(double f, double g, graphState curr, graphState prev)
Definition: AStarDelay.h:47
AStarDelay::GetNodesTouched
uint64_t GetNodesTouched()
Definition: AStarDelay.h:129
AStarDelayUtil::SearchNode::SearchNode
SearchNode(graphState curr)
Definition: AStarDelay.h:44
D_LOG2
#define D_LOG2
Definition: AStarDelay.h:31
AStarDelay::delayQueue
AStarDelayUtil::GQueue delayQueue
Definition: AStarDelay.h:211
AStarDelay::OpenGLDraw
void OpenGLDraw() const
Definition: AStarDelay.cpp:677
AStarDelay::DrawEdge
void DrawEdge(unsigned int from, unsigned int to, double weight)
Definition: AStarDelay.cpp:776
AStarDelayUtil::SearchNode::prevNode
graphState prevNode
Definition: AStarDelay.h:58
AStarDelay::neighbors
std::vector< graphState > neighbors
Definition: AStarDelay.h:207
D_FOUR
#define D_FOUR
Definition: AStarDelay.h:30
AStarDelay::closedSize
uint64_t closedSize
Definition: AStarDelay.h:169
AStarDelay::GetNodesFirstExpanded
uint64_t GetNodesFirstExpanded()
Definition: AStarDelay.h:131
AStarDelay::GetSolutionEdges
int GetSolutionEdges()
Definition: AStarDelay.h:146
AStarDelayUtil::SearchNodeHash
Definition: AStarDelay.h:107
D_ONE
#define D_ONE
Definition: AStarDelay.h:27
AStarDelay::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: AStarDelay.h:128
AStarDelay::UpdateOpenNode
double UpdateOpenNode(graphState neighbor, AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:544
AStarDelay::closedList
AStarDelayUtil::NodeLookupTable closedList
Definition: AStarDelay.h:167
AStarDelay::fQueue
AStarDelayUtil::GQueue fQueue
Definition: AStarDelay.h:211
AStarDelay::GetName
const char * GetName()
Definition: AStarDelay.h:145
AStarDelay::HandleNeighbor
double HandleNeighbor(graphState neighbor, AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:452
AStarDelayUtil::GGreater::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: AStarDelay.h:86
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
AStarDelayUtil::NodeLookupTable
std::unordered_map< graphState, AStarDelayUtil::SearchNode > NodeLookupTable
Definition: AStarDelay.h:115
AStarDelay::~AStarDelay
virtual ~AStarDelay()
Definition: AStarDelay.h:125
AStarDelay::GetNodesMetaExpanded
uint64_t GetNodesMetaExpanded()
Definition: AStarDelay.h:132
AStarDelayUtil::SearchNode
Definition: AStarDelay.h:39
AStarDelay::DrawText
void DrawText(double x, double y, double z, float r, float g, float b, char *str)
Definition: AStarDelay.cpp:755
AStarDelay::F
double F
Definition: AStarDelay.h:189
AStarDelay::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< graphState > &thePath)
Definition: AStarDelay.cpp:83
AStarDelay::metaexpanded
long metaexpanded
Definition: AStarDelay.h:164
AStarDelay::GetPath
void GetPath(GraphEnvironment *env, Graph *_g, graphState from, graphState to, std::vector< graphState > &thePath)
Definition: AStarDelay.cpp:24
AStarDelayUtil::FExtract::operator()
double operator()(const SearchNode &i) const
Definition: AStarDelay.h:102
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
OpenListB
A simple & efficient Heap class.
Definition: OpenListB.h:28
AStarDelay::start
graphState start
Definition: AStarDelay.h:208
AStarDelayUtil::SearchNodeCompare::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: AStarDelay.h:71
AStarDelay::Broadcast
void Broadcast(int level, int levelcount)
Definition: AStarDelay.cpp:321
AStarDelay::fDelay
double fDelay(double N)
Definition: AStarDelay.h:191
GraphAlgorithm.h
AStarDelay::ReversePropX1
void ReversePropX1(AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:274
AStarDelayUtil::GQueue
OpenListB< AStarDelayUtil::SearchNode, AStarDelayUtil::SearchNodeHash, AStarDelayUtil::SearchNodeEqual, AStarDelayUtil::GGreater, AStarDelayUtil::GGreater, AStarDelayUtil::FExtract > GQueue
Definition: AStarDelay.h:118
AStarDelay::HandleNeighborX
double HandleNeighborX(graphState neighbor, AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:476
AStarDelay::DoSingleStep
bool DoSingleStep(AStarDelayUtil::SearchNode &topNode, std::vector< graphState > &thePath)
Definition: AStarDelay.cpp:163
AStarDelay::InitializeSearch
bool InitializeSearch(GraphEnvironment *env, Graph *g, graphState from, graphState to, std::vector< graphState > &thePath)
Definition: AStarDelay.cpp:46
AStarDelay::env
GraphEnvironment * env
Definition: AStarDelay.h:209
AStarDelayUtil::PQueue
OpenListB< AStarDelayUtil::SearchNode, AStarDelayUtil::SearchNodeHash, AStarDelayUtil::SearchNodeEqual, AStarDelayUtil::SearchNodeCompare, AStarDelayUtil::GGreater, AStarDelayUtil::FExtract > PQueue
Definition: AStarDelay.h:113
AStarDelay::solutionCost
double solutionCost
Definition: AStarDelay.h:151
AStarDelayUtil::SearchNode::fCost
double fCost
Definition: AStarDelay.h:55
AStarDelay::nodesReopened
uint64_t nodesReopened
Definition: AStarDelay.h:206
AStarDelay::fD
int fD
Definition: AStarDelay.h:153
AStarDelayUtil::SearchNodeEqual
Definition: AStarDelay.h:64
AStarDelayUtil::SearchNode::isGoal
bool isGoal
Definition: AStarDelay.h:59
AStarDelay
Definition: AStarDelay.h:121
AStarDelay::g
Graph * g
Definition: AStarDelay.h:213
AStarDelayUtil::SearchNodeHash::operator()
size_t operator()(const SearchNode &x) const
Definition: AStarDelay.h:108
AStarDelayUtil::GGreater
Definition: AStarDelay.h:85
AStarDelay::ExtractPathToStart
void ExtractPathToStart(graphState goalNode, std::vector< graphState > &thePath)
Definition: AStarDelay.cpp:646
GraphAlgorithm
Definition: GraphAlgorithm.h:17
AStarDelay::nodesExpanded
double nodesExpanded
Definition: AStarDelay.h:205
AStarDelay::AStarDelay
AStarDelay()
Definition: AStarDelay.h:123
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
OpenListB.h
AStarDelay::UpdateClosedNode
double UpdateClosedNode(graphState neighbor, AStarDelayUtil::SearchNode &topNode)
Definition: AStarDelay.cpp:574
AStarDelay::fifo
std::deque< graphState > fifo
Definition: AStarDelay.h:220
D_TWO
#define D_TWO
Definition: AStarDelay.h:28
GraphEnvironment
Definition: GraphEnvironment.h:291
AStarDelayUtil::SearchNodeCompare
Definition: AStarDelay.h:69
AStarDelay::pathSize
int pathSize
Definition: AStarDelay.h:216
SearchEnvironment.h
GraphEnvironment.h