-
Notifications
You must be signed in to change notification settings - Fork 0
/
planner.py
105 lines (89 loc) · 3.65 KB
/
planner.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
import numpy as np
from TowerOfHanoi.GameState import GameState
def get_possible_moves(game_state):
# for each pillar, check with disk is on top and if it can be moved
# to either of the other two pillars
possible_moves = list()
disk_on_top = [10, 10, 10, 10]
for disk, pillar in enumerate(game_state.array):
if disk < disk_on_top[pillar]:
disk_on_top[pillar] = disk
for pillar, disk in enumerate(disk_on_top):
if disk == 10:
continue
pole_name = game_state.pole_names[pillar]
disk_name = game_state.disk_names[disk]
if pole_name == "start":
if disk_on_top[2] > disk:
target_pillar = "middle"
next_state = GameState(5, game_state.number)
next_state.move(disk_name, target_pillar)
possible_moves.append((next_state, (disk_name, pole_name, target_pillar)))
if disk_on_top[3] > disk:
target_pillar = "finish"
next_state = GameState(5, game_state.number)
next_state.move(disk_name, target_pillar)
possible_moves.append((next_state, (disk_name, pole_name, target_pillar)))
if pole_name == "middle":
if disk_on_top[1] > disk:
target_pillar = "start"
next_state = GameState(5, game_state.number)
next_state.move(disk_name, target_pillar)
possible_moves.append((next_state, (disk_name, pole_name, target_pillar)))
if disk_on_top[3] > disk:
target_pillar = "finish"
next_state = GameState(5, game_state.number)
next_state.move(disk_name, target_pillar)
possible_moves.append((next_state, (disk_name, pole_name, target_pillar)))
if pole_name == "finish":
if disk_on_top[1] > disk:
target_pillar = "start"
next_state = GameState(5, game_state.number)
next_state.move(disk_name, target_pillar)
possible_moves.append((next_state, (disk_name, pole_name, target_pillar)))
if disk_on_top[2] > disk:
target_pillar = "middle"
next_state = GameState(5, game_state.number)
next_state.move(disk_name, target_pillar)
possible_moves.append((next_state, (disk_name, pole_name, target_pillar)))
return possible_moves
def find_optimal_path(state):
counter = 0
element = (state, list())
visited = list()
queue = list()
queue.append(element)
while queue:
state, path = queue.pop(0)
if state.is_goal():
optimal_path = path
break
else:
# print(get_possible_moves(state))
for next_state, move in get_possible_moves(state):
next_path = [el for el in path]
next_path.append(move)
if next_state.number in visited:
continue
else:
visited.append(next_state.number)
element = (next_state, next_path)
queue.append(element)
else:
print("No path could be found.")
optimal_path = None
return optimal_path
def optimal_action(game_state):
# action format: disk_idx from_pillar to_pillar
path = find_optimal_path(game_state)
return path[0]
if __name__ == "__main__":
foo = GameState(5)
foo.move("orange", "start")
foo.move("yellow", "start")
foo.move("green", "start")
foo.move("blue", "start")
foo.move("purple", "start")
print(foo.array)
path = find_optimal_path(foo)
print(optimal_action(foo))