-
Notifications
You must be signed in to change notification settings - Fork 0
/
partA.py
168 lines (120 loc) · 5.25 KB
/
partA.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
"""
This file provides methods for Part A
"""
from grid import Grid
from path import Astar
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import numpy as np
def question_3():
print("----------------------")
print("Results for question 3")
print("Naive A* on large grid")
print("----------------------")
# creat grid with cell size = 1
cell_size = 1
grid_large = Grid(cell_size)
# load landmarks into grid
landmarks = grid_large.get_landmarks()
# create paths for A, B, C in question 3
# to run the naive implementation
astar_naive_A = Astar([0.5, -1.5], [0.5, 1.5], grid_large)
astar_naive_B = Astar([4.5, 3.5], [4.5, -1.5], grid_large)
astar_naive_C = Astar([-0.5, 5.5], [1.5, -3.5], grid_large)
# run naive implementation
astar_naive_A.naive()
astar_naive_A.get_path()
astar_naive_B.naive()
astar_naive_B.get_path()
astar_naive_C.naive()
astar_naive_C.get_path()
# results for naive approach to question 3 start and end configuration
print("Path A [0.5, -1.5], [0.5, 1.5]")
display_results(grid_large, cell_size, landmarks, astar_naive_A)
print("Path B [4.5, 3.5], [4.5, -1.5]")
display_results(grid_large, cell_size, landmarks, astar_naive_B)
print("Path C [-0.5, 5.5], [1.5, -3.5]")
display_results(grid_large, cell_size, landmarks, astar_naive_C)
def question_5():
print("----------------------")
print("Results for question 5")
print("Online A* on large grid")
print("----------------------")
# creat grid with cell size = 1
cell_size = 1
grid_large = Grid(cell_size)
# load landmarks into grid
landmarks = grid_large.get_landmarks()
# run the online implementation for the large grid
# given the start and end defined in question 3
astar_online_A = Astar([0.5, -1.5], [0.5, 1.5], grid_large)
astar_online_B = Astar([4.5, 3.5], [4.5, -1.5], grid_large)
astar_online_C = Astar([-0.5, 5.5], [1.5, -3.5], grid_large)
# run online implementation
astar_online_A.online()
astar_online_A.get_path()
astar_online_B.online()
astar_online_B.get_path()
astar_online_C.online()
astar_online_C.get_path()
# results for online approach to question 3 start and end configuration
print("Path A [0.5, -1.5], [0.5, 1.5]")
display_results(grid_large, cell_size, landmarks, astar_online_A)
print("Path B [4.5, 3.5], [4.5, -1.5]")
display_results(grid_large, cell_size, landmarks, astar_online_B)
print("Path C [-0.5, 5.5], [1.5, -3.5]")
display_results(grid_large, cell_size, landmarks, astar_online_C)
def question_7():
print("----------------------")
print("Results for question 7")
print("Online A* on fine grid")
print("----------------------")
# creat grid with cell size = 0.1
cell_size = 0.1
grid_small = Grid(cell_size)
# load landmarks into grid
landmarks = grid_small.get_landmarks()
# create paths for A, B, C in question 7 on small grid size
astar_online_A = Astar([2.45, -3.55], [0.95, -1.55], grid_small)
astar_online_B = Astar([4.95, -0.05], [2.45, 0.25], grid_small)
astar_online_C = Astar([-0.55, 1.45], [1.95, 3.95], grid_small)
# run online implementation
astar_online_A.online()
astar_online_A.get_path()
astar_online_B.online()
astar_online_B.get_path()
astar_online_C.online()
astar_online_C.get_path()
# results for online approach to question 7 start and end configuration
print("Path A [2.45, -3.55], [0.95, -1.55]")
display_results(grid_small, cell_size, landmarks, astar_online_A)
print("Path B [4.95, -0.05], [2.45, 0.25]")
display_results(grid_small, cell_size, landmarks, astar_online_B)
print("Path C [-0.55, 1.45], [1.95, 3.95]")
display_results(grid_small, cell_size, landmarks, astar_online_C)
def display_results(grid, cell_size, landmarks, astar):
fig, ax = plt.subplots(dpi=110)
#loc = plticker.MultipleLocator(base=cell_size)
loc = plticker.MultipleLocator(base=1)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)
plt.grid(True, color='blue', linestyle='--')
plt.imshow(grid.world.T, cmap=plt.cm.binary,
interpolation='none', origin='lower', extent=[-2, 5, -6, 6])
plt.plot(astar.path_world[0], astar.path_world[1], color='red', linewidth=2)
plt.scatter(astar.path_world[0], astar.path_world[1], color='red', s=20)
plt.scatter(astar.start_world[0], astar.start_world[1], s=30, color='blue')
#plt.text(astar.start_world[0], astar.start_world[1], 'S', fontsize =12,
#bbox=dict(facecolor='red', alpha=0.5))
ax.annotate('Start', xy=(astar.start_world[0],astar.start_world[1]),
xytext=(0, -30), textcoords='offset points',
arrowprops=dict(arrowstyle="->"))
plt.scatter(astar.goal_world[0], astar.goal_world[1], s=30, color='blue', label='Goal')
ax.annotate('Goal', xy=(astar.goal_world[0],astar.goal_world[1]),
xytext=(0, 20), textcoords='offset points',
arrowprops=dict(arrowstyle="->"))
plt.axis([-2, 5, -6, 6])
plt.title("Grid World")
plt.xlabel('x position (m)')
plt.ylabel('y position (m)')
plt.show()