HOG2
IndexOpenClosed.h
Go to the documentation of this file.
1 //
2 // IndexOpenClosed.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 4/18/16.
6 // Copyright © 2016 University of Denver. All rights reserved.
7 //
8 
9 #ifndef IndexOpenClosed_h
10 #define IndexOpenClosed_h
11 
19 #include "AStarOpenClosed.h"
20 
21 template<typename state>
23 public:
25  IndexOpenClosedData(const state &theData, double gCost, double hCost, uint64_t parent, uint64_t openLoc, dataLocation location)
26  :data(theData), g(gCost), h(hCost), parentID(parent), openLocation(openLoc), where(location) { reopened = false; }
27  state data;
28  double g;
29  double h;
30  uint64_t parentID;
31  uint64_t openLocation;
32  uint64_t round;
33  bool reopened;
35 };
36 
37 template<typename state>
39 public:
41  IndexOpenClosedDataWithF(const state &theData, double fCost, double gCost, double hCost, uint64_t parent, uint64_t openLoc, dataLocation location)
42  :data(theData), f(fCost), g(gCost), h(hCost), parentID(parent), openLocation(openLoc), where(location) { reopened = false; }
43  state data;
44  double f;
45  double g;
46  double h;
47  uint64_t parentID;
48  uint64_t openLocation;
49  uint64_t round;
50  bool reopened;
52 };
53 
54 
55 template <class state>
56 struct IndexCompare {
58  {
59  if (fequal(i1.g+i1.h, i2.g+i2.h))
60  {
61  return (fless(i1.g, i2.g));
62  }
63  return (fgreater(i1.g+i1.h, i2.g+i2.h));
64  }
65 };
66 
67 template <class state>
70  {
71  if (fequal(i1.f, i2.f))
72  {
73  return (fless(i1.g, i2.g));
74  }
75  return (fgreater(i1.f, i2.f));
76  }
77 };
78 
79 template<typename state, typename CmpKey = IndexCompareWithF<state>, class dataStructure = IndexOpenClosedDataWithF<state> >
81 public:
84  void Reset(int maxID);
85  uint64_t AddOpenNode(const state &val, uint64_t hash, double g, double h, uint64_t parent=kTAStarNoNode);
86  uint64_t AddOpenNode(const state &val, uint64_t hash, double f, double g, double h, uint64_t parent=kTAStarNoNode);
87  uint64_t AddClosedNode(state &val, uint64_t hash, double g, double h, uint64_t parent=kTAStarNoNode);
88  uint64_t AddClosedNode(state &val, uint64_t hash, double f, double g, double h, uint64_t parent=kTAStarNoNode);
89  void KeyChanged(uint64_t objKey);
90  //void IncreaseKey(uint64_t objKey);
91  dataLocation Lookup(uint64_t hashKey, uint64_t &objKey) const;
92  inline dataStructure &Lookup(uint64_t objKey) { return elements[objKey]; }
93  inline const dataStructure &Lookat(uint64_t objKey) const { return elements[objKey]; }
94  uint64_t GetRound() const { return currentRound; }
95  uint64_t Peek() const;
96  uint64_t Close();
97  void Reopen(uint64_t objKey);
98 
99  uint64_t GetOpenItem(unsigned int which) const { return theHeap[which]; }
100  size_t OpenSize() const { return theHeap.size(); }
101  size_t ClosedSize() const { return size()-OpenSize(); }
102  size_t size() const { return elements.size(); }
103  // void verifyData();
104 private:
105  bool HeapifyUp(unsigned int index);
106  void HeapifyDown(unsigned int index);
107 
108  uint64_t currentRound;
109  std::vector<uint64_t> theHeap;
110  std::vector<dataStructure> elements;
111 };
112 
113 
114 template<typename state, typename CmpKey, class dataStructure>
116 {
117  currentRound = 0;
118 // elements.resize(maxID);
119 // for (auto &x : elements)
120 // x.round = currentRound;
121 }
122 
123 template<typename state, typename CmpKey, class dataStructure>
125 {
126 }
127 
131 template<typename state, typename CmpKey, class dataStructure>
133 {
134  theHeap.resize(0);
135  if (elements.size() != maxID)
136  {
137  elements.resize(maxID);
138  for (auto &x : elements)
139  x.round = currentRound;
140  }
141  currentRound++;
142 }
143 
147 template<typename state, typename CmpKey, class dataStructure>
148 uint64_t IndexOpenClosed<state, CmpKey, dataStructure>::AddOpenNode(const state &val, uint64_t hash, double g, double h, uint64_t parent)
149 {
150  if (elements[hash].round == currentRound)
151  {
152  // shouldn't be in open/closed already
153  assert(false);
154  }
155  elements[hash] = dataStructure(val, g, h, parent, theHeap.size(), kOpenList);
156  elements[hash].round = currentRound;
157  if (parent == kTAStarNoNode)
158  elements[hash].parentID = hash;
159  theHeap.push_back(hash); // adding element id to back of heap
160  HeapifyUp(theHeap.size()-1); // heapify from back of the heap
161  return hash;
162 }
163 
164 template<typename state, typename CmpKey, class dataStructure>
165 uint64_t IndexOpenClosed<state, CmpKey, dataStructure>::AddOpenNode(const state &val, uint64_t hash, double f, double g, double h, uint64_t parent)
166 {
167  if (elements[hash].round == currentRound)
168  {
169  // shouldn't be in open/closed already
170  assert(false);
171  }
172  elements[hash] = dataStructure(val, f, g, h, parent, theHeap.size(), kOpenList);
173  elements[hash].round = currentRound;
174  if (parent == kTAStarNoNode)
175  elements[hash].parentID = hash;
176  theHeap.push_back(hash); // adding element id to back of heap
177  HeapifyUp(theHeap.size()-1); // heapify from back of the heap
178  return hash;
179 }
180 
184 template<typename state, typename CmpKey, class dataStructure>
185 uint64_t IndexOpenClosed<state, CmpKey, dataStructure>::AddClosedNode(state &val, uint64_t hash, double g, double h, uint64_t parent)
186 {
187  // shouldn't be in open/closed already
188  assert(elements[hash].round != currentRound);
189  elements[hash] = dataStructure(val, g, h, parent, 0, kClosedList);
190  elements[hash].round = currentRound;
191  if (parent == kTAStarNoNode)
192  elements[hash].parentID = hash;
193  return hash;
194 }
195 
199 template<typename state, typename CmpKey, class dataStructure>
200 uint64_t IndexOpenClosed<state, CmpKey, dataStructure>::AddClosedNode(state &val, uint64_t hash, double f, double g, double h, uint64_t parent)
201 {
202  // shouldn't be in open/closed already
203  assert(elements[hash].round != currentRound);
204  elements[hash] = dataStructure(val, f, g, h, parent, 0, kClosedList);
205  elements[hash].round = currentRound;
206  if (parent == kTAStarNoNode)
207  elements[hash].parentID = hash;
208  return hash;
209 }
210 
214 template<typename state, typename CmpKey, class dataStructure>
216 {
217  if (!HeapifyUp(elements[val].openLocation))
218  HeapifyDown(elements[val].openLocation);
219 }
220 
224 template<typename state, typename CmpKey, class dataStructure>
226 {
227  objKey = hashKey;
228  if (elements[hashKey].round == currentRound)
229  return elements[hashKey].where;
230  return kNotFound;
231 }
232 
233 
237 template<typename state, typename CmpKey, class dataStructure>
239 {
240  assert(OpenSize() != 0);
241 
242  return theHeap[0];
243 }
244 
248 template<typename state, typename CmpKey, class dataStructure>
250 {
251  assert(OpenSize() != 0);
252 
253  uint64_t ans = theHeap[0];
254  elements[ans].where = kClosedList;
255  theHeap[0] = theHeap[theHeap.size()-1];
256  elements[theHeap[0]].openLocation = 0;
257  theHeap.pop_back();
258  HeapifyDown(0);
259 
260  return ans;
261 }
262 
266 template<typename state, typename CmpKey, class dataStructure>
268 {
269  assert(elements[objKey].where == kClosedList);
270  elements[objKey].reopened = true;
271  elements[objKey].where = kOpenList;
272  elements[objKey].openLocation = theHeap.size();
273  theHeap.push_back(objKey);
274  HeapifyUp(theHeap.size()-1);
275 }
276 
277 
281 template<typename state, typename CmpKey, class dataStructure>
283 {
284  if (index == 0) return false;
285  int parent = (index-1)/2;
286  CmpKey compare;
287 
288  if (compare(elements[theHeap[parent]], elements[theHeap[index]]))
289  {
290  unsigned int tmp = theHeap[parent];
291  theHeap[parent] = theHeap[index];
292  theHeap[index] = tmp;
293  elements[theHeap[parent]].openLocation = parent;
294  elements[theHeap[index]].openLocation = index;
295  HeapifyUp(parent);
296  return true;
297  }
298  return false;
299 }
300 
301 template<typename state, typename CmpKey, class dataStructure>
303 {
304  CmpKey compare;
305  unsigned int child1 = index*2+1;
306  unsigned int child2 = index*2+2;
307  int which;
308  unsigned int count = theHeap.size();
309  // find smallest child
310  if (child1 >= count)
311  return;
312  else if (child2 >= count)
313  which = child1;
314  else if (!(compare(elements[theHeap[child1]], elements[theHeap[child2]])))
315  which = child1;
316  else
317  which = child2;
318 
319  //if (fless(theHeap[which]->GetKey(), theHeap[index]->GetKey()))
320  if (!(compare(elements[theHeap[which]], elements[theHeap[index]])))
321  {
322  unsigned int tmp = theHeap[which];
323  theHeap[which] = theHeap[index];
324  // table[theHeap[which]] = which;
325  theHeap[index] = tmp;
326  elements[theHeap[which]].openLocation = which;
327  elements[theHeap[index]].openLocation = index;
328  // table[theHeap[index]] = index;
329  // theHeap[which]->key = which;
330  // theHeap[index]->key = index;
331  HeapifyDown(which);
332  }
333 }
334 
335 #endif /* IndexOpenClosed_h */
IndexOpenClosed::OpenSize
size_t OpenSize() const
Definition: IndexOpenClosed.h:100
IndexOpenClosed::Close
uint64_t Close()
Move the best item to the closed list and return key.
Definition: IndexOpenClosed.h:249
IndexOpenClosed::Peek
uint64_t Peek() const
Peek at the next item to be expanded.
Definition: IndexOpenClosed.h:238
IndexCompare::operator()
bool operator()(const IndexOpenClosedData< state > &i1, const IndexOpenClosedData< state > &i2) const
Definition: IndexOpenClosed.h:57
IndexOpenClosedDataWithF
Definition: IndexOpenClosed.h:38
IndexOpenClosedDataWithF::IndexOpenClosedDataWithF
IndexOpenClosedDataWithF()
Definition: IndexOpenClosed.h:40
IndexOpenClosed::theHeap
std::vector< uint64_t > theHeap
Definition: IndexOpenClosed.h:109
IndexOpenClosedData
This open/closed list is designed for state spaces where the hash is an index that is small enough th...
Definition: IndexOpenClosed.h:22
IndexOpenClosedDataWithF::openLocation
uint64_t openLocation
Definition: IndexOpenClosed.h:48
AStarOpenClosed.h
IndexCompareWithF
Definition: IndexOpenClosed.h:68
IndexCompareWithF::operator()
bool operator()(const IndexOpenClosedDataWithF< state > &i1, const IndexOpenClosedDataWithF< state > &i2) const
Definition: IndexOpenClosed.h:69
IndexOpenClosed::AddClosedNode
uint64_t AddClosedNode(state &val, uint64_t hash, double g, double h, uint64_t parent=kTAStarNoNode)
Add object into closed list.
Definition: IndexOpenClosed.h:185
IndexOpenClosed::KeyChanged
void KeyChanged(uint64_t objKey)
Indicate that the key for a particular object has changed.
Definition: IndexOpenClosed.h:215
IndexOpenClosed::currentRound
uint64_t currentRound
Definition: IndexOpenClosed.h:108
IndexOpenClosed::Lookup
dataStructure & Lookup(uint64_t objKey)
Definition: IndexOpenClosed.h:92
IndexOpenClosedDataWithF::round
uint64_t round
Definition: IndexOpenClosed.h:49
IndexOpenClosed::Reopen
void Reopen(uint64_t objKey)
Move item off the closed list and back onto the open list.
Definition: IndexOpenClosed.h:267
IndexOpenClosed::GetRound
uint64_t GetRound() const
Definition: IndexOpenClosed.h:94
IndexOpenClosedDataWithF::g
double g
Definition: IndexOpenClosed.h:45
IndexOpenClosed::size
size_t size() const
Definition: IndexOpenClosed.h:102
kTAStarNoNode
const uint64_t kTAStarNoNode
Definition: AStarOpenClosed.h:33
IndexOpenClosedDataWithF::h
double h
Definition: IndexOpenClosed.h:46
IndexOpenClosedDataWithF::f
double f
Definition: IndexOpenClosed.h:44
IndexCompare
Definition: IndexOpenClosed.h:56
IndexOpenClosedData::round
uint64_t round
Definition: IndexOpenClosed.h:32
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
IndexOpenClosed::HeapifyDown
void HeapifyDown(unsigned int index)
Definition: IndexOpenClosed.h:302
IndexOpenClosedDataWithF::parentID
uint64_t parentID
Definition: IndexOpenClosed.h:47
IndexOpenClosed::AddOpenNode
uint64_t AddOpenNode(const state &val, uint64_t hash, double g, double h, uint64_t parent=kTAStarNoNode)
Add object into open list.
Definition: IndexOpenClosed.h:148
dataLocation
dataLocation
Definition: AStarOpenClosed.h:27
IndexOpenClosed::elements
std::vector< dataStructure > elements
Definition: IndexOpenClosed.h:110
IndexOpenClosedData::reopened
bool reopened
Definition: IndexOpenClosed.h:33
IndexOpenClosedDataWithF::reopened
bool reopened
Definition: IndexOpenClosed.h:50
IndexOpenClosedData::g
double g
Definition: IndexOpenClosed.h:28
IndexOpenClosedData::where
dataLocation where
Definition: IndexOpenClosed.h:34
IndexOpenClosed::GetOpenItem
uint64_t GetOpenItem(unsigned int which) const
Definition: IndexOpenClosed.h:99
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
IndexOpenClosed::Lookat
const dataStructure & Lookat(uint64_t objKey) const
Definition: IndexOpenClosed.h:93
kOpenList
@ kOpenList
Definition: AStarOpenClosed.h:28
IndexOpenClosed::HeapifyUp
bool HeapifyUp(unsigned int index)
Moves a node up the heap.
Definition: IndexOpenClosed.h:282
IndexOpenClosed::Reset
void Reset(int maxID)
Remove all objects from queue.
Definition: IndexOpenClosed.h:132
IndexOpenClosed
Definition: IndexOpenClosed.h:80
IndexOpenClosedDataWithF::where
dataLocation where
Definition: IndexOpenClosed.h:51
IndexOpenClosedData::IndexOpenClosedData
IndexOpenClosedData(const state &theData, double gCost, double hCost, uint64_t parent, uint64_t openLoc, dataLocation location)
Definition: IndexOpenClosed.h:25
IndexOpenClosed::~IndexOpenClosed
~IndexOpenClosed()
Definition: IndexOpenClosed.h:124
IndexOpenClosed::Lookup
dataLocation Lookup(uint64_t hashKey, uint64_t &objKey) const
Returns location of object as well as object key.
Definition: IndexOpenClosed.h:225
IndexOpenClosedDataWithF::IndexOpenClosedDataWithF
IndexOpenClosedDataWithF(const state &theData, double fCost, double gCost, double hCost, uint64_t parent, uint64_t openLoc, dataLocation location)
Definition: IndexOpenClosed.h:41
kNotFound
@ kNotFound
Definition: AStarOpenClosed.h:30
IndexOpenClosedData::IndexOpenClosedData
IndexOpenClosedData()
Definition: IndexOpenClosed.h:24
IndexOpenClosed::IndexOpenClosed
IndexOpenClosed()
Definition: IndexOpenClosed.h:115
IndexOpenClosed::ClosedSize
size_t ClosedSize() const
Definition: IndexOpenClosed.h:101
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
IndexOpenClosedData::h
double h
Definition: IndexOpenClosed.h:29
IndexOpenClosedDataWithF::data
state data
Definition: IndexOpenClosed.h:43
kClosedList
@ kClosedList
Definition: AStarOpenClosed.h:29
IndexOpenClosedData::openLocation
uint64_t openLocation
Definition: IndexOpenClosed.h:31
IndexOpenClosedData::data
state data
Definition: IndexOpenClosed.h:27
IndexOpenClosedData::parentID
uint64_t parentID
Definition: IndexOpenClosed.h:30