HOG2
BucketOpenClosed.h
Go to the documentation of this file.
1 //
2 // BucketOpenClosed.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 1/16/12.
6 // Copyright (c) 2012 University of Denver. All rights reserved.
7 //
8 
9 #ifndef hog2_glut_BucketOpenClosed_h
10 #define hog2_glut_BucketOpenClosed_h
11 
12 #include "AStarOpenClosed.h"
13 #include <list>
14 
15 template<typename state, typename CmpKey, class dataStructure = AStarOpenClosedData<state> >
17 public:
20  void Reset();
21  uint64_t AddOpenNode(const state &val, uint64_t hash, double f, double g, double h, uint64_t parent=kTAStarNoNode);
22  uint64_t AddClosedNode(state &val, uint64_t hash, double f, double g, double h, uint64_t parent=kTAStarNoNode);
23  void KeyChanged(uint64_t objKey);
24  //void IncreaseKey(uint64_t objKey);
25  dataLocation Lookup(uint64_t hashKey, uint64_t &objKey) const;
26  inline dataStructure &Lookup(uint64_t objKey) { return elements[objKey]; }
27  inline const dataStructure &Lookat(uint64_t objKey) const { return elements[objKey]; }
28  uint64_t Peek() const;
29  uint64_t Close();
30  void Reopen(uint64_t objKey);
31 
32  uint64_t GetOpenItem(unsigned int which);
33  size_t OpenSize() const { return openCount; }
34  size_t ClosedSize() const { return size()-OpenSize(); }
35  size_t size() const { return elements.size(); }
36  // void verifyData();
37  void Print();
38 private:
39  void FindNewMin();
40  uint64_t Add(uint64_t key, uint64_t fCost);
41  void Remove(uint64_t key, uint64_t fCost);
42  int openCount;
44  struct qData {
45  qData() { fCost = -1; }
46  std::vector<uint64_t> entries;
47  int fCost;
48  };
49  std::vector<qData> pQueue;
50  // storing the element id; looking up with...hash?
51  typedef std::unordered_map<uint64_t, uint64_t, AHash64> IndexTable;
53  std::vector<dataStructure > elements;
54 };
55 
56 template<typename state, typename CmpKey, class dataStructure>
58 {
59  openCount = 0;
60  minBucket = -1;
61 }
62 
63 template<typename state, typename CmpKey, class dataStructure>
65 {
66 }
67 
71 template<typename state, typename CmpKey, class dataStructure>
73 {
74  openCount = 0;
75  minBucket = -1;
76  table.clear();
77  elements.clear();
78  pQueue.resize(0);
79  //pQueue.resize(3);
80 }
81 
82 //inline uint64_t compactGH(uint64_t g, uint64_t h)
83 //{
84 // return (g<<32)|h;
85 //}
86 //
87 //inline uint64_t extractG(uint64_t value)
88 //{
89 // return value>>32;
90 //}
91 //
92 //inline uint64_t extractH(uint64_t value)
93 //{
94 // return (value)&0xFFFFFFFF;
95 //}
96 
100 template<typename state, typename CmpKey, class dataStructure>
101 uint64_t BucketOpenClosed<state, CmpKey, dataStructure>::AddOpenNode(const state &val, uint64_t hash, double f, double g, double h, uint64_t parent)
102 {
103  // should do lookup here...
104  if (table.find(hash) != table.end())
105  {
106  //return -1; // TODO: find correct id and return
107  assert(false);
108  }
109  openCount++;
110 // uint64_t newg = g;
111 // uint64_t newh = h;
112  uint64_t loc = Add(elements.size(), f);
113  elements.push_back(dataStructure(val, f, g, h, parent, loc, kOpenList));
114  if (parent == kTAStarNoNode)
115  elements.back().parentID = elements.size()-1;
116  table[hash] = elements.size()-1; // hashing to element list location
117  FindNewMin();
118  //printf("Added node to buckets %" PRId64 "/%" PRId64 "\n", newg+newh, newh);
119  return elements.size()-1;
120 }
121 
125 template<typename state, typename CmpKey, class dataStructure>
126 uint64_t BucketOpenClosed<state, CmpKey, dataStructure>::AddClosedNode(state &val, uint64_t hash, double f, double g, double h, uint64_t parent)
127 {
128  // should do lookup here...
129  assert(table.find(hash) == table.end());
130  elements.push_back(dataStructure(val, f, g, h, parent, 0, kClosedList));
131  if (parent == kTAStarNoNode)
132  elements.back().parentID = elements.size()-1;
133  table[hash] = elements.size(); // hashing to element list location
134  return elements.size()-1;
135 }
136 
140 template<typename state, typename CmpKey, class dataStructure>
142 {
143 // uint64_t g, h;
144 // g = extractG(elements[val].openLocation);
145 // h = extractH(elements[val].openLocation);
146  //printf("-----\nChanging key of [%" PRId64 "][%" PRId64 "] to [%1.0f][%1.0f]\n", g+h, h, elements[val].g+elements[val].h, elements[val].h);
147  //Print();
148  Remove(val, elements[val].openLocation);
149 // int len = pQueue[g+h][0].size();
150 // pQueue[g+h][0].remove(val);
151 // assert(pQueue[g+h][0].size() == len-1);
152 // int len = pQueue[g+h][h].size();
153 // pQueue[g+h][h].remove(val);
154 // assert(pQueue[g+h][h].size() == len-1);
155 
156 // g = elements[val].g;
157 // h = elements[val].h;
158 // elements[val].openLocation = compactGH(g, h);
159 // if (pQueue.size() < (g+h+1))
160 // pQueue.resize((int)(g+h+1));
161 // if (pQueue[g+h].size() < /*h+*/1)
162 // pQueue[g+h].resize((int)/*h+*/1);
163 // pQueue[g+h][0].push_back(val);
164  elements[val].openLocation = Add(val, elements[val].g+elements[val].h);
165  //pQueue[g+h][h].push_back(val);
166  //printf("Removed and re-added node to buckets %" PRId64 "/%" PRId64 "\n", g+h, h);
167 
168 // FindNewMin();
169  //Print();
170 }
171 
175 template<typename state, typename CmpKey, class dataStructure>
177 {
178  typename IndexTable::const_iterator it;
179  it = table.find(hashKey);
180  if (it != table.end())
181  {
182  objKey = (*it).second;
183  return elements[objKey].where;
184  }
185  return kNotFound;
186 }
187 
188 
192 template<typename state, typename CmpKey, class dataStructure>
194 {
195  assert(OpenSize() != 0);
196 
197  assert(pQueue[minBucket].entries.size() > 0);
198  return pQueue[minBucket].entries.back();
199 // assert(pQueue[minBucket][minSubBucket].size() > 0);
200 // return pQueue[minBucket][minSubBucket].front();
201 // return theHeap[0];
202 }
203 
207 template<typename state, typename CmpKey, class dataStructure>
209 {
210  assert(OpenSize() != 0);
211  assert(pQueue[minBucket].entries.size() > 0);
212  openCount--;
213 
214  uint64_t ans = pQueue[minBucket].entries.back();
215  pQueue[minBucket].entries.pop_back();
216  if (pQueue[minBucket].entries.size() == 0)
217  pQueue[minBucket].fCost = -1;
218  elements[ans].where = kClosedList;
219  //printf("Removed item from %" PRId64 "/%" PRId64 "\n", minBucket, minSubBucket);
220  FindNewMin();
221 
222  return ans;
223 }
224 
228 template<typename state, typename CmpKey, class dataStructure>
230 {
231  openCount++;
232  uint64_t g, h;
233  g = elements[objKey].g;
234  h = elements[objKey].h;
235  assert(elements[objKey].where == kClosedList);
236  elements[objKey].reopened = true;
237  elements[objKey].where = kOpenList;
238  elements[objKey].openLocation = Add(objKey, g+h);
239 }
240 
241 template<typename state, typename CmpKey, class dataStructure>
243 {
244  //assert(OpenSize() > 0);
245  if (OpenSize() == 0)
246  { minBucket = -1; return; }
247 
248  minBucket = -1;
249  for (int x = 0; x < pQueue.size(); x++)
250  {
251  while (pQueue[x].entries.size() > 0 && pQueue[x].entries.back() == -1)
252  {
253  pQueue[x].entries.pop_back();
254  }
255  if (pQueue[x].entries.size() == 0)
256  {
257  pQueue[x].fCost = -1;
258  }
259  else {
260  if (minBucket == -1)
261  {
262  minBucket = x;
263  continue;
264  }
265  else {
266  if (pQueue[x].fCost < pQueue[minBucket].fCost)
267  minBucket = x;
268  }
269  }
270  }
271 }
272 
273 template<typename state, typename CmpKey, class dataStructure>
275 {
276  for (unsigned int x = 0; x < elements.size(); x++)
277  {
278  if (elements[x].where == kOpenList)
279  {
280  if (which == 0)
281  return x;
282  which--;
283  }
284  return -1;
285  }
286  return -1;
287 }
288 
289 template<typename state, typename CmpKey, class dataStructure>
291 {
292  int cnt = 0;
293  printf("**pQueue[%d] items [%d, %d]:\n", OpenSize(), minBucket, minSubBucket);
294  for (unsigned int x = 0; x < pQueue.size(); x++)
295  {
296  for (unsigned int y = 0; y < pQueue[x].size(); y++)
297  {
298  if (pQueue[x][y].size() > 0)
299  printf("**[%d][%d] has %d elements\n", x, y, pQueue[x][y].size());
300  cnt += pQueue[x][y].size();
301  }
302  }
303  if (cnt != OpenSize())
304  {
305  assert(!"ERROR; sizes inconsistent");
306  }
307 }
308 
309 template<typename state, typename CmpKey, class dataStructure>
310 uint64_t BucketOpenClosed<state, CmpKey, dataStructure>::Add(uint64_t key, uint64_t fCost)
311 {
312  for (int x = 0; x < pQueue.size(); x++)
313  {
314  if (pQueue[x].fCost == fCost)
315  {
316  pQueue[x].entries.push_back(key);
317  FindNewMin();
318  return pQueue[x].entries.size()-1;
319  }
320  }
321  for (int x = 0; x < pQueue.size(); x++)
322  {
323  if (pQueue[x].fCost == -1)
324  {
325  pQueue[x].fCost = fCost;
326  pQueue[x].entries.push_back(key);
327  FindNewMin();
328  return pQueue[x].entries.size()-1;
329  }
330  }
331  assert(!"No space found to insert element");
332 }
333 
334 template<typename state, typename CmpKey, class dataStructure>
335 void BucketOpenClosed<state, CmpKey, dataStructure>::Remove(uint64_t key, uint64_t location)
336 {
337  for (int x = 0; x < pQueue.size(); x++)
338  {
339  if (pQueue[x].entries.size() > location && pQueue[x].entries[location] == key)
340  {
341  if (location == pQueue[x].entries.size()-1)
342  {
343  pQueue[x].entries.pop_back();
344  }
345  else {
346  pQueue[x].entries[location] = -1;
347  if (pQueue[x].entries.size() == 1)
348  pQueue[x].fCost = -1;
349  }
350  FindNewMin();
351  return;
352  }
353  }
354  assert(!"Element not found to remove");
355 }
356 
357 #endif
BucketOpenClosed::AddOpenNode
uint64_t AddOpenNode(const state &val, uint64_t hash, double f, double g, double h, uint64_t parent=kTAStarNoNode)
Add object into open list.
Definition: BucketOpenClosed.h:101
BucketOpenClosed::qData::entries
std::vector< uint64_t > entries
Definition: BucketOpenClosed.h:46
BucketOpenClosed::Close
uint64_t Close()
Move the best item to the closed list and return key.
Definition: BucketOpenClosed.h:208
BucketOpenClosed::GetOpenItem
uint64_t GetOpenItem(unsigned int which)
Definition: BucketOpenClosed.h:274
AStarOpenClosed.h
BucketOpenClosed::minBucket
int minBucket
Definition: BucketOpenClosed.h:43
BucketOpenClosed::Peek
uint64_t Peek() const
Peek at the next item to be expanded.
Definition: BucketOpenClosed.h:193
BucketOpenClosed::table
IndexTable table
Definition: BucketOpenClosed.h:52
BucketOpenClosed::Lookup
dataStructure & Lookup(uint64_t objKey)
Definition: BucketOpenClosed.h:26
BucketOpenClosed::AddClosedNode
uint64_t AddClosedNode(state &val, uint64_t hash, double f, double g, double h, uint64_t parent=kTAStarNoNode)
Add object into closed list.
Definition: BucketOpenClosed.h:126
BucketOpenClosed::elements
std::vector< dataStructure > elements
Definition: BucketOpenClosed.h:53
BucketOpenClosed::BucketOpenClosed
BucketOpenClosed()
Definition: BucketOpenClosed.h:57
BucketOpenClosed::minSubBucket
int minSubBucket
Definition: BucketOpenClosed.h:43
BucketOpenClosed::Reset
void Reset()
Remove all objects from queue.
Definition: BucketOpenClosed.h:72
kTAStarNoNode
const uint64_t kTAStarNoNode
Definition: AStarOpenClosed.h:33
BucketOpenClosed
Definition: BucketOpenClosed.h:16
loc
Definition: MapGenerators.cpp:296
BucketOpenClosed::IndexTable
std::unordered_map< uint64_t, uint64_t, AHash64 > IndexTable
Definition: BucketOpenClosed.h:51
BucketOpenClosed::pQueue
std::vector< qData > pQueue
Definition: BucketOpenClosed.h:49
BucketOpenClosed::Print
void Print()
Definition: BucketOpenClosed.h:290
BucketOpenClosed::qData::fCost
int fCost
Definition: BucketOpenClosed.h:47
dataLocation
dataLocation
Definition: AStarOpenClosed.h:27
BucketOpenClosed::KeyChanged
void KeyChanged(uint64_t objKey)
Indicate that the key for a particular object has changed.
Definition: BucketOpenClosed.h:141
kOpenList
@ kOpenList
Definition: AStarOpenClosed.h:28
BucketOpenClosed::qData::qData
qData()
Definition: BucketOpenClosed.h:45
BucketOpenClosed::FindNewMin
void FindNewMin()
Definition: BucketOpenClosed.h:242
BucketOpenClosed::size
size_t size() const
Definition: BucketOpenClosed.h:35
BucketOpenClosed::~BucketOpenClosed
~BucketOpenClosed()
Definition: BucketOpenClosed.h:64
BucketOpenClosed::OpenSize
size_t OpenSize() const
Definition: BucketOpenClosed.h:33
BucketOpenClosed::openCount
int openCount
Definition: BucketOpenClosed.h:42
kNotFound
@ kNotFound
Definition: AStarOpenClosed.h:30
BucketOpenClosed::qData
Definition: BucketOpenClosed.h:44
BucketOpenClosed::Reopen
void Reopen(uint64_t objKey)
Move item off the closed list and back onto the open list.
Definition: BucketOpenClosed.h:229
BucketOpenClosed::Lookup
dataLocation Lookup(uint64_t hashKey, uint64_t &objKey) const
Returns location of object as well as object key.
Definition: BucketOpenClosed.h:176
BucketOpenClosed::Lookat
const dataStructure & Lookat(uint64_t objKey) const
Definition: BucketOpenClosed.h:27
BucketOpenClosed::Remove
void Remove(uint64_t key, uint64_t fCost)
Definition: BucketOpenClosed.h:335
kClosedList
@ kClosedList
Definition: AStarOpenClosed.h:29
BucketOpenClosed::ClosedSize
size_t ClosedSize() const
Definition: BucketOpenClosed.h:34
BucketOpenClosed::Add
uint64_t Add(uint64_t key, uint64_t fCost)
Definition: BucketOpenClosed.h:310