-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_solver.py
237 lines (204 loc) · 9.19 KB
/
simple_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
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
import heapq
import time
import numpy as np
import collections
class Queue:
"""Priority Queue data structure based on heapq"""
def __init__(self):
self.elements = []
self.count = 0
def add(self, item, priority):
entry = (priority, self.count, item)
heapq.heappush(self.elements, entry)
self.count += 1
def pop(self):
(_, _, item) = heapq.heappop(self.elements)
return item
class State:
def __init__(self, player, boxes):
self.player = player
self.boxes = boxes
def goal_state(self, goals):
return all(map(lambda g: g in self.boxes, goals))
def manhattan_distance(self, box, goal):
return abs(box[0] - goal[0]) + abs(box[1] - goal[1])
def distance_heuristic(self, goals):
remaining_boxes = list(self.boxes)
result = 0
for g in goals:
min_distance = 999999
min_box = None
for box in remaining_boxes:
distance = self.manhattan_distance(box, g)
if distance < min_distance:
min_distance = distance
min_box = box
box = min_box
result += min_distance
remaining_boxes.remove(box)
return result
def plot_notation(self, walls, goals):
plot = "" # ["" for i in range(len(self.walls))]
for y in range(0, len(walls[0])):
for x in range(0, len(walls)):
if walls[x][y]:
plot += "#"
elif [x, y] == self.player:
plot += "@"
elif [x, y] in self.boxes:
plot += "$"
elif [x, y] in goals:
plot += "."
else:
plot += " "
plot += '\n'
print(plot)
class AISolver:
def __init__(self, board):
self.initial_state, self.walls, self.goals = self.standard_notation_to_board_state(board)
def solve(self, algorithm):
start = time.time()
self.nr_nodes = 0
if algorithm == "BFS":
print("Search using breadth first search")
solution = self.breadthFirstSearch(self.initial_state)
elif algorithm == "ASTAR":
print("Search using A* search")
solution = self.a_star_search(self.initial_state)
else:
print("Algorithm ", algorithm, "is not implemented")
solution = None
print("Actions: ")
print(solution)
print("Computation time:", time.time() - start, "seconds")
print("Number of generated nodes: ", self.nr_nodes)
return solution
def standard_notation_to_board_state(self, board):
walls = np.zeros((len(max(board, key=len)), len(board)))
boxes = []
goals = []
y = 0
for row in board:
x = 0
for char in row:
if char == "#":
walls[x, y] = 1
elif char == "@":
player = [x, y]
elif char == "$":
boxes.append([x, y])
elif char == ".":
goals.append([x, y])
elif char == " ":
pass
elif char == "+":
goals.append([x, y])
player = [x, y]
elif char == "*":
goals.append([x, y])
boxes.append([x, y])
else:
print("Unknown character")
x += 1
y += 1
board_state = State(player, boxes)
return board_state, walls, goals
def a_star_search(self, state):
"""
A* search
"""
start_state = state
fringe = Queue() # store states
fringe.add([start_state], start_state.distance_heuristic(self.goals)) # add start state to fringe
explored = set() # store set of explored states
actions = Queue() # store actions and their f-value
actions.add([], start_state.distance_heuristic(self.goals)) # add current
while fringe:
node = fringe.pop() # get node
node_action = actions.pop() # get path of actions to current node
if node[-1].goal_state(self.goals): # check if goal state is reached
solution = node_action # solution is path / list of actions
break
if not self.in_explored_set(node[-1], explored): # if current state is not in explored set
explored.add(node[-1]) # add current state to explored set
cost = len(node_action[1:]) # cost of current path of actions equals number of actions
moves = ["up", "down", "right", "left"]
for direction in moves: # iterate possible moves
new_state = self.move(node[-1], direction) # move
if new_state is None: # if move is not legal
continue
n_new = node.copy()
n_new.append(new_state) # append new state to path of current node
fringe.add(n_new, new_state.distance_heuristic(
self.goals) + cost) # append node and its f-value to path of current node
actions.add(node_action + [direction], new_state.distance_heuristic(
self.goals) + cost) # append action and its cost to path of actions
self.nr_nodes += 1
return solution
def breadthFirstSearch(self, state):
"""
Implementation of breadth first search
"""
start_state = state
fringe = collections.deque([[start_state]]) # store states
actions = collections.deque([[]]) # store actions
explored = set() # store set of explored states
while fringe:
node = fringe.popleft() # get node
node_action = actions.popleft() # get path of actions to current node
if node[-1].goal_state(self.goals): # check if goal state is reached
solution = node_action # solution is path / list of actions
break
if not self.in_explored_set(node[-1], explored): # if current state is not in explored set
explored.add(node[-1]) # add current state to explored set
moves = ["up", "down", "right", "left"]
for direction in moves: # iterate possible moves
new_state = self.move(node[-1], direction) # move
if new_state is None: # if move is not legal
continue
n_new = node.copy()
n_new.append(new_state) # append new state to path of current node
fringe.append(n_new) # append node to fringe
actions.append(node_action + [direction]) # append path of actions
self.nr_nodes += 1
return solution
def in_explored_set(self, state, explored_set):
contained = False
for s in explored_set:
if state.boxes == s.boxes and state.player == s.player:
contained = True
break
return contained
def move(self, state, direction):
new_state = State(state.player.copy(), state.boxes.copy())
if direction == "up":
target_player = [state.player[0], state.player[1] - 1]
target_box = [state.player[0], state.player[1] - 2]
if direction == "down":
target_player = [state.player[0], state.player[1] + 1]
target_box = [state.player[0], state.player[1] + 2]
if direction == "right":
target_player = [state.player[0] + 1, state.player[1]]
target_box = [state.player[0] + 2, state.player[1]]
if direction == "left":
target_player = [state.player[0] - 1, state.player[1]]
target_box = [state.player[0] - 2, state.player[1]]
if 0 <= target_player[0] < len(self.walls[0]) and 0 <= target_player[1] < len(
self.walls): # if not out of bounds
if not self.walls[target_player[0]][target_player[1]]:
new_state.player = target_player
if target_player in state.boxes: # if box is on player target position
if 0 <= target_box[0] < len(self.walls[0]) and 0 <= target_box[1] < len(
self.walls): # if box target position is not out of bounds
if not self.walls[target_box[0]][
target_box[1]] and not target_box in state.boxes: # if box target position is not wall
new_state.boxes[new_state.boxes.index(target_player)] = target_box
else:
new_state = None
else:
new_state = None
else:
new_state = None
else:
new_state = None
return new_state