-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
287 lines (227 loc) · 9.51 KB
/
test.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import numpy as np
import time
from queue import PriorityQueue
import cv2
import math
import imageio as ig
import argparse
class nodes:
def __init__(self, explored=False, cost2come=math.inf, parent_idx=None):
self.explored = explored
self.parent_idx = parent_idx
self.cost2come = cost2come
# creating obstacle shapes
# Adding clearance to map walls
def map_clearance(map_canvas):
cv2.rectangle(map_canvas, (0, 0), (600, 250), (255, 255, 255), 5)
# rectangle obstacle with clearance
def rectangle1(map_canvas):
cv2.rectangle(map_canvas, (100, 0), (150, 100), (150, 100, 100), -1)
cv2.rectangle(map_canvas, (100, 0), (150, 100), (255, 255, 255), 5)
def rectangle2(map_canvas):
cv2.rectangle(map_canvas, (100, 150), (150, 250), (150, 100, 100), -1)
cv2.rectangle(map_canvas, (100, 150), (150, 250), (255, 255, 255), 5)
# hexagon obstacle with clearance
def hexagon(map_canvas):
pts = np.array([[300, 50], [225, 82.5], [225, 157.5],
[300, 200], [375, 157.5], [375, 82.5]])
cv2.fillPoly(map_canvas, np.int32([pts]), (150, 100, 100))
cv2.polylines(map_canvas, np.int32([pts]), True, (255, 255, 255), 5)
# triangle obstacle with clearance
def triangle(map_canvas):
pts = np.array([[460, 25], [460, 225], [510, 125]])
cv2.fillPoly(map_canvas, [pts], (150, 100, 100))
cv2.polylines(map_canvas, [pts], True, (255, 255, 255), 5)
# function to create map with obstacles in it
def Map():
image = np.zeros((250, 600, 3))
map_clearance(image)
rectangle1(image)
rectangle2(image)
hexagon(image)
triangle(image)
cv2.imwrite('Map.jpg', image)
return image
# Map()
# taking start and goal positions from the user
def getInput():
inp = argparse.ArgumentParser()
inp.add_argument('-src', '--starting_position',
required=True, nargs='+', type=int)
inp.add_argument('-goal', '--goal_position',
required=True, nargs='+', type=int)
positions = vars(inp.parse_args())
return positions
# to retrieve starting pos of bot
def start_pos(c_space, pos):
x, y = c_space.shape[:2]
s_x = x-pos[1]-1
s_y = pos[0]
if not Isvalid(c_space, s_x, s_y):
print('Invalid input!, starting position OUT-OF-RANGE!!!')
return None
return s_x, s_y
# to retreive goal pos of bot
def goal_pos(c_space, pos):
x, y = c_space.shape[:2]
# print(x)
g_x = x-pos[1]-1
g_y = pos[0]
if not Isvalid(c_space, g_x, g_y):
print('Invalid input!, goal position OUT-OF-RANGE!!!')
return None
return g_x, g_y
# to check if the current node is in the correct space
def Isvalid(c_space, x, y):
x_c, y_c = c_space.shape[:2]
# current node pos within map and not in obstacle space
if (0 <= x < x_c) and (0 <= y < y_c) and (c_space[x][y] == (0, 0, 0)).all():
return True
else:
return False
# Defining action set to get new nodes from current nodes
def action_set(t_node, c_space, c_node, openlist):
x_cur, y_cur = c_node
# Move up
if Isvalid(c_space, x_cur-1, y_cur):
# comparing the cost of new child node and current node
if (t_node[x_cur][y_cur].cost2come + 1) < t_node[x_cur-1][y_cur].cost2come:
t_node[x_cur-1][y_cur].cost2come = t_node[x_cur][y_cur].cost2come + 1
t_node[x_cur-1][y_cur].parent_idx = c_node
openlist.put((t_node[x_cur-1][y_cur].cost2come, (x_cur-1, y_cur)))
# Move down
if Isvalid(c_space, x_cur+1, y_cur):
if (t_node[x_cur][y_cur].cost2come + 1) < t_node[x_cur+1][y_cur].cost2come:
t_node[x_cur+1][y_cur].cost2come = t_node[x_cur][y_cur].cost2come + 1
t_node[x_cur+1][y_cur].parent_idx = c_node
openlist.put((t_node[x_cur+1][y_cur].cost2come, (x_cur+1, y_cur)))
# Move left
if Isvalid(c_space, x_cur, y_cur-1):
if (t_node[x_cur][y_cur].cost2come + 1) < t_node[x_cur][y_cur-1].cost2come:
t_node[x_cur][y_cur-1].cost2come = t_node[x_cur][y_cur].cost2come + 1
t_node[x_cur][y_cur-1].parent_idx = c_node
openlist.put((t_node[x_cur][y_cur-1].cost2come, (x_cur, y_cur-1)))
# Move right
if Isvalid(c_space, x_cur, y_cur+1):
if (t_node[x_cur][y_cur].cost2come + 1) < t_node[x_cur][y_cur+1].cost2come:
t_node[x_cur][y_cur+1].cost2come = t_node[x_cur][y_cur].cost2come + 1
t_node[x_cur][y_cur+1].parent_idx = c_node
openlist.put((t_node[x_cur][y_cur+1].cost2come, (x_cur, y_cur+1)))
# Move up-left
if Isvalid(c_space, x_cur-1, y_cur-1):
if (t_node[x_cur][y_cur].cost2come + 1.4) < t_node[x_cur-1][y_cur-1].cost2come:
t_node[x_cur-1][y_cur-1].cost2come = t_node[x_cur][y_cur].cost2come + 1.4
t_node[x_cur-1][y_cur-1].parent_idx = c_node
openlist.put(
(t_node[x_cur-1][y_cur-1].cost2come, (x_cur-1, y_cur-1)))
# Move up-right
if Isvalid(c_space, x_cur-1, y_cur+1):
if (t_node[x_cur][y_cur].cost2come + 1.4) < t_node[x_cur-1][y_cur+1].cost2come:
t_node[x_cur-1][y_cur+1].cost2come = t_node[x_cur][y_cur].cost2come + 1.4
t_node[x_cur-1][y_cur+1].parent_idx = c_node
openlist.put(
(t_node[x_cur-1][y_cur+1].cost2come, (x_cur-1, y_cur+1)))
# Move down-left
if Isvalid(c_space, x_cur+1, y_cur-1):
if (t_node[x_cur][y_cur].cost2come + 1.4) < t_node[x_cur+1][y_cur-1].cost2come:
t_node[x_cur+1][y_cur-1].cost2come = t_node[x_cur][y_cur].cost2come + 1.4
t_node[x_cur+1][y_cur-1].parent_idx = c_node
openlist.put(
(t_node[x_cur+1][y_cur-1].cost2come, (x_cur+1, y_cur-1)))
# Move down-right
if Isvalid(c_space, x_cur+1, y_cur+1):
if (t_node[x_cur][y_cur].cost2come + 1.4) < t_node[x_cur+1][y_cur+1].cost2come:
t_node[x_cur+1][y_cur+1].cost2come = t_node[x_cur][y_cur].cost2come + 1.4
t_node[x_cur+1][y_cur+1].parent_idx = c_node
openlist.put(
(t_node[x_cur+1][y_cur+1].cost2come, (x_cur+1, y_cur+1)))
return t_node
# To compute minimum cost2come of popped node from the open list
def lowestC2C(node):
# giving a random position to node
c_node = (-1, -1)
c2c_min = math.inf
# updating the cost
for i in range(len(node)):
for j in range(len(node[i])):
if node[i][j].cost2come < c2c_min and not node[i][j].explored:
c_node = (i, j)
c2c_min = node[i][j].cost2come
return c_node
# backtracking from goal to start node, to get the shortest path
def backtrack(node, g_node, image):
# taking current node as goal node
c_node = g_node
# to store the next nodes
queue = []
# storing the path moves
s2g_images = []
while c_node is not None:
queue.append(c_node)
c_node = node[c_node[0]][c_node[1]].parent_idx
queue = queue[::-1]
for n in queue:
image[n[0]][n[1]] = (150, 100, 100)
s2g_images.append(np.uint8(image.copy()))
image[n[0]][n[1]] = (0, 0, 0)
"""Saving path video, error with fourcc tag"""
# fourcc = cv2.VideoWriter_fourcc(*'mp4s')
# v = cv2.VideoWriter('path.mp4', fourcc, 55, (250, 600), True)
# for i in range(len(s2g_images)):
# v.write(s2g_images[i])
# cv2.destroyAllWindows()
# v.release()
ig.mimsave('path.gif', s2g_images, fps=50)
cv2.imwrite('backtracked.png', s2g_images[-1])
# dijkstra algorithm to find the path
def Dijkstra(pos):
c_space = Map()
closedlist_node = []
openlist_node = PriorityQueue()
r, c = c_space.shape[:2]
src = start_pos(c_space, pos['starting_position'])
dst = goal_pos(c_space, pos['goal_position'])
# if the starting and goal positions are empty
if (src is None and dst is None):
exit(1)
node_info = [[nodes() for n in range(c)] for m in range(r)]
node_info = np.array(node_info)
node_info[src[0]][src[1]].explored = True
node_info[src[0]][src[1]].cost2come = 0
# putting the starting node in the open list
openlist_node.put((node_info[src[0]][src[1]].cost2come, src))
# time when the algorithm begins
time_initial = time.time()
# Creating local copy of the map
img = c_space.copy()
# Marking the goal point
img[dst[0]][dst[1]] = (0, 255, 0)
while openlist_node:
cur_node = openlist_node.get()[1]
# comparing current node to goal
if cur_node == dst:
# time when goal state is reached
time_final = time.time()
print('time to goal {} sec'.format(time_final-time_initial))
print('Backtracking!, visualisation from goal to start')
backtrack(node_info, dst, img)
break
# nodes info getting updated, by finding child nodes in all directions
node_info[cur_node[0]][cur_node[1]].explored = True
node_info = action_set(node_info, c_space, cur_node, openlist_node)
img[cur_node[0]][cur_node[1]] = (255, 0, 0)
closedlist_node.append(np.uint8(img.copy()))
"""Saving explored nodes video, error with fourcc tag"""
# fourcc = cv2.VideoWriter_fourcc(*'mp4s')
# v = cv2.VideoWriter('explored.mp4', fourcc, 55, (250, 600), True)
# for i in range(len(closedlist_node)):
# v.write(closedlist_node[i])
# cv2.destroyAllWindows()
# v.release()
ig.mimsave('closedlist.gif', closedlist_node, fps=50)
cv2.imwrite('explorednodes.png', closedlist_node[-1])
if __name__ == '__main__':
coordinates = getInput()
Dijkstra(coordinates)
# cv2.waitKey(0)
# cv2.destroyAllWindows()