HOG2
LexPermutationPDB.h
Go to the documentation of this file.
1 //
2 // LexPermutationPDB.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 8/10/16.
6 // Copyright © 2016 University of Denver. All rights reserved.
7 //
8 
9 #ifndef LexPermutationPDB_h
10 #define LexPermutationPDB_h
11 
12 #include "PermutationPDB.h"
13 
18 template <class state, class action, class environment, int bits = 8>
19 class LexPermutationPDB : public PermutationPDB<state, action, environment, bits> {
20 public:
21  LexPermutationPDB(environment *e, const state &s, const std::vector<int> &distincts);
22  LexPermutationPDB(environment *e, const std::vector<state> &s, const std::vector<int> &distincts);
23  virtual uint64_t GetPDBHash(const state &s, int threadID = 0) const;
24  virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID = 0) const;
25  virtual uint64_t GetAbstractHash(const state &s, int threadID = 0) const { return GetPDBHash(s); }
26  virtual state GetStateFromAbstractState(state &s) const { return s; }
27 
28  std::string GetFileName(const char *prefix);
29 private:
33 
34  uint64_t Factorial(int val) const;
35  uint64_t FactorialUpperK(int n, int k) const;
36 
37 protected:
38 
39  // cache for computing ranking/unranking
40  mutable std::vector<std::vector<int> > dualCache;
41  mutable std::vector<std::vector<int> > locsCache;
42 };
43 
44 template <class state, class action, class environment, int bits>
45 LexPermutationPDB<state, action, environment, bits>::LexPermutationPDB(environment *e, const state &s, const std::vector<int> &distincts)
46 :PermutationPDB<state, action, environment, bits>(e, s, distincts), dualCache(maxThreads), locsCache(maxThreads)
47 {
48  this->SetGoal(s);
49 }
50 
51 template <class state, class action, class environment, int bits>
52 LexPermutationPDB<state, action, environment, bits>::LexPermutationPDB(environment *e, const std::vector<state> &s, const std::vector<int> &distincts)
53 :PermutationPDB<state, action, environment, bits>(e, s[0], distincts), dualCache(maxThreads), locsCache(maxThreads)
54 {
55  this->SetGoal(s);
56 }
57 
58 template <class state, class action, class environment, int bits>
59 uint64_t LexPermutationPDB<state, action, environment, bits>::GetPDBHash(const state &s, int threadID) const
60 {
61  std::vector<int> &locs = locsCache[threadID];
62  std::vector<int> &dual = dualCache[threadID];
63  // TODO: test definition
64  locs.resize(distinct.size()); // vector for distinct item locations
65  dual.resize(s.size()); // vector for distinct item locations
66 
67  // find item locations
68  for (unsigned int x = 0; x < s.size(); x++)
69  {
70  if (s.puzzle[x] != -1)
71  dual[s.puzzle[x]] = x;
72  }
73  for (size_t x = 0; x < distinct.size(); x++)
74  {
75  locs[x] = dual[distinct[x]];
76  }
77 
78  uint64_t hashVal = 0;
79  int numEntriesLeft = (int)s.size();
80 
81  for (unsigned int x = 0; x < locs.size(); x++)
82  {
83  hashVal += locs[x]*FactorialUpperK(numEntriesLeft-1, s.size()-distinct.size());
84  numEntriesLeft--;
85 
86  // decrement locations of remaining items
87  for (unsigned y = x; y < locs.size(); y++)
88  {
89  if (locs[y] > locs[x])
90  locs[y]--;
91  }
92  }
93  return hashVal;
94 }
95 
96 template <class state, class action, class environment, int bits>
97 void LexPermutationPDB<state, action, environment, bits>::GetStateFromPDBHash(uint64_t hash, state &s, int threadID) const
98 {
99  uint64_t hashVal = hash;
100  std::vector<int> &dual = dualCache[threadID];
101 
102  dual.resize(distinct.size());
103 
104  int numEntriesLeft = puzzleSize-distinct.size()+1;
105  for (int x = distinct.size()-1; x >= 0; x--)
106  {
107  dual[x] = hashVal%numEntriesLeft;
108  hashVal /= numEntriesLeft;
109  numEntriesLeft++;
110  for (size_t y = x+1; y < distinct.size(); y++)
111  {
112  if (dual[y] >= dual[x])
113  dual[y]++;
114  }
115  }
116  // s.puzzle.resize(puzzleSize);
117  std::fill(&s.puzzle[0], &s.puzzle[s.size()], -1);
118  for (int x = 0; x < dual.size(); x++)
119  s.puzzle[dual[x]] = distinct[x];
120  s.FinishUnranking();
121 }
122 
123 void GetStateFromHash(uint64_t hash, int *pieces, int count)
124 {
125  int numEntriesLeft = 1;
126  for (int x = count-1; x >= 0; x--)
127  {
128  pieces[x] = hash%numEntriesLeft;
129  hash /= numEntriesLeft;
130  numEntriesLeft++;
131  for (int y = x+1; y < count; y++)
132  {
133  if (pieces[y] >= pieces[x])
134  pieces[y]++;
135  }
136  }
137 }
138 
139 
140 template <class state, class action, class environment, int bits>
142 {
143  static uint64_t table[21] =
144  { 1ll, 1ll, 2ll, 6ll, 24ll, 120ll, 720ll, 5040ll, 40320ll, 362880ll, 3628800ll, 39916800ll, 479001600ll,
145  6227020800ll, 87178291200ll, 1307674368000ll, 20922789888000ll, 355687428096000ll,
146  6402373705728000ll, 121645100408832000ll, 2432902008176640000ll };
147  if (val > 20)
148  return (uint64_t)-1;
149  return table[val];
150 }
151 
152 template <class state, class action, class environment, int bits>
154 {
155  std::string fileName;
156  fileName += prefix;
157  // For unix systems, the prefix should always end in a trailing slash
158  if (fileName.back() != '/' && prefix[0] != 0)
159  fileName+='/';
161  fileName += "-lex.pdb";
162 
163  return fileName;
164 }
165 
166 template <class state, class action, class environment, int bits>
168 {
169  uint64_t value = 1;
170  assert(n >= 0 && k >= 0);
171 
172  for (int i = n; i > k; i--)
173  {
174  value *= i;
175  }
176 
177  return value;
178 }
179 
180 
181 #endif /* LexPermutation_h */
LexPermutationPDB::GetStateFromAbstractState
virtual state GetStateFromAbstractState(state &s) const
Definition: LexPermutationPDB.h:26
LexPermutationPDB::GetFileName
std::string GetFileName(const char *prefix)
Definition: LexPermutationPDB.h:153
LexPermutationPDB::GetPDBHash
virtual uint64_t GetPDBHash(const state &s, int threadID=0) const
Definition: LexPermutationPDB.h:59
PermutationPDB::GetFileName
virtual std::string GetFileName(const char *prefix)
Definition: PermutationPDB.h:65
maxThreads
const int maxThreads
Definition: PDBHeuristic.h:36
pieces
const int pieces
Definition: RubiksCube7Edges.h:17
PermutationPDB.h
LexPermutationPDB::dualCache
std::vector< std::vector< int > > dualCache
Definition: LexPermutationPDB.h:40
GetStateFromHash
void GetStateFromHash(uint64_t hash, int *pieces, int count)
Definition: LexPermutationPDB.h:123
PermutationPDB
This class does the basic permutation calculation with a regular N^2 permutation computation.
Definition: PermutationPDB.h:19
LexPermutationPDB::locsCache
std::vector< std::vector< int > > locsCache
Definition: LexPermutationPDB.h:41
PDBHeuristic< state, action, environment, state, bits >::SetGoal
void SetGoal(const state &goal)
Definition: PDBHeuristic.h:45
LexPermutationPDB::GetStateFromPDBHash
virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID=0) const
Definition: LexPermutationPDB.h:97
LexPermutationPDB::FactorialUpperK
uint64_t FactorialUpperK(int n, int k) const
Definition: LexPermutationPDB.h:167
LexPermutationPDB::LexPermutationPDB
LexPermutationPDB(environment *e, const state &s, const std::vector< int > &distincts)
Definition: LexPermutationPDB.h:45
LexPermutationPDB::Factorial
uint64_t Factorial(int val) const
Definition: LexPermutationPDB.h:141
LexPermutationPDB
This class does the basic permutation calculation with a regular N^2 permutation computation.
Definition: LexPermutationPDB.h:19
LexPermutationPDB::GetAbstractHash
virtual uint64_t GetAbstractHash(const state &s, int threadID=0) const
Definition: LexPermutationPDB.h:25
bits
constexpr uint64_t bits(uint64_t a, uint64_t b, uint64_t c)
Definition: Hexagon.cpp:16