HOG2
TreePermutationPDB.h
Go to the documentation of this file.
1 //
2 // TreePermutationPDB.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 10/19/15.
6 // Copyright © 2015 University of Denver. All rights reserved.
7 //
8 
9 #ifndef TreePermutationPDB_h
10 #define TreePermutationPDB_h
11 
12 #include "PermutationPDB.h"
13 
20 template <class state, class action, class environment, int bits = 8>
21 class TreePermutationPDB : public PermutationPDB<state, action, environment, bits> {
22 public:
23  TreePermutationPDB(environment *e, const state &s, const std::vector<int> &distincts);
24 
25  virtual uint64_t GetPDBHash(const state &s, int threadID = 0) const;
26  virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID = 0) const;
27  virtual uint64_t GetAbstractHash(const state &s, int threadID = 0) const { return GetPDBHash(s); }
28  virtual state GetStateFromAbstractState(state &s) { return s; }
29 
30  std::string GetFileName(const char *prefix);
31 private:
34 
35  uint64_t Factorial(int val) const;
36  uint64_t FactorialUpperK(int n, int k) const;
37 
38  const int maxThreads = 32;
39 
40  // cache for computing ranking/unranking
41  mutable std::vector<std::vector<int> > dualCache;
42  mutable std::vector<std::vector<int> > locsCache;
43  mutable std::vector<std::vector<int> > tempCache;
44 
45  state example;
46 };
47 
48 inline int mylog2(int val)
49 {
50  switch (val)
51  {
52  case 1: return 0;
53  case 2: return 1;
54  case 3: return 2;
55  case 4: return 2;
56  case 5: return 3;
57  case 6: return 3;
58  case 7: return 3;
59  case 8: return 3;
60  case 9: return 4;
61  case 10:return 4;
62  case 11:return 4;
63  case 12:return 4;
64  case 13:return 4;
65  case 14:return 4;
66  case 15:return 4;
67  case 16:return 4;
68  case 17:return 5;
69  case 18:return 5;
70  case 19:return 5;
71  case 20:return 5;
72  case 21:return 5;
73  case 22:return 5;
74  case 23:return 5;
75  case 24:return 5;
76  case 25:return 5;
77  case 26:return 5;
78  case 27:return 5;
79  case 28:return 5;
80  case 29:return 5;
81  case 30:return 5;
82  case 31:return 5;
83  case 32:return 5;
84  default:return 6;
85  }
86 }
87 
88 
89 template <class state, class action, class environment, int bits>
90 TreePermutationPDB<state, action, environment, bits>::TreePermutationPDB(environment *e, const state &s, const std::vector<int> &distincts)
91 :PermutationPDB<state, action, environment, bits>(e, s, distincts),
92 dualCache(maxThreads), locsCache(maxThreads), tempCache(maxThreads)
93 {
94  this->SetGoal(s);
95 }
96 
97 template <class state, class action, class environment, int bits>
98 uint64_t TreePermutationPDB<state, action, environment, bits>::GetPDBHash(const state &s, int threadID) const
99 {
100  std::vector<int> &values = locsCache[threadID];
101  std::vector<int> &dual = dualCache[threadID];
102  // TODO: test definition
103  values.resize(distinct.size()); // vector for distinct item locations
104  dual.resize(s.puzzle.size()); // vector for distinct item locations
105 
106  // find item locations
107  for (unsigned int x = 0; x < s.puzzle.size(); x++)
108  {
109  if (s.puzzle[x] != -1)
110  dual[s.puzzle[x]] = x;
111  }
112  for (int x = 0; x < distinct.size(); x++)
113  {
114  values[x] = dual[distinct[x]];
115  }
116 
117  uint64_t rank = 0;
118  int k = mylog2(s.puzzle.size());
119  //static std::vector<int> temp;
120  std::vector<int> &temp = tempCache[threadID];
121 
122  temp.resize((1<<(1+k))-1);
123  std::fill(temp.begin(), temp.end(), 0);
124  for (int x = 0; x < values.size(); x++)
125  {
126  int counter = values[x];
127  int node = (1<<k)-1+values[x];
128  for (int y = 0; y < k; y++)
129  {
130  int isEven = (1-(node&1));
131  counter -= isEven*(temp[(node-1)>>1] - temp[node]);
132  // if (0 == node%2) // right node
133  // counter -= (temp[(node-1)>>1] - temp[node]);
134  temp[node]++;
135  node = (node-1)>>1;
136  }
137  temp[node]++;
138  rank = rank*(s.puzzle.size()-x)+counter;
139  }
140 
141  return rank;
142 }
143 
144 template <class state, class action, class environment, int bits>
145 void TreePermutationPDB<state, action, environment, bits>::GetStateFromPDBHash(uint64_t hash, state &s, int threadID) const
146 {
147  size_t count = example.puzzle.size();
148  s.puzzle.resize(count);
149  int k = mylog2(count);
150  std::vector<int> &temp = tempCache[threadID];
151  temp.resize((1<<(1+k))-1);
152  for (int x = 0; x < temp.size(); x++)
153  temp[x] = (1<<(k-mylog2(x+2)+1));
154 
155  std::vector<int> &values = locsCache[threadID];
156  values.resize(distinct.size());
157  int numEntriesLeft = s.puzzle.size()-distinct.size()+1;
158  for (int x = (int)values.size()-1; x >= 0; x--)
159  {
160  values[x] = hash%numEntriesLeft;
161  hash /= numEntriesLeft;
162  numEntriesLeft++;
163  }
164  memset(&s.puzzle[0], 0xFF, s.puzzle.size()*sizeof(s.puzzle[0]));
165  for (int x = 0; x < values.size(); x++)
166  {
167  int digit = values[x];
168  int node = 0;
169  for (int y = 0; y < k; y++)
170  {
171  temp[node]--;
172  node = ((node+1)<<1)-1;
173  uint32_t diff = digit-temp[node];
174  diff = (~diff)>>31;
175  digit -= diff*temp[node];
176  node += diff;
177  // if (digit >= temp[node])
178  // {
179  // digit -= temp[node];
180  // node++;
181  // }
182  }
183  temp[node] = 0;
184  values[x] = node - (1<<k) + 1;
185  s.puzzle[values[x]] = distinct[x];
186  }
187  s.FinishUnranking();
188 }
189 
190 template <class state, class action, class environment, int bits>
192 {
193  static uint64_t table[21] =
194  { 1ll, 1ll, 2ll, 6ll, 24ll, 120ll, 720ll, 5040ll, 40320ll, 362880ll, 3628800ll, 39916800ll, 479001600ll,
195  6227020800ll, 87178291200ll, 1307674368000ll, 20922789888000ll, 355687428096000ll,
196  6402373705728000ll, 121645100408832000ll, 2432902008176640000ll };
197  if (val > 20)
198  return (uint64_t)-1;
199  return table[val];
200 }
201 
202 template <class state, class action, class environment, int bits>
204 {
205  std::string fileName;
206  fileName += prefix;
207  // For unix systems, the prefix should always end in a trailing slash
208  if (fileName.back() != '/' && prefix[0] != 0)
209  fileName+='/';
211  fileName += "-lex.pdb";
212 
213  return fileName;
214 }
215 
216 template <class state, class action, class environment, int bits>
218 {
219  uint64_t value = 1;
220  assert(n >= 0 && k >= 0);
221 
222  for (int i = n; i > k; i--)
223  {
224  value *= i;
225  }
226 
227  return value;
228 }
229 
230 
231 #endif /* TreePermutationPDB_h */
TreePermutationPDB::GetFileName
std::string GetFileName(const char *prefix)
Definition: TreePermutationPDB.h:203
TreePermutationPDB::GetAbstractHash
virtual uint64_t GetAbstractHash(const state &s, int threadID=0) const
Definition: TreePermutationPDB.h:27
TreePermutationPDB::GetPDBHash
virtual uint64_t GetPDBHash(const state &s, int threadID=0) const
Definition: TreePermutationPDB.h:98
TreePermutationPDB::FactorialUpperK
uint64_t FactorialUpperK(int n, int k) const
Definition: TreePermutationPDB.h:217
PermutationPDB::GetFileName
virtual std::string GetFileName(const char *prefix)
Definition: PermutationPDB.h:65
TreePermutationPDB
This class does the basic permutation calculation in lexicographical order.
Definition: TreePermutationPDB.h:21
TreePermutationPDB::Factorial
uint64_t Factorial(int val) const
Definition: TreePermutationPDB.h:191
maxThreads
const int maxThreads
Definition: PDBHeuristic.h:36
PermutationPDB.h
TreePermutationPDB::TreePermutationPDB
TreePermutationPDB(environment *e, const state &s, const std::vector< int > &distincts)
Definition: TreePermutationPDB.h:90
PermutationPDB
This class does the basic permutation calculation with a regular N^2 permutation computation.
Definition: PermutationPDB.h:19
TreePermutationPDB::locsCache
std::vector< std::vector< int > > locsCache
Definition: TreePermutationPDB.h:42
TreePermutationPDB::tempCache
std::vector< std::vector< int > > tempCache
Definition: TreePermutationPDB.h:43
TreePermutationPDB::GetStateFromAbstractState
virtual state GetStateFromAbstractState(state &s)
Definition: TreePermutationPDB.h:28
PDBHeuristic< state, action, environment, state, bits >::SetGoal
void SetGoal(const state &goal)
Definition: PDBHeuristic.h:45
TreePermutationPDB::dualCache
std::vector< std::vector< int > > dualCache
Definition: TreePermutationPDB.h:41
TreePermutationPDB::GetStateFromPDBHash
virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID=0) const
Definition: TreePermutationPDB.h:145
mylog2
int mylog2(int val)
Definition: TreePermutationPDB.h:48
TreePermutationPDB::example
state example
Definition: TreePermutationPDB.h:45
TreePermutationPDB::maxThreads
const int maxThreads
Definition: TreePermutationPDB.h:38
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
bits
constexpr uint64_t bits(uint64_t a, uint64_t b, uint64_t c)
Definition: Hexagon.cpp:16