HOG2
MR1PermutationPDB.h
Go to the documentation of this file.
1 //
2 // MR1PermutationPDB.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 10/14/15.
6 // Copyright © 2015 University of Denver. All rights reserved.
7 //
8 
9 #ifndef MR1PermutationPDB_h
10 #define MR1PermutationPDB_h
11 
12 #include "PermutationPDB.h"
13 
24 template <class state, class action, class environment, int bits = 8>
25 class MR1PermutationPDB : public PermutationPDB<state, action, environment, bits> {
26 public:
27  MR1PermutationPDB(environment *e, const state &s, const std::vector<int> &distincts);
28 
29  virtual uint64_t GetPDBHash(const state &s, int threadID = 0) const;
30  virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID = 0) const;
31  virtual uint64_t GetAbstractHash(const state &s, int threadID = 0) const { return GetPDBHash(s); }
32  virtual state GetStateFromAbstractState(state &s) const { return s; }
33 
34  std::string GetFileName(const char *prefix);
35 private:
38 
39  // cache for computing ranking/unranking
40  mutable std::vector<std::vector<int> > dualCache;
41  mutable std::vector<std::vector<int> > locsCache;
42  mutable std::vector<std::vector<int> > valueStack;
43 
44 };
45 
46 template <class state, class action, class environment, int bits>
47 MR1PermutationPDB<state, action, environment, bits>::MR1PermutationPDB(environment *e, const state &s, const std::vector<int> & distincts)
48 :PermutationPDB<state, action, environment, bits>(e, s, distincts), dualCache(maxThreads), locsCache(maxThreads), valueStack(maxThreads)
49 {
50  this->SetGoal(s);
51 }
52 
53 inline void swap(int &a, int &b)
54 {
55  int tmp = a;
56  a = b;
57  b = tmp;
58 }
59 
60 template <class state, class action, class environment, int bits>
61 uint64_t MR1PermutationPDB<state, action, environment, bits>::GetPDBHash(const state &s, int threadID) const
62 {
63  std::vector<int> &locs = locsCache[threadID];
64  std::vector<int> &dual = dualCache[threadID];
65  std::vector<int> &values = valueStack[threadID];
66  locs.resize(example.size()); // vector for distinct item locations
67  dual.resize(example.size()); // vector for distinct item locations
68  values.resize(0);
69  memset(&locs[0], 0xFF, locs.size()*sizeof(locs[0]));
70  memset(&dual[0], 0xFF, dual.size()*sizeof(dual[0]));
71  int puzzleSize = (int)example.size();
72 
73  // find current duals
74  for (unsigned int x = 0; x < puzzleSize; x++)
75  {
76  if (s.puzzle[x] != -1)
77  dual[s.puzzle[x]] = x;
78  }
79  // get locs by converting from the distinct array
80  for (size_t x = 0; x < distinct.size(); x++)
81  {
82  locs[puzzleSize-x-1] = dual[distinct[distinct.size()-x-1]];
83  //dual[distinct[distinct.size()-x-1]] = -1;
84  }
85  memset(&dual[0], 0xFF, dual.size()*sizeof(dual[0]));
86  // get new duals for the actual locs (after conversion)
87  for (int x = puzzleSize-distinct.size(); x < puzzleSize; x++)
88  {
89  dual[locs[x]] = x;
90  }
91 
92  size_t last = puzzleSize-distinct.size();
93  for (size_t i = puzzleSize; i > last; i--)
94  {
95  values.push_back(locs[i-1]); //val = locs[i-1];//get(perm, i-1);
96 
97  if (dual[i-1] != -1)
98  {
99  swap(locs[i-1], locs[dual[i-1]]);
100  swap(dual[values.back()], dual[i-1]);
101  }
102 
103  // swap(perm, i-1, get(dual, i-1));
104  // swap(dual, s, i-1);
105  }
106  uint64_t result = 0;
107  int cnt = last+1;
108  while (values.size() > 0)
109  {
110  result *= cnt;
111  result += values.back();
112  values.pop_back();
113  cnt++;
114  }
115  return result;
116 }
117 
118 template <class state, class action, class environment, int bits>
119 void MR1PermutationPDB<state, action, environment, bits>::GetStateFromPDBHash(uint64_t hash, state &s, int threadID) const
120 {
121  int puzzleSize = (int)example.size();
122  //s.puzzle.resize(puzzleSize);
123  std::vector<int> &dual = dualCache[threadID];
124  dual.resize(puzzleSize); // vector for distinct item locations
125  for (size_t x = 0; x < dual.size(); x++)
126  dual[x] = x;
127 
128  size_t last = (puzzleSize-distinct.size());
129  memset(&s.puzzle[0], 0xFF, puzzleSize*sizeof(s.puzzle[0]));
130 
131  for (size_t i = puzzleSize; i > last; i--)
132  {
133  swap(dual[hash%i], dual[i-1]);
134  hash = hash/i;
135  s.puzzle[dual[i-1]] = distinct[i-last-1];
136  }
137  s.FinishUnranking();
138 }
139 
140 template <class state, class action, class environment, int bits>
142 {
143  std::string fileName;
144  fileName += prefix;
145  // For unix systems, the prefix should always end in a trailing slash
146  if (fileName.back() != '/' && prefix[0] != 0)
147  fileName+='/';
149  fileName += "-MR1.pdb";
150 
151  return fileName;
152 }
153 
154 
155 #endif /* MR1PermutationPDB_h */
MR1PermutationPDB::dualCache
std::vector< std::vector< int > > dualCache
Definition: MR1PermutationPDB.h:40
PermutationPDB::GetFileName
virtual std::string GetFileName(const char *prefix)
Definition: PermutationPDB.h:65
swap
void swap(int &a, int &b)
Definition: MR1PermutationPDB.h:53
maxThreads
const int maxThreads
Definition: PDBHeuristic.h:36
PermutationPDB.h
MR1PermutationPDB::GetStateFromPDBHash
virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID=0) const
Definition: MR1PermutationPDB.h:119
MR1PermutationPDB::GetAbstractHash
virtual uint64_t GetAbstractHash(const state &s, int threadID=0) const
Definition: MR1PermutationPDB.h:31
PermutationPDB
This class does the basic permutation calculation with a regular N^2 permutation computation.
Definition: PermutationPDB.h:19
MR1PermutationPDB::valueStack
std::vector< std::vector< int > > valueStack
Definition: MR1PermutationPDB.h:42
MR1PermutationPDB::MR1PermutationPDB
MR1PermutationPDB(environment *e, const state &s, const std::vector< int > &distincts)
Definition: MR1PermutationPDB.h:47
MR1PermutationPDB::GetStateFromAbstractState
virtual state GetStateFromAbstractState(state &s) const
Definition: MR1PermutationPDB.h:32
MR1PermutationPDB::GetPDBHash
virtual uint64_t GetPDBHash(const state &s, int threadID=0) const
Definition: MR1PermutationPDB.h:61
PDBHeuristic< state, action, environment, state, bits >::SetGoal
void SetGoal(const state &goal)
Definition: PDBHeuristic.h:45
MR1PermutationPDB::GetFileName
std::string GetFileName(const char *prefix)
Definition: MR1PermutationPDB.h:141
MR1PermutationPDB
This class uses the first of two Myrvold-Russkey ranking functions which run in linear time,...
Definition: MR1PermutationPDB.h:25
MR1PermutationPDB::locsCache
std::vector< std::vector< int > > locsCache
Definition: MR1PermutationPDB.h:41
bits
constexpr uint64_t bits(uint64_t a, uint64_t b, uint64_t c)
Definition: Hexagon.cpp:16