HOG2
RubiksCubeCorners.h
Go to the documentation of this file.
1 //
2 // RubiksCorner.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 1/11/13.
6 // Copyright (c) 2013 University of Denver. All rights reserved.
7 //
8 
9 #ifndef __hog2_glut__RubiksCorner__
10 #define __hog2_glut__RubiksCorner__
11 
12 #include <iostream>
13 #include <stdint.h>
14 #include <vector>
15 #include "PDBHeuristic.h"
16 #include "MR1Permutation.h"
17 #include "SearchEnvironment.h"
18 
20 {
21 public:
23  void Reset();
24  int GetCubeInLoc(unsigned int whichLoc) const;
25  void SetCubeInLoc(unsigned int whichLoc, int cube);
26  uint64_t GetCubeOrientation(unsigned int whichLoc) const;
27  void SetCubeOrientation(unsigned int whichLoc, int orient); // orientation is the offset of the 0(low) side
28  int GetFaceInLoc(unsigned int whichLoc) const;
29  void Rotate(uint64_t a, uint64_t b, uint64_t c, uint64_t d);
30  void Swap(uint64_t a, uint64_t b, uint64_t c, uint64_t d);
31  // 48 bits
32  // 16 bits of rotations
33  // 32 bits of pieces
34 //private:
35  uint64_t state;
36 };
37 
39 {
40 public:
42  void Reset();
43  int GetCubeInLoc(unsigned int whichLoc) const;
44  void SetCubeInLoc(unsigned int whichLoc, int cube);
45  uint64_t GetCubeOrientation(unsigned int whichLoc) const;
46  void SetCubeOrientation(unsigned int whichLoc, int orient); // orientation is the offset of the 0(low) side
47  int GetFaceInLoc(unsigned int whichLoc) const; // loc 0...24
48  void Rotate(uint64_t a, uint64_t b, uint64_t c, uint64_t d);
49  void Swap(uint64_t a, uint64_t b, uint64_t c, uint64_t d);
50  // 48 bits
51  // 16 bits of rotations
52  // 32 bits of pieces
53  //private:
54  uint8_t state[16];
55 };
56 
57 //typedef RubiksCornerStateBits RubiksCornerState;
59 
60 namespace std {
61  template <> struct hash<RubiksCornerStateBits>
62  {
63  size_t operator()(const RubiksCornerStateBits &s) const
64  {
65  return s.state;
66  }
67  };
68 
69  template <> struct hash<RubiksCornerStateArray>
70  {
71  size_t operator()(const RubiksCornerStateArray &s) const
72  {
73  size_t result = 0;
74  for (int x = 0; x < 16; x++)
75  result ^= (s.state[x])<<(x*4);
76  return result;
77  }
78  };
79 }
80 
81 
82 
83 static bool operator==(const RubiksCornerStateBits &l1, const RubiksCornerStateBits &l2)
84 {
85  return l1.state == l2.state;
86 }
87 
88 static bool operator==(const RubiksCornerStateArray &l1, const RubiksCornerStateArray &l2)
89 {
90  for (int x = 0; x < 16; x++)
91  if (l1.state[x] != l2.state[x])
92  return false;
93  return true;
94 }
95 
96 static std::ostream &operator<<(std::ostream &out, const RubiksCornerState &s)
97 {
98  for (int x = 0; x < 8; x++)
99  //out << s.GetCubeInLoc(x) << " [" << s.GetCubeOrientation(x) << "] ";
100  out << s.GetCubeInLoc(x) << " [" << s.GetCubeOrientation(s.GetCubeInLoc(x)) << "] ";
101  return out;
102 }
103 
104 
106 
108 public:
111  int length() { if (next == 0) return 1; return 1+next->length(); }
112 };
113 
114 class RubiksCorner : public SearchEnvironment<RubiksCornerState, RubiksCornersAction>
115 {
116 public:
118  {
119  for (int x = 0; x < 18; x++)
120  {
121  moves[x].act = x;
122  if (x != 17)
123  moves[x].next = &moves[x+1];
124  } moves[17].next = 0;
125  }
126 
128  virtual void GetSuccessors(const RubiksCornerState &nodeID, std::vector<RubiksCornerState> &neighbors) const;
129  virtual void GetActions(const RubiksCornerState &nodeID, std::vector<RubiksCornersAction> &actions) const;
130  virtual RubiksCornersAction GetAction(const RubiksCornerState &s1, const RubiksCornerState &s2) const;
131  virtual void ApplyAction(RubiksCornerState &s, RubiksCornersAction a) const;
132 
135  void unrankPlayer(uint64_t d, RubiksCornerState & s, int who)
136  {
137  GetStateFromHash(d, s);
138  }
139  //
140  int64_t getMaxSinglePlayerRank();
141  int64_t getMaxSinglePlayerRank2();
142  int64_t getMaxSinglePlayerRank2(int64_t firstIndex);
143 
146 
147  int64_t rankPlayer(RubiksCornerState &s, int who)
148  { return GetStateHash(s); }
149  void rankPlayerFirstTwo(const RubiksCornerState &s, int who, int64_t &rank);
150  void rankPlayerRemaining(const RubiksCornerState &s, int who, int64_t &rank);
151  void rankPlayer(RubiksCornerState &s, int who, int64_t &index1, int64_t &index2)
152  { rankPlayerFirstTwo(s, 0, index1); rankPlayerRemaining(s, 0, index2); }
153 
154 
155  virtual void GetNextState(const RubiksCornerState &, RubiksCornersAction , RubiksCornerState &) const;
156 
157  virtual bool InvertAction(RubiksCornersAction &a) const;
158 
160  virtual double HCost(const RubiksCornerState &node1, const RubiksCornerState &node2) const { return 0; }
161 
164  virtual double HCost(const RubiksCornerState &node) const
165  { return 0; }
166 
167  virtual double GCost(const RubiksCornerState &node1, const RubiksCornerState &node2) const { return 1.0; }
168  virtual double GCost(const RubiksCornerState &node, const RubiksCornersAction &act) const { return 1.0; }
169  virtual bool GoalTest(const RubiksCornerState &node, const RubiksCornerState &goal) const { return GoalTest(node); }
170 
172  virtual bool GoalTest(const RubiksCornerStateBits &node) const;
173  virtual bool GoalTest(const RubiksCornerStateArray &node) const;
174 
175  uint64_t GetStateHash(const RubiksCornerState &node) const;
176  void GetStateFromHash(uint64_t hash, RubiksCornerState &node) const;
177  virtual uint64_t GetActionHash(RubiksCornersAction act) const { return 0; }
178 
179  virtual void OpenGLDraw() const;
180  virtual void OpenGLDraw(const RubiksCornerState&) const;
182  virtual void OpenGLDraw(const RubiksCornerState&, const RubiksCornerState&, float) const;
183  virtual void OpenGLDraw(const RubiksCornerState&, const RubiksCornersAction&) const;
184  void OpenGLDrawCube(const RubiksCornerState &s, int cube) const;
185 
186 private:
187  void SetFaceColor(int face, const RubiksCornerState&) const;
188  // void SetFaceColor(int face, const RubiksCornerState&) const;
189  static uint64_t MRRank(int n, uint64_t perm, uint64_t dual);
190  static void MRUnrank2(int n, uint64_t r, uint64_t &perm);
192 };
193 
194 class RubikCornerPDB : public PDBHeuristic<RubiksCornerState, RubiksCornersAction, RubiksCorner, RubiksCornerState, 4> {
195 public:
196  RubikCornerPDB(RubiksCorner *e, const RubiksCornerState &s, std::vector<int> &distinctCorners);
197  static uint64_t GetStateSpaceSize();
198  static uint64_t GetStateHash(const RubiksCornerState &s);
199  static void GetStateFromHash(RubiksCornerState &s, uint64_t hash);
200  uint64_t GetPDBSize() const;
201  uint64_t GetPDBHash(const RubiksCornerState &s, int threadID = 0) const;
202  virtual uint64_t GetAbstractHash(const RubiksCornerState &s, int threadID = 0) const { return GetPDBHash(s); }
203  void GetStateFromPDBHash(uint64_t hash, RubiksCornerState &s, int threadID = 0) const;
205 
206  virtual bool Load(const char *prefix);
207  virtual void Save(const char *prefix);
208  virtual bool Load(FILE *f);
209  virtual void Save(FILE *f);
210  virtual std::string GetFileName(const char *prefix);
211 private:
212  uint64_t Factorial(int val) const;
213  uint64_t FactorialUpperK(int n, int k) const;
214  std::vector<int> corners;
215  size_t puzzleSize;
216  uint64_t pdbSize;
218 
219  // cache for computing ranking/unranking
220 // mutable std::vector<std::vector<int> > dual;
221 // mutable std::vector<std::vector<int> > locs;
222 };
223 
224 
225 #define Rubik RubiksCorner
226 #define RubikState RubiksCornerState
227 #define RubikMove RubikCornerMove
228 
229 #endif /* defined(__hog2_glut__RubiksCorner__) */
230 
231 
RubiksCornerStateArray
Definition: RubiksCubeCorners.h:38
RubikCornerPDB::GetPDBHash
uint64_t GetPDBHash(const RubiksCornerState &s, int threadID=0) const
Definition: RubiksCubeCorners.cpp:1218
RubiksCornerStateBits::state
uint64_t state
Definition: RubiksCubeCorners.h:35
RubiksCornersAction
int RubiksCornersAction
Definition: RubiksCubeCorners.h:105
RubiksCorner::moves
RubikCornerMove moves[18]
Definition: RubiksCubeCorners.h:191
RubikCornerPDB::GetAbstractHash
virtual uint64_t GetAbstractHash(const RubiksCornerState &s, int threadID=0) const
Definition: RubiksCubeCorners.h:202
operator==
static bool operator==(const RubiksCornerStateBits &l1, const RubiksCornerStateBits &l2)
Definition: RubiksCubeCorners.h:83
RubiksCornerStateArray::GetCubeOrientation
uint64_t GetCubeOrientation(unsigned int whichLoc) const
Definition: RubiksCubeCorners.cpp:248
RubiksCorner::MRUnrank2
static void MRUnrank2(int n, uint64_t r, uint64_t &perm)
Definition: RubiksCubeCorners.cpp:731
RubiksCorner::OpenGLDrawCube
void OpenGLDrawCube(const RubiksCornerState &s, int cube) const
Definition: RubiksCubeCorners.cpp:766
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
RubikCornerPDB::GetStateFromPDBHash
void GetStateFromPDBHash(uint64_t hash, RubiksCornerState &s, int threadID=0) const
Definition: RubiksCubeCorners.cpp:1283
RubikCornerMove::length
int length()
Definition: RubiksCubeCorners.h:111
RubikCornerPDB::Factorial
uint64_t Factorial(int val) const
Definition: RubiksCubeCorners.cpp:1450
RubiksCornerStateBits::GetFaceInLoc
int GetFaceInLoc(unsigned int whichLoc) const
Definition: RubiksCubeCorners.cpp:180
RubiksCornerStateBits::Rotate
void Rotate(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
Definition: RubiksCubeCorners.cpp:189
RubiksCorner::InvertAction
virtual bool InvertAction(RubiksCornersAction &a) const
Definition: RubiksCubeCorners.cpp:467
RubikCornerPDB::GetStateFromAbstractState
RubiksCornerState GetStateFromAbstractState(RubiksCornerState &s) const
Definition: RubiksCubeCorners.h:204
RubikCornerPDB::pdbSize
uint64_t pdbSize
Definition: RubiksCubeCorners.h:216
RubiksCorner::rankPlayerRemaining
void rankPlayerRemaining(const RubiksCornerState &s, int who, int64_t &rank)
Definition: RubiksCubeCorners.cpp:536
RubiksCornerStateBits::GetCubeOrientation
uint64_t GetCubeOrientation(unsigned int whichLoc) const
Definition: RubiksCubeCorners.cpp:163
RubiksCorner::GCost
virtual double GCost(const RubiksCornerState &node, const RubiksCornersAction &act) const
Definition: RubiksCubeCorners.h:168
RubikCornerPDB::puzzleSize
size_t puzzleSize
Definition: RubiksCubeCorners.h:215
RubikCornerPDB::GetFileName
virtual std::string GetFileName(const char *prefix)
Definition: RubiksCubeCorners.cpp:1417
RubikCornerPDB
Definition: RubiksCubeCorners.h:194
RubiksCornerStateArray::state
uint8_t state[16]
Definition: RubiksCubeCorners.h:54
RubiksCorner::UndoMove
void UndoMove(RubiksCornerState &s, RubikCornerMove *a)
Definition: RubiksCubeCorners.cpp:498
std::hash< RubiksCornerStateBits >::operator()
size_t operator()(const RubiksCornerStateBits &s) const
Definition: RubiksCubeCorners.h:63
RubiksCorner::rankPlayer
void rankPlayer(RubiksCornerState &s, int who, int64_t &index1, int64_t &index2)
Definition: RubiksCubeCorners.h:151
RubiksCorner::GetStateHash
uint64_t GetStateHash(const RubiksCornerState &node) const
Definition: RubiksCubeCorners.cpp:586
RubiksCornerStateBits::SetCubeOrientation
void SetCubeOrientation(unsigned int whichLoc, int orient)
Definition: RubiksCubeCorners.cpp:170
RubiksCorner::HCost
virtual double HCost(const RubiksCornerState &node) const
Heuristic value between node and the stored goal.
Definition: RubiksCubeCorners.h:164
RubiksCorner::HCost
virtual double HCost(const RubiksCornerState &node1, const RubiksCornerState &node2) const
Heuristic value between two arbitrary nodes.
Definition: RubiksCubeCorners.h:160
RubiksCornerStateArray::Rotate
void Rotate(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
Definition: RubiksCubeCorners.cpp:266
RubikCornerMove::act
RubiksCornersAction act
Definition: RubiksCubeCorners.h:109
RubiksCornerStateBits::SetCubeInLoc
void SetCubeInLoc(unsigned int whichLoc, int cube)
Definition: RubiksCubeCorners.cpp:155
RubiksCorner::GetSuccessors
virtual void GetSuccessors(const RubiksCornerState &nodeID, std::vector< RubiksCornerState > &neighbors) const
Definition: RubiksCubeCorners.cpp:289
RubiksCornerStateBits::Swap
void Swap(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
Definition: RubiksCubeCorners.cpp:207
RubiksCornerStateArray::SetCubeInLoc
void SetCubeInLoc(unsigned int whichLoc, int cube)
Definition: RubiksCubeCorners.cpp:243
RubikCornerPDB::GetStateSpaceSize
static uint64_t GetStateSpaceSize()
Definition: RubiksCubeCorners.cpp:1189
RubiksCornerStateArray::RubiksCornerStateArray
RubiksCornerStateArray()
Definition: RubiksCubeCorners.cpp:225
RubikCornerPDB::Load
virtual bool Load(const char *prefix)
Definition: RubiksCubeCorners.cpp:1365
RubiksCorner::ApplyAction
virtual void ApplyAction(RubiksCornerState &s, RubiksCornersAction a) const
Definition: RubiksCubeCorners.cpp:315
RubikCornerMove
Definition: RubiksCubeCorners.h:107
PDBHeuristic
Definition: PDBHeuristic.h:39
RubikCornerPDB::Save
virtual void Save(const char *prefix)
Definition: RubiksCubeCorners.cpp:1378
operator<<
static std::ostream & operator<<(std::ostream &out, const RubiksCornerState &s)
Definition: RubiksCubeCorners.h:96
RubiksCornerStateArray::GetFaceInLoc
int GetFaceInLoc(unsigned int whichLoc) const
Definition: RubiksCubeCorners.cpp:260
RubiksCornerStateArray::Swap
void Swap(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
Definition: RubiksCubeCorners.cpp:275
RubiksCorner::~RubiksCorner
~RubiksCorner()
Definition: RubiksCubeCorners.h:127
MR1KPermutation
Definition: MR1Permutation.h:16
RubikCornerMove::next
RubikCornerMove * next
Definition: RubiksCubeCorners.h:110
RubiksCorner::GetActionHash
virtual uint64_t GetActionHash(RubiksCornersAction act) const
Definition: RubiksCubeCorners.h:177
RubikCornerPDB::GetPDBSize
uint64_t GetPDBSize() const
Definition: RubiksCubeCorners.cpp:1207
RubikCornerPDB::mr1
MR1KPermutation mr1
Definition: RubiksCubeCorners.h:217
RubiksCorner::OpenGLDraw
virtual void OpenGLDraw() const
Definition: RubiksCubeCorners.cpp:741
RubiksCornerStateArray::GetCubeInLoc
int GetCubeInLoc(unsigned int whichLoc) const
Definition: RubiksCubeCorners.cpp:238
RubiksCornerStateBits::RubiksCornerStateBits
RubiksCornerStateBits()
Definition: RubiksCubeCorners.cpp:137
RubikCornerPDB::GetStateHash
static uint64_t GetStateHash(const RubiksCornerState &s)
Definition: RubiksCubeCorners.cpp:1195
RubiksCorner::rankPlayer
int64_t rankPlayer(RubiksCornerState &s, int who)
Definition: RubiksCubeCorners.h:147
RubiksCorner::SetFaceColor
void SetFaceColor(int face, const RubiksCornerState &) const
Definition: RubiksCubeCorners.cpp:1130
RubiksCorner::rankPlayerFirstTwo
void rankPlayerFirstTwo(const RubiksCornerState &s, int who, int64_t &rank)
Definition: RubiksCubeCorners.cpp:531
RubiksCornerStateArray::SetCubeOrientation
void SetCubeOrientation(unsigned int whichLoc, int orient)
Definition: RubiksCubeCorners.cpp:254
RubiksCornerStateBits
Definition: RubiksCubeCorners.h:19
RubiksCornerStateArray::Reset
void Reset()
Definition: RubiksCubeCorners.cpp:229
RubiksCorner::getMaxSinglePlayerRank2
int64_t getMaxSinglePlayerRank2()
Definition: RubiksCubeCorners.cpp:522
std
Definition: CanonicalGraphEnvironment.h:26
RubikCornerPDB::GetStateFromHash
static void GetStateFromHash(RubiksCornerState &s, uint64_t hash)
Definition: RubiksCubeCorners.cpp:1201
std::hash< RubiksCornerStateArray >::operator()
size_t operator()(const RubiksCornerStateArray &s) const
Definition: RubiksCubeCorners.h:71
RubiksCorner::RubiksCorner
RubiksCorner()
Definition: RubiksCubeCorners.h:117
RubikCornerPDB::corners
std::vector< int > corners
Definition: RubiksCubeCorners.h:214
RubiksCorner::GetActions
virtual void GetActions(const RubiksCornerState &nodeID, std::vector< RubiksCornersAction > &actions) const
Definition: RubiksCubeCorners.cpp:299
RubiksCorner::MRRank
static uint64_t MRRank(int n, uint64_t perm, uint64_t dual)
Definition: RubiksCubeCorners.cpp:640
MR1Permutation.h
RubiksCornerStateBits::GetCubeInLoc
int GetCubeInLoc(unsigned int whichLoc) const
Definition: RubiksCubeCorners.cpp:150
RubiksCorner::ApplyMove
void ApplyMove(RubiksCornerState &s, RubikCornerMove *a)
Definition: RubiksCubeCorners.cpp:494
RubiksCorner::GetStateFromHash
void GetStateFromHash(uint64_t hash, RubiksCornerState &node) const
Definition: RubiksCubeCorners.cpp:670
RubiksCorner::GetAction
virtual RubiksCornersAction GetAction(const RubiksCornerState &s1, const RubiksCornerState &s2) const
Definition: RubiksCubeCorners.cpp:308
PDBHeuristic.h
RubiksCorner::GoalTest
virtual bool GoalTest(const RubiksCornerState &node, const RubiksCornerState &goal) const
Definition: RubiksCubeCorners.h:169
RubiksCornerStateBits::Reset
void Reset()
Definition: RubiksCubeCorners.cpp:141
RubiksCorner::getMaxSinglePlayerRank
int64_t getMaxSinglePlayerRank()
Definition: RubiksCubeCorners.cpp:512
RubiksCorner::GetNextState
virtual void GetNextState(const RubiksCornerState &, RubiksCornersAction, RubiksCornerState &) const
Definition: RubiksCubeCorners.cpp:460
RubiksCorner
Definition: RubiksCubeCorners.h:114
RubiksCornerState
RubiksCornerStateArray RubiksCornerState
Definition: RubiksCubeCorners.h:58
RubikCornerPDB::RubikCornerPDB
RubikCornerPDB(RubiksCorner *e, const RubiksCornerState &s, std::vector< int > &distinctCorners)
Definition: RubiksCubeCorners.cpp:1183
RubikCornerPDB::FactorialUpperK
uint64_t FactorialUpperK(int n, int k) const
Definition: RubiksCubeCorners.cpp:1461
RubiksCorner::GCost
virtual double GCost(const RubiksCornerState &node1, const RubiksCornerState &node2) const
Definition: RubiksCubeCorners.h:167
SearchEnvironment
Definition: SearchEnvironment.h:30
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h
RubiksCorner::unrankPlayer
void unrankPlayer(uint64_t d, RubiksCornerState &s, int who)
Definition: RubiksCubeCorners.h:135
RubiksCorner::getMoves
RubikCornerMove * getMoves(RubiksCornerState &)
Definition: RubiksCubeCorners.h:144
RubiksCorner::freeMove
void freeMove(RubikCornerMove *m)
Definition: RubiksCubeCorners.h:145