Repository navigation
Expand file tree
/
Copy patheigencube.py
More file actions
384 lines (330 loc) · 14.2 KB
/
Copy patheigencube.py
File metadata and controls
384 lines (330 loc) · 14.2 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
""" A minimalstic Rubik's cube solver.
We model a Rubik's cube as a discrete object in 3-dimensional Euclidean space,
centered at the origin (0, 0, 0).
Each *cubelet* has coordinates (x, y, z) in {-1, 0, 1}^3.
Each *move* is a 90 degree hyperplane rotation, with the hyperplane given
by a standard unit vector or its opposite.
"""
import numpy as np
import heapq
import random
import functools
from datetime import datetime
from dataclasses import dataclass, field
from typing import Any
# Whether or not to use randomization during search.
RANDOMIZE_SEARCH = True
# Remember start up time for stats.
STARTUP_TIME = datetime.now()
# Redefine print to include timestamps.
_print = print
def print(*args, **kw):
_print("[%s]" % (datetime.now().strftime('%H:%M:%S')), *args, **kw)
# Returns the 1-norm of a vector.
def norm1(v): return abs(v[0]) + abs(v[1]) + abs(v[2])
# Returns a 2-dimensional matrix as a tuple.
def tupled(np_mat): return tuple(tuple(int(x) for x in row) for row in np_mat)
# We are interested in x, y, z coordinates ranging over {-1, 0, 1}.
crange = [-1, 0, 1]
vectors = tuple((x,y,z) for x in crange for y in crange for z in crange)
unit_vectors = [v for v in vectors if norm1(v) == 1]
# A cube is encoded as a finite map from cubelets to 3-by-3 rotation matrices
# `r`, where each cubelet is encoded as the 3D vector `c` that indicates the
# position of the cubelet in the solved cube.
# The cubelets current position is given by the matrix-vector product `c*r`.
solved_cube = tuple((c, tupled(np.identity(3))) for c in vectors if any(c))
NUM_CUBELETS = len(solved_cube)
# A move is a 90 degree rotation of the slice pointed at by a unit vector,
# clockwise (direction 1) or counterclockwise (-1) as seen looking at that face.
moves = [(v, direction) for v in unit_vectors for direction in [-1, 1]]
# Each color is encoded by the unit vector corresponding to the direction that
# faces of that color point to in a solved cube.
color_names = {
(+1, 0, 0): "GREEN", # front
(0, +1, 0): "RED", # right
(0, 0, +1): "WHITE", # top
(-1, 0, 0): "BLUE", # back
(0, -1, 0): "ORANGE", # left
(0, 0, -1): "YELLOW", # bottom
}
assert all(v in color_names for v in unit_vectors)
cubelet_types = ("hidden", "center", "edge", "corner")
def describe_cubelet_type(cubelet): return cubelet_types[norm1(cubelet)]
def describe_vector(vector):
x, y, z = vector
descriptions = []
descriptions += ["top"] if z == 1 else ["bottom"] if z == -1 else []
descriptions += ["right"] if y == 1 else ["left"] if y == -1 else []
descriptions += ["front"] if x == 1 else ["back"] if x == -1 else []
assert descriptions
return "-".join(descriptions)
def describe_config(cubelet, rotation):
colors = np.diag(cubelet)
sticker_directions = rotation @ colors
return ", ".join(sorted(
describe_vector(direction) + ": " + color_names[tuple(color)]
for color, direction in zip(colors.T, sticker_directions.T)
if any(color)
))
@functools.cache
def position(cubelet, rotation): return tuple(np.matmul(rotation, cubelet))
def describe_cubelet(cubelet, rotation):
return "%s %s: %s" % (
describe_cubelet_type(cubelet),
describe_vector(position(cubelet, rotation)),
describe_config(cubelet, rotation),
)
def describe_cube(cube):
return "\n".join(sorted(describe_cubelet(*c) for c in cube))
def describe_move(move):
v, direction = move
return "%s rotation of %s slice" % (
"clockwise" if direction == 1 else "counterclockwise",
describe_vector(v),
)
def inverse_move(move):
v, direction = move
return (v, -direction)
@functools.cache
def rotation_matrix(move):
v, direction = move
# A quarter turn about the axis `v`, clockwise as seen looking at the face `v`
# points to (direction 1) or counterclockwise (direction -1). It keeps the part
# of a vector along `v` (the projection v vᵀ) and turns the rest a quarter turn,
# which is the cross product with `v`.
# https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
x, y, z = v
cross = np.array([[0, -z, y], [z, 0, -x], [-y, x, 0]]) # cross @ p = v × p
return np.outer(v, v) - direction * cross
@functools.cache
def apply_move_to_cubelet_rotation(move, cubelet, rotation):
v, direction = move
move_applies = np.dot(v, position(cubelet, rotation)) > 0
return tupled(rotation_matrix(move) @ rotation) if move_applies else rotation
def apply_move_to_cube(move, cube):
return tuple(
(cubelet, apply_move_to_cubelet_rotation(move, cubelet, rotation))
for cubelet, rotation in cube
)
def shuffle(cube, iterations=100_000, seed=42):
if seed: random.seed(seed)
for _ in range(iterations):
move = moves[random.randrange(len(moves))]
cube = apply_move_to_cube(move, cube)
return cube
def run_tests():
# Check that every move is a permutation with cycle length 4.
for move in moves:
cubes = [solved_cube]
for _ in range(3): cubes.append(apply_move_to_cube(move, cubes[-1]))
assert len(set(cubes)) == 4
assert apply_move_to_cube(move, cubes[-1]) == cubes[0]
@dataclass(order=True, frozen=True)
class PrioritizedItem:
item: Any = field(compare=False)
priority: int
def astar(start, is_goal, apply_move, heuristic = lambda _: 0,
get_moves = lambda _: moves, random_weight=0, max_moves=100_000):
if is_goal(start): return (start, ())
budget = max_moves
def reconstruct_solution(dst):
path, current = [], dst
while current in came_from:
src, move = came_from[current]
path.append(move)
current = src
return (dst, tuple(reversed(path)))
while True:
frontier = [PrioritizedItem(start, 0)]
came_from, cost_so_far = {}, { start : 0 }
moves_simulated = 0
budget_exceeded = False
while frontier:
src = heapq.heappop(frontier).item
last_move = came_from[src][1] if src in came_from else None
for move in get_moves(src):
# Never immediately undo the move just taken.
if last_move:
if move == inverse_move(last_move): continue
# Opposite face moves commute; prune duplicate branches by enforcing canonical order.
if last_move[0] > move[0] and (-last_move[0][0], -last_move[0][1], -last_move[0][2]) == move[0]: continue
dst, cost = apply_move(move, src), cost_so_far[src] + 1
moves_simulated += 1
budget_exceeded = budget is not None and moves_simulated >= budget
if dst in cost_so_far and cost_so_far[dst] <= cost:
if budget_exceeded: break
continue
cost_so_far[dst], came_from[dst] = cost, (src, move)
if is_goal(dst): return reconstruct_solution(dst)
if budget_exceeded: break
h_weight = random.gauss(1, random_weight) if RANDOMIZE_SEARCH else 1
priority = cost + h_weight * heuristic(dst)
heapq.heappush(frontier, PrioritizedItem(dst, priority))
if budget_exceeded:
break
if not budget_exceeded or budget is None or random_weight == 0 or not RANDOMIZE_SEARCH:
return None
print("search budget of %d moves exceeded; restarting" % budget)
budget = max(int(1.5 * budget), budget + 1)
@functools.cache
def is_cubelet_solved(cubelet, rotation):
colors = np.diag(cubelet)
sticker_directions = rotation @ colors
return np.array_equal(sticker_directions, colors)
def is_cube_solved(cube): return all(is_cubelet_solved(c, r) for c, r in cube)
def num_solved_with_criterion(cube, criterion):
return sum(is_cubelet_solved(c, r) for c,r in cube if criterion(c))
@functools.cache
def min_moves_to_solved(cubelet, rotation):
def is_dst(r): return is_cubelet_solved(cubelet, r)
def apply_move(m, r): return tupled(rotation_matrix(m) @ r)
_, path = astar(rotation, is_dst, apply_move)
return len(path)
@functools.cache
def min_moves_to_position(cubelet, rotation):
def is_dst(r): return position(cubelet, r) == cubelet
def apply_move(m, r): return tupled(rotation_matrix(m) @ r)
_, path = astar(rotation, is_dst, apply_move)
return len(path)
def top_layer_heuristic(cube):
p, n = 0.5, 8
d = sum(min_moves_to_solved(c, r)**p for c, r in cube if c[2] == 1) ** (1/p)
return d/n
def middle_layer_heuristic(cube):
p, n = 0.5, 4
d = sum(min_moves_to_solved(c, r)**p for c, r in cube if c[2] >= 0) ** (1/p)
return d/n
def bottom_layer_edge_heuristic(cube):
p, n = 0.5, 3
d = sum(min_moves_to_solved(c, r)**p for c, r in cube
if not (c[2] == -1 and norm1(c) == 3)) ** (1/p)
return d/n
def bottom_layer_corner_heuristic(cube):
p, n1, n2, n3 = 0.5, 5, 3, 8
d1 = sum(min_moves_to_solved(c, r)**p for c, r in cube if c[2] == 1) ** (1/p)
d2 = sum(min_moves_to_solved(c, r)**p for c, r in cube if c[2] == 0) ** (1/p)
d3 = sum((min_moves_to_position(c, r) if norm1(c) == 3 else min_moves_to_solved(c, r))**p
for c, r in cube if c[2] == -1) ** (1/p)
return d1/n1 + d2/n2 + d3/n3
def is_top_edge(cubelet): return cubelet[2] == 1 and norm1(cubelet) == 2
def is_top_cubelet(cubelet): return cubelet[2] == 1
def is_top_or_middle_cubelet(cubelet): return cubelet[2] >= 0
def solve_top_and_middle_layer(cube, report_progress_callback):
solution_moves = ()
for num_solved in range(17):
report_progress_callback(cube)
print("solving cubelet #%d" % (num_solved + 1))
def is_goal(cube): return (
num_solved_with_criterion(cube, is_top_edge) >= min(4, num_solved + 1) and
num_solved_with_criterion(cube, is_top_cubelet) >= min(9, num_solved + 1) and
num_solved_with_criterion(cube, is_top_or_middle_cubelet) >= min(17, num_solved + 1)
)
heuristic = top_layer_heuristic if num_solved < 9 else middle_layer_heuristic
cube, next_moves = astar(cube, is_goal, apply_move_to_cube,
heuristic, random_weight=0.25)
print("-> found solution with %d moves" % len(next_moves))
solution_moves += next_moves
return (cube, solution_moves)
def is_bottom_edge(cubelet): return cubelet[2] == -1 and norm1(cubelet) == 2
def is_bottom_corner(cubelet): return cubelet[2] == -1 and norm1(cubelet) == 3
def has_yellow_bottom(cubelet, rotation):
return cubelet[2] == -1 and position((0, 0, -1), rotation) == (0, 0, -1)
def is_in_right_place(c, r): return position(c, r) == c
def num_bottom_edges_positioned(cube):
return sum(is_bottom_edge(c) and has_yellow_bottom(c, r) for c, r in cube)
def num_bottom_corners_positioned(cube):
return sum(is_bottom_corner(c) and is_in_right_place(c, r) for c, r in cube)
def solve_bottom_layer_edges(cube, report_progress_callback):
solution_moves = ()
for i in range(8):
report_progress_callback(cube)
print("solving bottom cross #%d" % (i + 1))
def is_goal(cube): return (
num_solved_with_criterion(cube, is_top_or_middle_cubelet) == 17 and
num_bottom_edges_positioned(cube) >= min(4, i + 1) and
num_solved_with_criterion(cube, is_bottom_edge) >= min(4, i - 3)
)
cube, next_moves = astar(cube, is_goal, apply_move_to_cube,
bottom_layer_edge_heuristic, random_weight=0.25)
print("-> found solution with %d moves" % len(next_moves))
solution_moves += next_moves
return (cube, solution_moves)
def solve_bottom_layer_corners(cube, report_progress_callback):
solution_moves = ()
for i in range(4):
report_progress_callback(cube)
print("positioning bottom corners #%d" % (i + 1))
def is_goal(cube): return (
num_solved_with_criterion(cube, is_top_or_middle_cubelet) == 17 and
num_solved_with_criterion(cube, is_bottom_edge) == 4 and
num_bottom_corners_positioned(cube) >= min(4, i + 1)
)
cube, next_moves = astar(cube, is_goal, apply_move_to_cube,
bottom_layer_corner_heuristic, random_weight=0.3)
print("-> found solution with %d moves" % len(next_moves))
solution_moves += next_moves
return (cube, solution_moves)
def bottom_left_front_corner(cube):
return next((c,r) for c,r in cube if position(c, r) == (1, -1, -1))
def solve_endgame(cube, report_progress_callback):
solution = []
left, top, bottom = ((0, -1, 0), 1), ((0, 0, 1), 1), ((0, 0, -1), 1)
routine = 2 * (inverse_move(left), inverse_move(top), left, top)
def apply(move):
nonlocal cube
solution.append(move)
cube = apply_move_to_cube(move, cube)
def is_corner_oriented():
c, r = bottom_left_front_corner(cube)
for _ in range(4):
if is_cubelet_solved(c, r): return True
r = apply_move_to_cubelet_rotation(bottom, c, r)
return False
for _ in range(4):
report_progress_callback(cube)
while not is_corner_oriented():
for move in routine: apply(move)
apply(bottom)
while not is_cube_solved(cube): apply(bottom)
return (cube, tuple(solution))
def solve(cube, report_progress_callback=lambda cube: None):
cube, solution1 = solve_top_and_middle_layer(cube, report_progress_callback)
print(50 * "-")
cube, solution2 = solve_bottom_layer_edges(cube, report_progress_callback)
print(50 * "-")
cube, solution3 = solve_bottom_layer_corners(cube, report_progress_callback)
print(50 * "-")
cube, solution4 = solve_endgame(cube, report_progress_callback)
solution = solution1 + solution2 + solution3 + solution4
print("Solved cube in %d moves. Final cube:" % len(solution))
# print(describe_cube(cube))
print("is_cube_solved: ", is_cube_solved(cube))
print("cube == solved_cube: ", cube == solved_cube)
return solution
def print_stats():
secs_elapsed = (datetime.now() - STARTUP_TIME).total_seconds()
cache_info = apply_move_to_cubelet_rotation.cache_info()
moves = (cache_info.hits + cache_info.misses) / len(solved_cube)
print("- time elapsed: %.1f sec" % secs_elapsed)
print("- moves simulated: %d (%.0f moves/sec) " % (
moves,
moves / secs_elapsed
))
cache_info = min_moves_to_solved.cache_info()
print("- min moves to solved calculations: ", cache_info.hits + cache_info.misses)
if __name__ == "__main__":
import sys
run_tests()
arg = sys.argv[1] if len(sys.argv) > 1 else "42"
if arg in ("-h", "--help"):
print("Usage: python eigencube.py [seed | --benchmark]")
elif arg == "--benchmark":
for seed in range(100):
print("== SEED:", seed, "==========================================")
solve(shuffle(solved_cube, iterations=100_000, seed=seed))
print_stats()
else:
seed = int(arg)
print("Solving scrambled cube (seed=%d)..." % seed)
solve(shuffle(solved_cube, iterations=100_000, seed=seed))
print_stats()