9 #ifndef TreePermutationPDB_h
10 #define TreePermutationPDB_h
20 template <
class state,
class action,
class environment,
int bits = 8>
25 virtual uint64_t
GetPDBHash(
const state &s,
int threadID = 0)
const;
89 template <
class state,
class action,
class environment,
int bits>
97 template <
class state,
class action,
class environment,
int bits>
100 std::vector<int> &values = locsCache[threadID];
101 std::vector<int> &dual = dualCache[threadID];
103 values.resize(distinct.size());
104 dual.resize(s.puzzle.size());
107 for (
unsigned int x = 0; x < s.puzzle.size(); x++)
109 if (s.puzzle[x] != -1)
110 dual[s.puzzle[x]] = x;
112 for (
int x = 0; x < distinct.size(); x++)
114 values[x] = dual[distinct[x]];
118 int k =
mylog2(s.puzzle.size());
120 std::vector<int> &temp = tempCache[threadID];
122 temp.resize((1<<(1+k))-1);
123 std::fill(temp.begin(), temp.end(), 0);
124 for (
int x = 0; x < values.size(); x++)
126 int counter = values[x];
127 int node = (1<<k)-1+values[x];
128 for (
int y = 0; y < k; y++)
130 int isEven = (1-(
node&1));
131 counter -= isEven*(temp[(
node-1)>>1] - temp[
node]);
138 rank = rank*(s.puzzle.size()-x)+counter;
144 template <
class state,
class action,
class environment,
int bits>
147 size_t count = example.puzzle.size();
148 s.puzzle.resize(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));
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--)
160 values[x] = hash%numEntriesLeft;
161 hash /= numEntriesLeft;
164 memset(&s.puzzle[0], 0xFF, s.puzzle.size()*
sizeof(s.puzzle[0]));
165 for (
int x = 0; x < values.size(); x++)
167 int digit = values[x];
169 for (
int y = 0; y < k; y++)
173 uint32_t diff = digit-temp[
node];
175 digit -= diff*temp[
node];
184 values[x] =
node - (1<<k) + 1;
185 s.puzzle[values[x]] = distinct[x];
190 template <
class state,
class action,
class environment,
int bits>
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 };
202 template <
class state,
class action,
class environment,
int bits>
205 std::string fileName;
208 if (fileName.back() !=
'/' && prefix[0] != 0)
211 fileName +=
"-lex.pdb";
216 template <
class state,
class action,
class environment,
int bits>
220 assert(n >= 0 && k >= 0);
222 for (
int i = n; i > k; i--)