HOG2
PermutationPDB.h
Go to the documentation of this file.
1 //
2 // PermutationPDB.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 8/27/15.
6 // Copyright (c) 2015 University of Denver. All rights reserved.
7 //
8 
9 #ifndef hog2_glut_PermutationPDB_h
10 #define hog2_glut_PermutationPDB_h
11 
12 #include "PDBHeuristic.h"
13 
18 template <class state, class action, class environment, int bits = 8>
19 class PermutationPDB : public PDBHeuristic<state, action, environment, state, bits> {
20 public:
21  PermutationPDB(environment *e, const state &s, const std::vector<int> &distincts);
22 
23  virtual uint64_t GetPDBSize() const;
24 
25  virtual uint64_t GetPDBHash(const state &s, int threadID = 0) const = 0;
26  virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID = 0) const = 0;
27  virtual uint64_t GetAbstractHash(const state &s, int threadID = 0) const = 0;
28  virtual state GetStateFromAbstractState(state &s) const = 0;
29 
30  bool Load(FILE *f);
31  void Save(FILE *f);
32  bool Load(const char *prefix);
33  void Save(const char *prefix);
34  virtual std::string GetFileName(const char *prefix);
35 private:
36  uint64_t Factorial(int val) const;
37  uint64_t FactorialUpperK(int n, int k) const;
38 
39 protected:
40  std::vector<int> distinct;
41  size_t puzzleSize;
42  uint64_t pdbSize;
43  state example;
44 };
45 
46 template <class state, class action, class environment, int bits>
47 PermutationPDB<state, action, environment, bits>::PermutationPDB(environment *e, const state &s, const std::vector<int> &distincts)
48 :PDBHeuristic<state, action, environment, state, bits>(e), distinct(distincts), puzzleSize(s.size()), example(s)
49 {
50  pdbSize = 1;
51  for (int x = (int)s.size(); x > s.size()-distincts.size(); x--)
52  {
53  pdbSize *= x;
54  }
55 }
56 
57 template <class state, class action, class environment, int bits>
59 {
60  return pdbSize;
61 }
62 
63 
64 template <class state, class action, class environment, int bits>
66 {
67  std::string fileName;
68  fileName += prefix;
69  // For unix systems, the prefix should always end in a trailing slash
70  if (fileName.back() != '/' && prefix[0] != 0)
71  fileName+='/';
73  fileName += "-";
74  for (size_t x = 0; x < PDBHeuristic<state, action, environment, state, bits>::goalState.size(); x++)
75  {
76  fileName += std::to_string(PDBHeuristic<state, action, environment, state, bits>::goalState[0].puzzle[x]);
77  fileName += ";";
78  }
79  fileName.pop_back(); // remove colon
80  fileName += "-";
81  for (size_t x = 0; x < distinct.size(); x++)
82  {
83  fileName += std::to_string(distinct[x]);
84  fileName += ";";
85  }
86  fileName.pop_back(); // remove colon
87  fileName += "-";
88  fileName += std::to_string(bits);
89  fileName += "bpe";
90  return fileName;
91 }
92 
93 template <class state, class action, class environment, int bits>
95 {
96  FILE *f = fopen(GetFileName(prefix).c_str(), "r+");
97  if (f == 0)
98  {
99  printf("Unable to open for loading '%s'\n", GetFileName(prefix).c_str());
100  return false;
101  }
102  bool result = Load(f);
103  fclose(f);
104  return result;
105 }
106 
107 template <class state, class action, class environment, int bits>
109 {
110  FILE *f = fopen(GetFileName(prefix).c_str(), "w+");
111  if (f == 0)
112  {
113  fprintf(stderr, "Error saving");
114  return;
115  }
116  Save(f);
117  fclose(f);
118 }
119 
120 template <class state, class action, class environment, int bits>
122 {
124  {
125  return false;
126  }
127  if (fread(&puzzleSize, sizeof(puzzleSize), 1, f) != 1)
128  return false;
129  if (fread(&pdbSize, sizeof(pdbSize), 1, f) != 1)
130  return false;
131  if (fread(&example, sizeof(example), 1, f) != 1)
132  return false;
133  size_t distinctSize = distinct.size();
134  if (fread(&distinctSize, sizeof(distinctSize), 1, f) != 1)
135  return false;
136  distinct.resize(distinctSize);
137  if (fread(&distinct[0], sizeof(distinct[0]), distinct.size(), f) != distinctSize)
138  return false;
139  return true;
140 }
141 
142 template <class state, class action, class environment, int bits>
144 {
146  fwrite(&puzzleSize, sizeof(puzzleSize), 1, f);
147  fwrite(&pdbSize, sizeof(pdbSize), 1, f);
148  fwrite(&example, sizeof(example), 1, f);
149  size_t distinctSize = distinct.size();
150  fwrite(&distinctSize, sizeof(distinctSize), 1, f);
151  fwrite(&distinct[0], sizeof(distinct[0]), distinct.size(), f);
152 }
153 
154 template <class state, class action, class environment, int bits>
156 {
157  uint64_t value = 1;
158  assert(n >= 0 && k >= 0);
159 
160  for (int i = n; i > k; i--)
161  {
162  value *= i;
163  }
164 
165  return value;
166 }
167 
168 #endif
PermutationPDB::example
state example
Definition: PermutationPDB.h:43
PermutationPDB::GetPDBSize
virtual uint64_t GetPDBSize() const
Definition: PermutationPDB.h:58
PermutationPDB::FactorialUpperK
uint64_t FactorialUpperK(int n, int k) const
Definition: PermutationPDB.h:155
PermutationPDB::GetStateFromAbstractState
virtual state GetStateFromAbstractState(state &s) const =0
PermutationPDB::GetFileName
virtual std::string GetFileName(const char *prefix)
Definition: PermutationPDB.h:65
PermutationPDB::GetPDBHash
virtual uint64_t GetPDBHash(const state &s, int threadID=0) const =0
PermutationPDB::distinct
std::vector< int > distinct
Definition: PermutationPDB.h:40
PermutationPDB::Load
bool Load(FILE *f)
Definition: PermutationPDB.h:121
PermutationPDB
This class does the basic permutation calculation with a regular N^2 permutation computation.
Definition: PermutationPDB.h:19
PermutationPDB::puzzleSize
size_t puzzleSize
Definition: PermutationPDB.h:41
PermutationPDB::PermutationPDB
PermutationPDB(environment *e, const state &s, const std::vector< int > &distincts)
Definition: PermutationPDB.h:47
PDBHeuristic
Definition: PDBHeuristic.h:39
PDBHeuristic::Save
virtual void Save(const char *prefix)=0
PermutationPDB::Save
void Save(FILE *f)
Definition: PermutationPDB.h:143
PermutationPDB::pdbSize
uint64_t pdbSize
Definition: PermutationPDB.h:42
PermutationPDB::GetAbstractHash
virtual uint64_t GetAbstractHash(const state &s, int threadID=0) const =0
PDBHeuristic.h
PermutationPDB::GetStateFromPDBHash
virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID=0) const =0
PermutationPDB::Factorial
uint64_t Factorial(int val) const
bits
constexpr uint64_t bits(uint64_t a, uint64_t b, uint64_t c)
Definition: Hexagon.cpp:16