forked from MirjanaBlazhevska/FEIT01L006
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation.py
189 lines (165 loc) · 7.38 KB
/
Simulation.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
from collections import deque
from time import sleep
import heapq
import numpy as np
from matplotlib import pyplot as plt
world_shape = 20, 20
path_color = (0.8, 0.5, 0.8)
agent_color = (0.8, 0, 0)
world_color = (1, 1, 0.8)
expanded_state_color = (0.5, 0.8, 0.5)
visited_state_color = (0.8, 1, 0.8)
goal_color = (0, 0, 0.8)
class Simulation:
def __init__(self, agent, goal, world, world_weight):
plt.ion()
self.figure = plt.figure()
self.ax = plt.axes()
self.ax.set_autoscaley_on(False)
self.agent = agent
self.goal = goal
self.world = world
self.world_weight = world_weight
self.clean_world()
def update_screen(self):
plt.cla()
self.ax.imshow(self.world)
self.figure.canvas.draw()
self.figure.canvas.flush_events()
def blind_search_visualised(self, search_algorithm):
initial_state = self.agent
states_queue = deque([[initial_state]])
visited = {initial_state}
while states_queue:
states_list = states_queue.popleft()
state_to_expand = states_list[-1]
if state_to_expand != initial_state:
self.world[state_to_expand] = expanded_state_color
for next_state in self.expand_state(state_to_expand):
if next_state not in visited:
visited.add(next_state)
if next_state == self.goal:
return states_list + [next_state]
if search_algorithm == 'breadth':
states_queue.append(states_list + [next_state])
elif search_algorithm == 'depth':
states_queue.appendleft(states_list + [next_state])
else:
raise AttributeError('{} is not a valid algorithm'.format(search_algorithm))
if all(self.world[next_state] != expanded_state_color):
self.world[next_state] = visited_state_color
self.update_screen()
return []
def a_star_search_visualised(self, alpha, distance=None):
"""
A* algorithm
:param alpha: ratio between path cost and heuristic. weight = path_so_far + alpha * heuristic
:param distance: heuristic function to use. Can be euclidean or manhattan
:return: None
"""
initial_state = self.agent
expanded = set()
states_queue = [(0, [initial_state])]
heapq.heapify(states_queue)
while states_queue:
current_weight, states_list = heapq.heappop(states_queue)
state_to_expand = states_list[-1]
if state_to_expand != initial_state:
self.world[state_to_expand] = expanded_state_color
if state_to_expand == self.goal:
return states_list
if state_to_expand in expanded:
continue
for next_state in self.expand_state(state_to_expand):
transition_weight = self.world_weight[next_state] - self.world_weight[state_to_expand]
djikstra_weight = current_weight + transition_weight
heuristic_weight = self.distance(self.goal, next_state, distance) if distance else 0
a_star_weight = (1 - alpha) * djikstra_weight + alpha * heuristic_weight
if next_state not in expanded:
if all(self.world[next_state] != expanded_state_color):
self.world[next_state] = visited_state_color
heapq.heappush(states_queue, (a_star_weight, states_list + [next_state]))
expanded.add(state_to_expand)
self.update_screen()
return []
@staticmethod
def expand_state(state):
agent_i, agent_j = state
new_states = []
if agent_i > 0:
new_states.append((agent_i - 1, agent_j))
if agent_j > 0:
new_states.append((agent_i, agent_j - 1))
if agent_i < world_shape[0] - 1:
new_states.append((agent_i + 1, agent_j))
if agent_j < world_shape[1] - 1:
new_states.append((agent_i, agent_j + 1))
return new_states
@staticmethod
def distance(point_1, point_2, distance):
if distance == 'manhattan':
return abs(point_1[0] - point_2[0]) + abs(point_1[1] - point_2[1])
elif distance == 'euclidean':
return np.sqrt((point_1[0] - point_2[0]) ** 2 + (point_1[1] - point_2[1]) ** 2)
def clean_world(self):
self.world[:] = world_color
self.world[:, :, 0] = world_color[0] * self.world_weight
self.world[:, :, 1] = world_color[1] * self.world_weight
self.world[:, :, 2] = world_color[2] * self.world_weight
self.world[self.agent] = agent_color
self.world[self.goal] = goal_color
def draw_path(self, path):
self.world[self.agent] = agent_color
for state in path[1:-1]:
self.world[state] = path_color
self.world[self.goal] = goal_color
self.update_screen()
sleep(2)
def simulate_algorithm(self, algorithm, a_star_alpha=None, distance=None):
self.clean_world()
suptitle = {'depth': 'Depth first search', 'breadth': 'Breadth first search', 'djikstra': 'Djikstra search',
'greedy': 'Greedy search', 'a_star': 'A star'}
if algorithm == 'a_star':
filename = 'images/{}_{}_{:.3f}.png'.format(algorithm, distance, a_star_alpha)
plt.suptitle('{} {} {:.3f}'.format(suptitle[algorithm], distance, a_star_alpha))
elif algorithm == 'greedy':
filename = 'images/{}_{}.png'.format(algorithm, distance)
plt.suptitle('{} {}'.format(suptitle[algorithm], distance))
else:
filename = 'images/{}.png'.format(algorithm)
plt.suptitle(suptitle[algorithm])
if algorithm in ['depth', 'breadth']:
path = self.blind_search_visualised(algorithm)
else:
alpha = 0 if algorithm == 'djikstra' else 1 if algorithm == 'greedy' else a_star_alpha
path = self.a_star_search_visualised(alpha, distance)
self.draw_path(path)
plt.imsave(filename, self.world)
def simulate_all(self):
self.simulate_algorithm('depth')
self.simulate_algorithm('breadth')
self.simulate_algorithm('djikstra')
self.simulate_algorithm('greedy', distance='manhattan')
self.simulate_algorithm('greedy', distance='euclidean')
self.simulate_algorithm('a_star', a_star_alpha=0.8, distance='manhattan')
self.simulate_algorithm('a_star', a_star_alpha=0.8, distance='euclidean')
def calc_cost(x, y):
f = (x - 2 * world_shape[0] // 3) ** 2 + (y - world_shape[1] // 2) ** 2
return f
def world_weight_playground():
world_weight = np.array([calc_cost(x, y) for x in range(world_shape[0]) for y in range(world_shape[1])])
world_weight = world_weight.reshape(world_shape)
world_weight = 1 - world_weight / np.max(world_weight)
plt.suptitle('World weight')
plt.imshow(world_weight)
plt.colorbar()
plt.show()
return world_weight
def main():
agent = world_shape[0] // 2, world_shape[1] // 5
goal = world_shape[0] // 5, 4 * world_shape[1] // 5
world = np.zeros((world_shape[0], world_shape[1], 3), dtype=np.float)
world_weight = world_weight_playground()
sim = Simulation(agent, goal, world, world_weight)
sim.simulate_all()
main()