-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
61 lines (42 loc) · 1.53 KB
/
solver.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
import numpy as np
import gym
import rubiks_cube_gym # noqa
import pickle
import argparse
def solve_scramble(scramble):
state = env.reset(scramble=scramble)
number_of_moves = 0
episode_reward = 0
success = 0
done = False
if render:
env.render()
while not done:
action = np.argmax(q_table[state])
new_state, reward, done, info = env.step(action)
episode_reward += reward
if render:
env.render(render_time=500)
print(action, end=" ")
if reward >= 60:
success = 100
state = new_state
number_of_moves += 1
print()
return episode_reward, success, number_of_moves
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Validate a q_table against scrambles from WCA competitions')
parser.add_argument('-q', '--q_table', required=True, help="Q Table name")
parser.add_argument('-e', '--env', required=True, help="Environment name")
parser.add_argument('-s', '--scramble', required=True, help="Scramble to solve")
parser.add_argument('-r', '--render', type=bool, default=False, help="Render the solution")
args = parser.parse_args()
env = gym.make(args.env)
with open(args.q_table, "rb") as f:
q_table = pickle.load(f)
render = args.render
episode_reward, success, number_of_moves = solve_scramble(args.scramble)
print("----RESULTS----")
print(f"Reward: {episode_reward}")
print(f"Success: {success}")
print(f"Number of moves: {number_of_moves}")