-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrubik_state.py
254 lines (217 loc) · 11.5 KB
/
rubik_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from __future__ import annotations
import copy
import re
from random import randint
from typing import List, Dict
import numpy as np
import pandas as pd
class RubikState:
cube_size = 3
num_cube_faces = 6
clockwise = (1, 0)
solved_properties = {'corner_position': list(range(8)),
'edge_position': list(range(12)),
'corner_orientation': [0] * 8,
'edge_orientation': [0] * 12}
solved_faces = 'wwwwwwwwwgggggggggrrrrrrrrrbbbbbbbbboooooooooyyyyyyyyy'
possible_notations = ('L', 'L2', 'L\'', 'R', 'R2', 'R\'',
'F', 'F2', 'F\'', 'B', 'B2', 'B\'',
'U', 'U2', 'U\'', 'D', 'D2', 'D\'')
def __init__(self, properties: Dict[str, List[int]] | None,
faces: str | None, notation: str | List[str], notation_path: List[str] | None):
self.properties = RubikState.solved_properties if properties is None else copy.deepcopy(properties)
self.cp = self.properties['corner_position']
self.ep = self.properties['edge_position']
self.co = self.properties['corner_orientation']
self.eo = self.properties['edge_orientation']
self.faces = RubikState.solved_faces if faces is None else faces
self.top, self.left, self.front, \
self.right, self.back, self.bottom = \
[np.array(list(self.faces[RubikState.cube_size ** 2 * i:RubikState.cube_size ** 2 * (i + 1)]))
.reshape(RubikState.cube_size, RubikState.cube_size) for i in range(RubikState.num_cube_faces)]
self.notation = notation
self.notation_path = list() if notation_path is None else notation_path
if self.notation:
self.make_move(self.notation)
self.notation_path.append(self.notation)
def make_move(self, move: str | List[str]) -> None:
"""
in the first block of each notation, all permutations of cubies are made according
to changes in positions and orientations.
in the first block, the first line is for positions, the second line is for orientations
in the second block, the movements occur according to facelet-colors
"""
def l_clockwise() -> None:
self.cp[0], self.cp[4], self.cp[1], self.cp[5] = \
self.cp[5], self.cp[0], self.cp[4], self.cp[1]
self.ep[4], self.ep[0], self.ep[5], self.ep[1] = \
self.ep[1], self.ep[4], self.ep[0], self.ep[5]
self.left = np.rot90(self.left, axes=RubikState.clockwise)
self.front[:, [0]], self.top[:, [0]], \
self.back[:, [2]], self.bottom[:, [0]] \
= self.top[:, [0]], self.back[:, [2]][::-1], \
self.bottom[:, [0]][::-1], self.front[:, [0]]
def r_clockwise() -> None:
self.cp[3], self.cp[7], self.cp[2], self.cp[6] = \
self.cp[6], self.cp[3], self.cp[7], self.cp[2]
self.ep[6], self.ep[3], self.ep[7], self.ep[2] = \
self.ep[2], self.ep[6], self.ep[3], self.ep[7]
self.right = np.rot90(self.right, axes=RubikState.clockwise)
self.front[:, [2]], self.top[:, [2]], \
self.back[:, [0]], self.bottom[:, [2]] \
= self.bottom[:, [2]], self.front[:, [2]], \
self.top[:, [2]][::-1], self.back[:, [0]][::-1]
def f_clockwise() -> None:
self.cp[4], self.cp[3], self.cp[6], self.cp[1] = \
self.cp[1], self.cp[4], self.cp[3], self.cp[6]
self.ep[5], self.ep[8], self.ep[6], self.ep[9] = \
self.ep[9], self.ep[5], self.ep[8], self.ep[6]
self.co[self.cp[1]] = RubikState.calculate_new_co(self.co[self.cp[1]] + 1)
self.co[self.cp[3]] = RubikState.calculate_new_co(self.co[self.cp[3]] + 1)
self.co[self.cp[4]] = RubikState.calculate_new_co(self.co[self.cp[4]] - 1)
self.co[self.cp[6]] = RubikState.calculate_new_co(self.co[self.cp[6]] - 1)
self.front = np.rot90(self.front, axes=RubikState.clockwise)
self.top[[2]], self.right[:, [0]], \
self.bottom[[0]], self.left[:, [2]] \
= np.rot90(self.left[:, [2]][::-1]), \
np.rot90(self.top[[2]])[::-1], \
np.rot90(self.right[:, [0]][::-1]), \
np.rot90(self.bottom[[0]])[::-1]
def b_clockwise() -> None:
self.cp[7], self.cp[0], self.cp[5], self.cp[2] = \
self.cp[2], self.cp[7], self.cp[0], self.cp[5]
self.ep[7], self.ep[11], self.ep[4], self.ep[10] = \
self.ep[10], self.ep[7], self.ep[11], self.ep[4]
self.co[self.cp[0]] = RubikState.calculate_new_co(self.co[self.cp[0]] + 1)
self.co[self.cp[2]] = RubikState.calculate_new_co(self.co[self.cp[2]] + 1)
self.co[self.cp[5]] = RubikState.calculate_new_co(self.co[self.cp[5]] - 1)
self.co[self.cp[7]] = RubikState.calculate_new_co(self.co[self.cp[7]] - 1)
self.back = np.rot90(self.back, axes=RubikState.clockwise)
self.top[[0]], self.right[:, [2]], \
self.bottom[[2]], self.left[:, [0]] \
= np.rot90(self.right[:, [2]]), \
np.rot90(self.bottom[[2]]), \
np.rot90(self.left[:, [0]]), \
np.rot90(self.top[[0]])
def u_clockwise() -> None:
self.cp[0], self.cp[7], self.cp[3], self.cp[4] = \
self.cp[4], self.cp[0], self.cp[7], self.cp[3]
self.ep[0], self.ep[11], self.ep[3], self.ep[8] = \
self.ep[8], self.ep[0], self.ep[11], self.ep[3]
self.co[self.cp[0]] = RubikState.calculate_new_co(self.co[self.cp[0]] - 1)
self.co[self.cp[3]] = RubikState.calculate_new_co(self.co[self.cp[3]] - 1)
self.co[self.cp[4]] = RubikState.calculate_new_co(self.co[self.cp[4]] + 1)
self.co[self.cp[7]] = RubikState.calculate_new_co(self.co[self.cp[7]] + 1)
self.eo[self.ep[0]] = (self.eo[self.ep[0]] + 1) % 2
self.eo[self.ep[3]] = (self.eo[self.ep[3]] + 1) % 2
self.eo[self.ep[8]] = (self.eo[self.ep[8]] + 1) % 2
self.eo[self.ep[11]] = (self.eo[self.ep[11]] + 1) % 2
self.top = np.rot90(self.top, axes=RubikState.clockwise)
self.front[[0]], self.right[[0]], \
self.back[[0]], self.left[[0]] \
= self.right[[0]], self.back[[0]], \
self.left[[0]], self.front[[0]]
def d_clockwise() -> None:
self.cp[1], self.cp[6], self.cp[2], self.cp[5] = \
self.cp[5], self.cp[1], self.cp[6], self.cp[2]
self.ep[1], self.ep[9], self.ep[2], self.ep[10] = \
self.ep[10], self.ep[1], self.ep[9], self.ep[2]
self.co[self.cp[1]] = RubikState.calculate_new_co(self.co[self.cp[1]] - 1)
self.co[self.cp[2]] = RubikState.calculate_new_co(self.co[self.cp[2]] - 1)
self.co[self.cp[5]] = RubikState.calculate_new_co(self.co[self.cp[5]] + 1)
self.co[self.cp[6]] = RubikState.calculate_new_co(self.co[self.cp[6]] + 1)
self.eo[self.ep[1]] = (self.eo[self.ep[1]] + 1) % 2
self.eo[self.ep[2]] = (self.eo[self.ep[2]] + 1) % 2
self.eo[self.ep[9]] = (self.eo[self.ep[9]] + 1) % 2
self.eo[self.ep[10]] = (self.eo[self.ep[10]] + 1) % 2
self.bottom = np.rot90(self.bottom, axes=RubikState.clockwise)
self.front[[2]], self.right[[2]], \
self.back[[2]], self.left[[2]] \
= self.left[[2]], self.front[[2]], \
self.right[[2]], self.back[[2]]
if isinstance(move, list):
self.notation_path += move
for mv in move:
self.make_move(mv)
match move:
case 'L': l_clockwise()
case 'L2': [l_clockwise() for _ in range(2)]
case 'L\'': [l_clockwise() for _ in range(3)]
case 'R': r_clockwise()
case 'R2': [r_clockwise() for _ in range(2)]
case 'R\'': [r_clockwise() for _ in range(3)]
case 'F': f_clockwise()
case 'F2': [f_clockwise() for _ in range(2)]
case 'F\'': [f_clockwise() for _ in range(3)]
case 'B': b_clockwise()
case 'B2': [b_clockwise() for _ in range(2)]
case 'B\'': [b_clockwise() for _ in range(3)]
case 'U': u_clockwise()
case 'U2': [u_clockwise() for _ in range(2)]
case 'U\'': [u_clockwise() for _ in range(3)]
case 'D': d_clockwise()
case 'D2': [d_clockwise() for _ in range(2)]
case 'D\'': [d_clockwise() for _ in range(3)]
@property
def f_cost(self) -> int:
"""
heuristic function formula is f(n) = g(n) + h(n).
g(n) is the cost of reaching from the initial state to the current
one uses the length of the path of the notations.
"""
return len(self.notation_path) + self.h_cost
@property
def h_cost(self) -> int:
"""
estimation of the weights of the rubik's state.
4 edge positions and 4 edge orientations.
▢ 11 ▢
0 ▢ 3
▢ 8 ▢
"""
top_layer_cross_heuristic = 0
for i in [0, 11, 3, 8]:
if self.ep[i] == i:
top_layer_cross_heuristic += 1
if self.eo[i] == 0:
top_layer_cross_heuristic += 1
return 8 - top_layer_cross_heuristic
def __lt__(self, other: RubikState) -> bool:
return self.f_cost < other.f_cost
def is_target_state(self) -> bool:
return self.h_cost == 0
def make_line_state(self) -> str:
make_one_line = lambda matrix: str(matrix.tolist())
single_array = map(make_one_line, [
self.top, self.left, self.front, self.right, self.back, self.bottom])
return ''.join(re.findall(r'[a-z]', str(list(single_array))))
def __str__(self) -> str:
def sum_lines_np(*matrices: np.ndarray) -> str:
final_str = ''
nums = len(matrices)
for i in range(RubikState.cube_size):
for num in range(nums):
final_str += matrices[num][i].__str__()
final_str += ' ' if num < nums - 1 else ''
final_str += '\n' if i < nums else ''
final_str = final_str.replace('][', ' ')
final_str = re.sub(r'[\[\]\']', '', final_str)
return final_str
empty_np = np.full((RubikState.cube_size, RubikState.cube_size), ' ')
scheme = sum_lines_np(self.left, self.front, self.right, self.back)
scheme = sum_lines_np(empty_np, self.top) + '\n' + scheme + sum_lines_np(empty_np, self.bottom)
str_dict = {k: ' '.join(map(str, v)) for k, v in self.properties.items()}
data = {'position': [str_dict['corner_position'], str_dict['edge_position']],
'orientation': [str_dict['corner_orientation'], str_dict['edge_orientation']]}
df = pd.DataFrame.from_dict(data, orient='index').rename(columns={0: 'corner', 1: 'edge'})
return scheme + '\n\n' + self.make_line_state() + '\n\n' + df.to_string()
@staticmethod
def make_random_notations(num: int) -> List[str]:
random_notations = list()
for _ in range(num):
notation = randint(0, 17)
random_notations.append(RubikState.possible_notations[notation])
return random_notations
@staticmethod
def calculate_new_co(corner_orientation: int) -> int:
return 2 if corner_orientation == -1 else corner_orientation % 3