HOG2
RubiksCube.h
Go to the documentation of this file.
1 //
2 // RubiksCube.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 4/6/13.
6 // Copyright (c) 2013 University of Denver. All rights reserved.
7 //
8 
9 #ifndef __hog2_glut__RubiksCube__
10 #define __hog2_glut__RubiksCube__
11 
12 #include <iostream>
13 #include <stdint.h>
14 #include <unordered_map>
15 #include <vector>
16 #include "RubiksCubeCorners.h"
17 #include "RubiksCubeEdges.h"
18 #include "SearchEnvironment.h"
19 #include "RubiksCube7Edges.h"
20 #include "FourBitArray.h"
21 #include "DiskBitFile.h"
22 #include "EnvUtil.h"
23 #include "Bloom.h"
24 #include "MinBloom.h"
25 #include "PDBHeuristic.h"
26 
28 {
29 public:
31  {
32  Reset();
33  }
34  void Reset()
35  {
36  corner.Reset();
37  edge.Reset();
38  //edge7.Reset();
39  }
42 // Rubik7EdgeState edge7;
43 };
44 
45 namespace std {
46  template <> struct hash<RubiksState>
47  {
48  size_t operator()(const RubiksState & x) const
49  {
50  return std::hash<RubikEdgeState>{}(x.edge)^(std::hash<RubiksCornerState>{}(x.corner)<<2);
51  }
52  };
53 }
54 
55 static bool operator==(const RubiksState &l1, const RubiksState &l2)
56 {
57 // return (l1.corner.state == l2.corner.state) && (l1.edge.state == l2.edge.state);
58  return (l1.corner == l2.corner) && (l1.edge == l2.edge);
59 }
60 
61 static bool operator!=(const RubiksState &l1, const RubiksState &l2)
62 {
63  return !(l1 == l2);
64 }
65 
66 static std::ostream &operator<<(std::ostream &out, const RubiksState &s)
67 {
68  out << "{" << s.edge << ", " << s.corner << "}";
69  return out;
70 }
71 
72 typedef int RubiksAction;
73 
74 //static std::ostream &operator<<(std::ostream &out, const RubiksAction &s)
75 //{
76 // switch (s) {
77 // case 0: out << "U "; break;
78 // case 1: out << "U- "; break;
79 // case 2: out << "U2 "; break;
80 // case 3: out << "D "; break;
81 // case 4: out << "D- "; break;
82 // case 5: out << "D2 "; break;
83 // case 6: out << "F "; break;
84 // case 7: out << "F- "; break;
85 // case 8: out << "F2 "; break;
86 // case 9: out << "B "; break;
87 // case 10: out << "B- "; break;
88 // case 11: out << "B2 "; break;
89 // case 12: out << "L "; break;
90 // case 13: out << "L- "; break;
91 // case 14: out << "L2 "; break;
92 // case 15: out << "R "; break;
93 // case 16: out << "R- "; break;
94 // case 17: out << "R2 "; break;
95 // }
96 // return out;
97 //}
98 
99 //class RubikCornerMove {
100 //public:
101 // RubiksCornersAction act;
102 // RubikCornerMove *next;
103 // int length() { if (next == 0) return 1; return 1+next->length(); }
104 //};
105 
106 class RubiksCube : public SearchEnvironment<RubiksState, RubiksAction>
107 {
108 public:
110 //:f("/home/sturtevant/sturtevant/code/cc/rubik/RC-10edge")
111 //:f("/data/cc/rubik/10/RC-10edge")
112 //:f("/store/rubik/RC")
113  {
114  f = 0;
115  pruneSuccessors = false;
116  minCompression = true;
117  bloomFilter = false;
118  uint64_t maxBuckSize = GetMaxBucketSize<RubikEdge, RubikEdgeState>(false);
119  InitTwoPieceData<RubikEdge, RubikEdgeState>(data, maxBuckSize);
120  InitBucketSize<RubikEdge, RubikEdgeState>(buckets, maxBuckSize);
121 
122  edgeDist.resize(16);
123  cornDist.resize(16);
124  depth8 = 0;
125  depth9 = 0;
126  // for (int x = 0; x < 18; x++)
127 // {
128 // moves[x].act = x;
129 // if (x != 17)
130 // moves[x].next = &moves[x+1];
131 // } moves[17].next = 0;
132  }
133  ~RubiksCube() { /*delete depth8; delete depth9;*/ }
134  std::string GetName() { return "Rubiks Cube"; }
135  void SetPruneSuccessors(bool val) { pruneSuccessors = val; history.resize(0); }
136  virtual void GetSuccessors(const RubiksState &nodeID, std::vector<RubiksState> &neighbors) const;
137  virtual void GetActions(const RubiksState &nodeID, std::vector<RubiksAction> &actions) const;
138  virtual void GetPrunedActions(const RubiksState &nodeID, RubiksAction lastAction, std::vector<RubiksAction> &actions) const;
139  virtual RubiksAction GetAction(const RubiksState &s1, const RubiksState &s2) const;
140  virtual void ApplyAction(RubiksState &s, RubiksAction a) const;
141  virtual void UndoAction(RubiksState &s, RubiksAction a) const;
142 
143  virtual void GetNextState(const RubiksState &, RubiksAction , RubiksState &) const;
144 
145  virtual bool InvertAction(RubiksAction &a) const;
146 
148  virtual double HCost(const RubiksState &node1, const RubiksState &node2) const;
149  virtual double HCost(const RubiksState &node1, const RubiksState &node2, double parentHCost) const;
150  int Edge12PDBDist(const RubiksState &s);
151 
154  virtual double HCost(const RubiksState &node) const;
155 
156  virtual double GCost(const RubiksState &node1, const RubiksState &node2) const { return 1.0; }
157  virtual double GCost(const RubiksState &node, const RubiksAction &act) const { return 1.0; }
158  virtual bool GoalTest(const RubiksState &node, const RubiksState &goal) const;
159 
161  virtual bool GoalTest(const RubiksState &node) const;
162 
163  virtual uint64_t GetStateHash(const RubiksState &node) const;
164  virtual uint64_t GetCornerHash(const RubiksState &node) const;
165  virtual uint64_t GetEdgeHash(const RubiksState &node) const;
166 
167  virtual uint64_t GetActionHash(RubiksAction act) const { return act; }
168  virtual void GetStateFromHash(uint64_t hash, RubiksState &node) const;
169  virtual void GetStateFromHash(uint64_t cornerHash, uint64_t edgeHash, RubiksState &node) const;
170 
171 // FourBitArray &GetCornerPDB() { return cornerPDB; }
172 // FourBitArray &GetEdgePDB() { return edgePDB; }
173  //FourBitArray &GetEdge7PDB(bool min) { if (min) return edge7PDBmin; return edge7PDBint; }
174 
175  virtual void OpenGLDraw() const;
176  virtual void OpenGLDraw(const RubiksState&) const;
177  virtual void OpenGLDrawCorners(const RubiksState&) const;
178  virtual void OpenGLDrawEdges(const RubiksState&) const;
179  virtual void OpenGLDrawEdgeDual(const RubiksState&) const;
180  virtual void OpenGLDrawCenters() const;
181  virtual void OpenGLDrawCubeBackground() const;
183  virtual void OpenGLDraw(const RubiksState&, const RubiksState&, float) const;
184  virtual void OpenGLDraw(const RubiksState&, const RubiksAction&) const;
185 
186  //int64_t percentage;
191  std::vector<uint64_t> edgeDist;
192  std::vector<uint64_t> cornDist;
193  std::unordered_map<uint64_t, uint8_t> depthTable;
196 //private:
197  void OpenGLDrawCube(int cube) const;
198  void SetFaceColor(int face) const;
199  mutable std::vector<RubiksAction> history;
205 // FourBitArray cornerPDB;
206 // FourBitArray edgePDB;
207  //FourBitArray edge7PDBmin;
208  //FourBitArray edge7PDBint;
209 
210 
212  std::vector<bucketInfo> data;
213  std::vector<bucketData> buckets;
214 
216 };
217 
218 
219 class RubikPDB : public PDBHeuristic<RubiksState, RubiksAction, RubiksCube, RubiksState, 4> {
220 public:
221  RubikPDB(RubiksCube *e, const RubiksState &s, std::vector<int> distinctEdges, std::vector<int> distinctCorners);
222  uint64_t GetStateHash(const RubiksState &s) const;
223  void GetStateFromHash(RubiksState &s, uint64_t hash) const;
224 
225  uint64_t GetPDBSize() const;
226 
227  uint64_t GetPDBHash(const RubiksState &s, int threadID = 0) const;
228  virtual uint64_t GetAbstractHash(const RubiksState &s, int threadID = 0) const { return GetPDBHash(s); }
229  void GetStateFromPDBHash(uint64_t hash, RubiksState &s, int threadID = 0) const;
231 
232  void OpenGLDraw() const
233  {
234  RubiksState s;
235  GetStateFromPDBHash(0, s);
236  env->OpenGLDraw(s);
237  }
238 
239  // const char *GetName();
240  bool Load(const char *prefix);
241  void Save(const char *prefix);
242  bool Load(FILE *f);
243  void Save(FILE *f);
244  std::string GetFileName(const char *prefix);
245 private:
248  std::vector<int> edges;
249  std::vector<int> corners;
250 };
251 
252 class RubikDualPDB : public Heuristic<RubiksState> {
253 public:
255  virtual double HCost(const RubiksState &a, const RubiksState &b) const;
256 private:
258 };
259 
260 class RubikArbitraryGoalPDB : public Heuristic<RubiksState> {
261 public:
263  virtual double HCost(const RubiksState &a, const RubiksState &b) const;
264 private:
266 };
267 
268 
269 #endif /* defined(__hog2_glut__RubiksCube__) */
RubiksCube::GoalTest
virtual bool GoalTest(const RubiksState &node, const RubiksState &goal) const
Definition: RubiksCube.cpp:397
operator!=
static bool operator!=(const RubiksState &l1, const RubiksState &l2)
Definition: RubiksCube.h:61
RubiksCornerStateArray
Definition: RubiksCubeCorners.h:38
RubiksCube::e7
Rubik7Edge e7
Definition: RubiksCube.h:204
Rubik7EdgeState
Definition: RubiksCube7Edges.h:19
RubikArbitraryGoalPDB::pdb
RubikPDB * pdb
Definition: RubiksCube.h:265
RubiksCube::GetCornerHash
virtual uint64_t GetCornerHash(const RubiksState &node) const
Definition: RubiksCube.cpp:418
RubiksState::RubiksState
RubiksState()
Definition: RubiksCube.h:30
RubiksCube::buckets
std::vector< bucketData > buckets
Definition: RubiksCube.h:213
RubiksCube::OpenGLDrawCube
void OpenGLDrawCube(int cube) const
Definition: RubiksCube.cpp:534
RubikArbitraryGoalPDB::RubikArbitraryGoalPDB
RubikArbitraryGoalPDB(RubikPDB *pdb)
Definition: RubiksCube.cpp:1032
Bloom.h
RubiksCube::minBloomFilter
bool minBloomFilter
Definition: RubiksCube.h:190
RubikDualPDB
Definition: RubiksCube.h:252
RubiksCube::data
std::vector< bucketInfo > data
Definition: RubiksCube.h:212
RubikArbitraryGoalPDB
Definition: RubiksCube.h:260
RubiksCube::c
RubiksCorner c
Definition: RubiksCube.h:200
RubiksCube
Definition: RubiksCube.h:106
Heuristic
Definition: Heuristic.h:30
RubiksCube::bloomFilter
bool bloomFilter
Definition: RubiksCube.h:189
operator==
static bool operator==(const RubiksState &l1, const RubiksState &l2)
Definition: RubiksCube.h:55
RubiksCube::OpenGLDrawCorners
virtual void OpenGLDrawCorners(const RubiksState &) const
Definition: RubiksCube.cpp:452
RubiksCube::GetNextState
virtual void GetNextState(const RubiksState &, RubiksAction, RubiksState &) const
Definition: RubiksCube.cpp:109
RubiksCube::UndoAction
virtual void UndoAction(RubiksState &s, RubiksAction a) const
Definition: RubiksCube.cpp:95
FourBitArray.h
RubiksCube::RubiksCube
RubiksCube()
Definition: RubiksCube.h:109
RubiksAction
int RubiksAction
Definition: RubiksCube.h:72
RubiksCube::GetName
std::string GetName()
Definition: RubiksCube.h:134
RubiksCube::depth8
BloomFilter * depth8
Definition: RubiksCube.h:194
RubiksCube::OpenGLDrawEdgeDual
virtual void OpenGLDrawEdgeDual(const RubiksState &) const
Definition: RubiksCube.cpp:468
RubiksCube::GetPrunedActions
virtual void GetPrunedActions(const RubiksState &nodeID, RubiksAction lastAction, std::vector< RubiksAction > &actions) const
Definition: RubiksCube.cpp:24
RubikCornerPDB
Definition: RubiksCubeCorners.h:194
RubikPDB::Save
void Save(const char *prefix)
Definition: RubiksCube.cpp:888
RubikDualPDB::HCost
virtual double HCost(const RubiksState &a, const RubiksState &b) const
Definition: RubiksCube.cpp:1001
RubiksCube::GetEdgeHash
virtual uint64_t GetEdgeHash(const RubiksState &node) const
Definition: RubiksCube.cpp:423
RubiksState::corner
RubiksCornerState corner
Definition: RubiksCube.h:40
RubikPDB::ePDB
RubikEdgePDB ePDB
Definition: RubiksCube.h:246
RubiksState::edge
RubikEdgeState edge
Definition: RubiksCube.h:41
RubiksCube::GCost
virtual double GCost(const RubiksState &node, const RubiksAction &act) const
Definition: RubiksCube.h:157
RubiksCube::edgeDist
std::vector< uint64_t > edgeDist
Definition: RubiksCube.h:191
RubikPDB::corners
std::vector< int > corners
Definition: RubiksCube.h:249
RubikPDB::RubikPDB
RubikPDB(RubiksCube *e, const RubiksState &s, std::vector< int > distinctEdges, std::vector< int > distinctCorners)
Definition: RubiksCube.cpp:824
RubikDualPDB::RubikDualPDB
RubikDualPDB(RubikPDB *pdb)
Definition: RubiksCube.cpp:995
RubikPDB::GetPDBHash
uint64_t GetPDBHash(const RubiksState &s, int threadID=0) const
Definition: RubiksCube.cpp:849
RubikEdgePDB
Definition: RubiksCubeEdges.h:202
RubikPDB::OpenGLDraw
void OpenGLDraw() const
Definition: RubiksCube.h:232
RubikPDB::GetAbstractHash
virtual uint64_t GetAbstractHash(const RubiksState &s, int threadID=0) const
Definition: RubiksCube.h:228
RubiksCube::history
std::vector< RubiksAction > history
Definition: RubiksCube.h:199
RubikEdge
Definition: RubiksCubeEdges.h:125
RubiksCube::minBloom
MinBloomFilter * minBloom
Definition: RubiksCube.h:195
RubiksCube::minCompression
bool minCompression
Definition: RubiksCube.h:188
RubikArbitraryGoalPDB::HCost
virtual double HCost(const RubiksState &a, const RubiksState &b) const
Definition: RubiksCube.cpp:1038
PDBHeuristic
Definition: PDBHeuristic.h:39
RubiksCube::f
DiskBitFile * f
Definition: RubiksCube.h:211
RubiksCube::dual
RubikEdgeState dual
Definition: RubiksCube.h:202
std::hash< RubiksState >::operator()
size_t operator()(const RubiksState &x) const
Definition: RubiksCube.h:48
RubikDualPDB::pdb
RubikPDB * pdb
Definition: RubiksCube.h:257
operator<<
static std::ostream & operator<<(std::ostream &out, const RubiksState &s)
Definition: RubiksCube.h:66
RubiksCube::SetPruneSuccessors
void SetPruneSuccessors(bool val)
Definition: RubiksCube.h:135
RubiksCube7Edges.h
DiskBitFile.h
RubiksCube::HCost
virtual double HCost(const RubiksState &node1, const RubiksState &node2) const
Heuristic value between two arbitrary nodes.
Definition: RubiksCube.cpp:290
BloomFilter
Definition: Bloom.h:16
RubiksCube::GetActionHash
virtual uint64_t GetActionHash(RubiksAction act) const
Definition: RubiksCube.h:167
RubiksCube::e7dual
Rubik7EdgeState e7dual
Definition: RubiksCube.h:203
RubiksState
Definition: RubiksCube.h:27
RubiksCubeEdges.h
RubiksCube::GetStateHash
virtual uint64_t GetStateHash(const RubiksState &node) const
Definition: RubiksCube.cpp:410
EnvUtil.h
RubiksCube::InvertAction
virtual bool InvertAction(RubiksAction &a) const
Definition: RubiksCube.cpp:115
PDBHeuristic< RubiksState, RubiksAction, RubiksCube, RubiksState, 4 >::env
RubiksCube * env
Definition: PDBHeuristic.h:128
MinBloom.h
RubiksCube::~RubiksCube
~RubiksCube()
Definition: RubiksCube.h:133
RubiksCube::OpenGLDrawCenters
virtual void OpenGLDrawCenters() const
Definition: RubiksCube.cpp:522
RubikEdgeStateArray
Definition: RubiksCubeEdges.h:37
RubikPDB::cPDB
RubikCornerPDB cPDB
Definition: RubiksCube.h:247
RubiksCube::GetAction
virtual RubiksAction GetAction(const RubiksState &s1, const RubiksState &s2) const
Definition: RubiksCube.cpp:71
RubiksCube::Edge12PDBDist
int Edge12PDBDist(const RubiksState &s)
Definition: RubiksCube.cpp:813
RubiksCube::e
RubikEdge e
Definition: RubiksCube.h:201
MinBloomFilter
Definition: MinBloom.h:15
RubikPDB::GetPDBSize
uint64_t GetPDBSize() const
Definition: RubiksCube.cpp:844
RubiksCube::GetStateFromHash
virtual void GetStateFromHash(uint64_t hash, RubiksState &node) const
Definition: RubiksCube.cpp:434
RubiksCornerStateArray::Reset
void Reset()
Definition: RubiksCubeCorners.cpp:229
RubiksCubeCorners.h
RubikPDB::edges
std::vector< int > edges
Definition: RubiksCube.h:248
std
Definition: CanonicalGraphEnvironment.h:26
RubikPDB::GetFileName
std::string GetFileName(const char *prefix)
Definition: RubiksCube.cpp:932
RubiksState::Reset
void Reset()
Definition: RubiksCube.h:34
RubiksCube::GetSuccessors
virtual void GetSuccessors(const RubiksState &nodeID, std::vector< RubiksState > &neighbors) const
Definition: RubiksCube.cpp:15
Rubik7Edge
Definition: RubiksCube7Edges.h:102
RubikPDB::GetStateHash
uint64_t GetStateHash(const RubiksState &s) const
Definition: RubiksCube.cpp:831
RubiksCube::GCost
virtual double GCost(const RubiksState &node1, const RubiksState &node2) const
Definition: RubiksCube.h:156
RubikPDB::Load
bool Load(const char *prefix)
Definition: RubiksCube.cpp:871
RubiksCube::depth9
BloomFilter * depth9
Definition: RubiksCube.h:194
RubiksCube::pruneSuccessors
bool pruneSuccessors
Definition: RubiksCube.h:215
RubiksCube::cornDist
std::vector< uint64_t > cornDist
Definition: RubiksCube.h:192
RubiksCube::OpenGLDraw
virtual void OpenGLDraw() const
Definition: RubiksCube.cpp:440
RubiksCube::depthTable
std::unordered_map< uint64_t, uint8_t > depthTable
Definition: RubiksCube.h:193
PDBHeuristic.h
RubiksCube::ApplyAction
virtual void ApplyAction(RubiksState &s, RubiksAction a) const
Definition: RubiksCube.cpp:86
RubikPDB::GetStateFromPDBHash
void GetStateFromPDBHash(uint64_t hash, RubiksState &s, int threadID=0) const
Definition: RubiksCube.cpp:854
RubiksCube::SetFaceColor
void SetFaceColor(int face) const
Definition: RubiksCube.cpp:633
RubikPDB::GetStateFromHash
void GetStateFromHash(RubiksState &s, uint64_t hash) const
Definition: RubiksCube.cpp:837
RubiksCorner
Definition: RubiksCubeCorners.h:114
DiskBitFile
Definition: DiskBitFile.h:23
RubikPDB
Definition: RubiksCube.h:219
SearchEnvironment
Definition: SearchEnvironment.h:30
RubiksCube::OpenGLDrawCubeBackground
virtual void OpenGLDrawCubeBackground() const
Definition: RubiksCube.cpp:476
RubikPDB::GetStateFromAbstractState
RubiksState GetStateFromAbstractState(RubiksState &s) const
Definition: RubiksCube.h:230
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h
RubiksCube::compressionFactor
int compressionFactor
Definition: RubiksCube.h:187
RubiksCube::GetActions
virtual void GetActions(const RubiksState &nodeID, std::vector< RubiksAction > &actions) const
Definition: RubiksCube.cpp:43
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129
RubiksCube::OpenGLDrawEdges
virtual void OpenGLDrawEdges(const RubiksState &) const
Definition: RubiksCube.cpp:459