20 #pragma mark Declarations
25 template <
class state,
class action>
55 template <
class state,
class action>
62 double HCost(
const state &s)
const;
64 { assert(!
"Additive Gost used but not defined for this class\n"); }
68 static uint64_t
Hash(
const state &s)
73 for (
size_t x = 0; x < tmp.size(); x++)
74 dual.puzzle[tmp.puzzle[x]] = x;
75 return mr1.
Rank(tmp.puzzle, dual.puzzle, s.size(), s.size());
84 static bool Output_Puzzles(std::vector<state> &puzzle_vector,
bool write_puzz_num);
86 std::vector<std::vector<int> > &permutations,
bool puzz_num_start);
105 virtual double DefaultH(
const state &s)
const {
return 0; }
110 uint64_t
nUpperk(
int n,
int k)
const;
119 virtual bool State_Check(
const state &to_check) = 0;
127 std::vector<std::vector<uint8_t> >
PDB;
136 template <
typename state,
typename environment>
140 virtual double HCost(
const state &a,
const state &b)
const
141 { state g;
return h->HCost(
e->TranformToStandardGoal(a, b), g); }
1012 template <
class state,
class action>
1099 template <
class state,
class action>
1102 static uint64_t table[21] =
1103 { 1ll, 1ll, 2ll, 6ll, 24ll, 120ll, 720ll, 5040ll, 40320ll, 362880ll, 3628800ll, 39916800ll, 479001600ll,
1104 6227020800ll, 87178291200ll, 1307674368000ll, 20922789888000ll, 355687428096000ll,
1105 6402373705728000ll, 121645100408832000ll, 2432902008176640000ll };
1107 return (uint64_t)-1;
1114 template <
class state,
class action>
1117 factorialCache.resize(maxItem+1);
1118 for (
int n = 0; n < factorialCache.size(); n++)
1120 factorialCache[n].resize(maxItem-minPattern+1);
1121 for (
int k = 0; k < factorialCache[n].size(); k++)
1124 for (
int i = n; i > k; i--)
1128 factorialCache[n][k] = value;
1137 template <
class state,
class action>
1140 assert(factorialCache.size() > n);
1141 assert(factorialCache[n].size() > k);
1142 return factorialCache[n][k];
1275 template <
class state,
class action>
1279 mr1.Unrank(hash, s.puzzle, tmp.puzzle, s.size(), s.size());
1280 s.FinishUnranking();
1283 template <
class state,
class action>
1288 for (
size_t x = 0; x < tmp.size(); x++)
1289 dual.puzzle[tmp.puzzle[x]] = x;
1291 uint64_t hash = mr1.Rank(&tmp.puzzle[0], &dual.puzzle[0], s.size(), s.size());
1295 template <
class state,
class action>
1300 for (
size_t x = 0; x < b.size(); x++)
1302 dual.puzzle[b.puzzle[x]] = x;
1304 for (
size_t x = 0; x < b.size(); x++)
1306 result.puzzle[x] = dual.puzzle[a.puzzle[x]];
1308 result.FinishUnranking();
1359 template <
class state,
class action>
1362 const auto size = to_check.size();
1364 bool in_puzzle[size];
1366 for (
size_t i = 0; i < size; i++)
1368 in_puzzle[i] =
false;
1371 for (
size_t i = 0; i < size; i++)
1373 in_puzzle[to_check[i]] =
true;
1376 for (
size_t i = 0; i < size; i++)
1393 template <
class state,
class action>
1397 for (
unsigned i = 0; i < puzzle_vector.size(); i++)
1402 if (!Check_Permutation(puzzle_vector[i].puzzle))
1404 std::cerr <<
"Invalid Puzzle: " << puzzle_vector[i] <<
'\n';
1409 for (
unsigned i = 0; i < puzzle_vector.size(); i++)
1413 printf(
"%u ", i + 1);
1415 printf(
"%d", puzzle_vector[i].puzzle[0]);
1417 for (
unsigned j = 1; j < puzzle_vector[i].puzzle.size(); j++)
1419 printf(
" %d", puzzle_vector[i].puzzle[j]);
1427 template <
class state,
class action>
1429 std::vector<std::vector<int> > &permutations,
bool puzz_num_start)
1431 std::ifstream ifs(filename, std::ios::in);
1435 fprintf(stderr,
"File Reading Failed\n");
1439 std::string s, temp;
1441 std::vector<int> puzz_ints;
1444 unsigned puzz_count = 0;
1446 while(!ifs.eof() && (puzz_count < max_puzzles || max_puzzles==0) )
1453 for (
unsigned int i = 0; i < s.length(); i++)
1455 if (s.at(i) ==
' ' || s.at(i) ==
'\t')
1457 if (temp.length() > 0)
1459 if (puzz_num_start && first)
1466 puzz_ints.push_back(atoi(temp.c_str()));
1473 temp.push_back(s.at(i));
1478 if (temp.length() > 0)
1480 puzz_ints.push_back(atoi(temp.c_str()));
1485 if (puzz_ints.size() == size && Check_Permutation(puzz_ints))
1488 permutations.push_back(puzz_ints);
1498 template <
class state,
class action>
1501 std::vector<int> permutation;
1503 for (
unsigned i = 0; i < size; i++)
1505 permutation.push_back(i);
1512 { index = rand() % size;
1513 temp = permutation[size - 1];
1514 permutation[size - 1] = permutation[index];
1515 permutation[index] = temp;
1523 template <
class state,
class action>
1526 for (
unsigned i = 0; i < puzzles.size(); i++)
1528 if (!State_Check(puzzles[i]) || !Check_Permutation(puzzles[i].puzzle))
1530 std::cerr << puzzles[i] <<
'\n';
1531 std::cerr <<
"Invalid Puzzle\n";
1539 #endif // PERMPUZZ_H