-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubesolver.py
More file actions
71 lines (58 loc) · 2.17 KB
/
cubesolver.py
File metadata and controls
71 lines (58 loc) · 2.17 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
from patterndb import build_db
from structs import *
from astar import *
class CubeProblem(Problem):
def __init__(self, cube, actions=ACTIONS):
self._initial_state = cube
self._actions = actions
model_cube = Cube(3).sub_cube(top)
edge_cube = model_cube.sub_cube(edge)
corner_cube = model_cube.sub_cube(corner)
pair_cube = model_cube.sub_cube(lambda b: b.solved_location.x == 1 and b.solved_location.y != -1)
self._dbs = [build_db("korfedge", edge_cube, 6, ACTIONS),
build_db("korfcorner", corner_cube, 6, ACTIONS),
build_db("korffull", model_cube, 6, ACTIONS),
build_db("korfpair", pair_cube, 7, ACTIONS)]
def initial_state(self):
return self._initial_state
def goal_test(self, state):
return state.solved()
def actions(self, node):
return filter_actions(self._actions, node.action)
def apply_action(self, state, action):
return state.apply(action), 1
def heuristic(self, state):
db_lookup = max([db.get_from_cube(state) for db in self._dbs])
return max(state.turn_distance() / 8, db_lookup)
if __name__ == "__main__":
from plotter import start_plotter
from time import sleep
from random import randrange
from astar import search
render = start_plotter()
cube = Cube(3)
cube = cube.sub_cube(top)
turns = 12
previous = None
for i in range(turns):
action = random.choice(filter_actions(ACTIONS, previous)) # More likely to give a good shuffle rather than pure random
cube = cube.apply(action)
previous = action
render(cube)
def callback(status):
print(status)
render(status["state"])
# import cProfile
# with cProfile.Profile() as pr:
p = CubeProblem(cube)
states, actions = search(p, callback=callback, callback_freq=100)
# pr.print_stats()
print(f'Solved in {len(actions)-1} turns')
for i in range(20):
current = cube
render(current)
for action in actions[1:]:
current = current.apply(action)
render(current)
sleep(1)
sleep(4)