-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra-pathplanning-Bharadwaj-Chukkala.py
372 lines (268 loc) · 10.9 KB
/
Dijkstra-pathplanning-Bharadwaj-Chukkala.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#IMPORTING NECESSARY LIBRARIES
import numpy as np
import matplotlib.pyplot as plt
import time
import heapq
########## DEFINING A NODE CLASS TO STORE NODES AS OBJECTS ###############
class Node:
def __init__(self, x, y, cost, parent_id):
self.x = x
self.y = y
self.cost = cost
self.parent_id = parent_id
def __lt__(self,other):
return self.cost < other.cost
########### DEFINING ACTIONS TO BE PERFORMED ##############
########### CALCULATING COST TO COME FOR ALL ACTIONS ########
def move_East(x,y,cost):
x = x + 1
cost = 1 + cost
return x,y,cost
def move_West(x,y,cost):
x = x - 1
cost = 1 + cost
return x,y,cost
def move_North(x,y,cost):
y = y + 1
cost = 1 + cost
return x,y,cost
def move_South(x,y,cost):
y = y - 1
cost = 1 + cost
return x,y,cost
def move_NorthEast(x,y,cost):
x = x + 1
y = y + 1
cost = 1 + cost
return x,y,cost
def move_NorthWest(x,y,cost): #Traversing Diagonally will cost more because of the distance covered
x = x - 1
y = y + 1
cost = np.sqrt(2) + cost
return x,y,cost
def move_SouthEast(x,y,cost):
x = x + 1
y = y - 1
cost = np.sqrt(2) + cost
return x,y,cost
def move_SouthWest(x,y,cost):
x = x -1
y = y - 1
cost = np.sqrt(2) + cost
return x,y,cost
############ DEFINING A FUNCTION TO PERFORM ACTIONS THAT ARE DEFINED #########
def Action_set(move,x,y,cost):
if move == 'West':
return move_West(x,y,cost)
elif move == 'East':
return move_East(x,y,cost)
elif move == 'North':
return move_North(x,y,cost)
elif move == 'South':
return move_South(x,y,cost)
elif move == 'NorthEast':
return move_NorthEast(x,y,cost)
elif move == 'NorthWest':
return move_NorthWest(x,y,cost)
elif move == 'SouthEast':
return move_SouthEast(x,y,cost)
elif move == 'SouthWest':
return move_SouthWest(x,y,cost)
else:
return None
############ CONFIGURATION SPACE CONSTRUCTION WITH OBSTACLES ############
def C_obs_space(width,height):
obs_space = np.full((height,width),0)
for y in range(height) :
for x in range(width):
####### CLEARANCE FOR THE OBSTACLES #######
#Polygon Obstacle (Clearance)
t1 = (y-5) - ((0.316) *(x+5)) - 173.608
t2 = (y+5) + (1.23 * (x+5)) - 229.34
t3 = (y-5) + (3.2 * (x-5)) - 436
t4 = (y+5) - 0.857*(x-5) - 111.42
t5 = y + (0.1136*x) - 189.09
#Circle Obstacle (Clearance)
C = ((y -185)**2) + ((x-300)**2) - (45)**2
#Hexagon Obstacle (Clearance)
h1 = (y-5) - 0.577*(x+5) - 24.97
h2 = (y-5) + 0.577*(x-5) - 255.82
h3 = (x-6.5) - 235
h6 = (x+6.5) - 165
h5 = (y+5) + 0.577*(x+5) - 175
h4 = (y+5) - 0.577*(x-5) + 55.82
#Conditions defining all points bounded by these lines are in the obstacle clearance area
if(h1<0 and h2<0 and h3<0 and h4>0 and h5>0 and h6>0) or C<=0 or (t1<0 and t5>0 and t4>0)or (t2>0 and t5<0 and t3<0):
obs_space[y,x] = 1
######## OBSTACLES ########
#Hexagon Obstacle
a1 = y - 0.577*x - 24.97
a2 = y + 0.577*x - 255.82
a3 = x - 235
a6 = x - 165
a5 = y + 0.577*x - 175
a4 = y - 0.577*x + 55.82
#Circle Obstacle
D = ((y -185)**2) + ((x-300)**2) - (40)**2
#Polygon Obstacle
l1 = y - ((0.316) *x) - 173.608
l2 = y + (1.23 * x) - 229.34
l3 = y + (3.2 * x) - 436
l4 = y - 0.857*x - 111.42
l5 = y + (0.1136*x) - 189.09
#Conditions defining all points bounded by these lines are in the obstacle area
if(a1<0 and a2<0 and a3<0 and a4>0 and a5>0 and a6>0) or D<0 or (l1<0 and l5>0 and l4>0)or (l2>0 and l5<0 and l3<0):
obs_space[y,x] = 2
####### DEFINING THE BOUNDARIES FOR CONFIGURATION SPACE ########
for i in range(400):
obs_space[0][i] = 1
for i in range(400):
obs_space[249][i] = 1
for i in range(250):
obs_space[i][1] = 1
for i in range(250):
obs_space[i][399] = 1
return obs_space
########## TO SEE IF THE MOVE IS VALID OR NOT #########
def ValidMove(x, y, obs_space):
e = obs_space.shape
if( x > e[1] or x < 0 or y > e[0] or y < 0 ):
return False
else:
try:
if(obs_space[y][x] == 1 or obs_space[y][x]==2):
return False
except:
pass
return True
########## DEFINING A FUNCTION TO CHECK IF THE PRESENT NODE IS GOAL NODE ##########
def Check_goal(present, goal):
if (present.x == goal.x) and (present.y == goal.y):
return True
else:
return False
######### GENERATE UNIQUE KEY ##########
def key(node):
key = 1022*node.x + 111*node.y
return key
########## DIJKSTRA ALGORITHM ###########
def dijkstra(start, goal,obs_space):
if Check_goal(start, goal):
return None,1
goal_node = goal
start_node = start
moves = ['West','East','North','South','NorthEast','NorthWest','SouthEast','SouthWest']
unexplored_nodes = {} #List of all open nodes
start_key = key(start_node) #Generating a unique key for identifying the node
unexplored_nodes[(start_key)] = start_node
explored_nodes = {} #List of all closed nodes
priority_list = [] #List to store all dictionary entries with cost as the sorting variable
heapq.heappush(priority_list, [start_node.cost, start_node]) #This Data structure will prioritize the node to be explored which has less cost.
all_nodes = [] #stores all nodes that have been traversed, for visualization purposes.
while (len(priority_list) != 0):
#popping the first element in the priority list to create child nodes for exploration
present_node = (heapq.heappop(priority_list))[1]
#appending all child nodes so that the explored region of the map can be plotted.
all_nodes.append([present_node.x, present_node.y])
#creating a dict key for identfication of node individually
present_id = key(present_node)
#The program will exist if the present node is the goal node
if Check_goal(present_node, goal_node):
goal_node.parent_id = present_node.parent_id
goal_node.cost = present_node.cost
print("Goal Node found")
return all_nodes,1
if present_id in explored_nodes:
continue
else:
explored_nodes[present_id] = present_node
#deleting the node from the open nodes list because it has been explored and further its child nodes will be generated
del unexplored_nodes[present_id]
#For all actions in action set, a new child node has to be formed if it is not already explored
for move in moves:
x,y,cost = Action_set(move,present_node.x,present_node.y,present_node.cost)
#Creating a node class object for all coordinates being explored
new_node = Node(x,y,cost,present_node)
new_node_id = key(new_node)
if not ValidMove(new_node.x, new_node.y, obs_space):
continue
elif new_node_id in explored_nodes:
continue
if new_node_id in unexplored_nodes:
if new_node.cost < unexplored_nodes[new_node_id].cost:
unexplored_nodes[new_node_id].cost = new_node.cost
unexplored_nodes[new_node_id].parent_id = new_node.parent_id
else:
unexplored_nodes[new_node_id] = new_node
heapq.heappush(priority_list, [ new_node.cost, new_node])
return all_nodes,0
########### BACKTRACK AND GENERATE SHORTEST PATH ############
def Backtrack(goal_node):
x_path = []
y_path = []
x_path.append(goal_node.x)
y_path.append(goal_node.y)
parent_node = goal_node.parent_id
while parent_node != -1:
x_path.append(parent_node.x)
y_path.append(parent_node.y)
parent_node = parent_node.parent_id
x_path.reverse()
y_path.reverse()
return x_path,y_path
######### PLOT OBSTACLES SPACE, EXPLORED NODES, SHORTEST PATH #######
def plot(start_node,goal_node,x_path,y_path,all_nodes,obs_space):
### Start node and Goal node ###
plt.plot(start_node.x, start_node.y, "Dw")
plt.plot(goal_node.x, goal_node.y, "Dg")
### Configuration Space for Obstacles ####
plt.imshow(obs_space, "GnBu")
ax = plt.gca()
ax.invert_yaxis() #y-axis inversion
### All visited nodes ###
for i in range(len(all_nodes)):
plt.plot(all_nodes[i][0], all_nodes[i][1], "2g")
#plt.pause(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001)
### Shortest path found ###
plt.plot(x_path,y_path, ":r")
plt.show()
plt.pause(3)
plt.close('all')
######### CALLING ALL MY FUNCTIONS TO IMPLEMENT dijkstra ALGORITHM ON A POINT ROBOT ###########
if __name__ == '__main__':
width = 400
height = 250
obs_space = C_obs_space(width, height)
#### Taking start node coordinates as input from user #####
start_coordinates = input("Enter coordinates for Start Node: ")
s_x, s_y = start_coordinates.split()
s_x = int(s_x)
s_y = int(s_y)
### Checking if the user input is valid #####
if not ValidMove(s_x, s_y, obs_space):
print("Start node is out of bounds")
exit(-1)
##### Taking Goal node coordinates as input from user #####
goal_coordinates = input("Enter coordinates for Goal Node: ")
g_x, g_y = goal_coordinates.split()
g_x = int(g_x)
g_y = int(g_y)
### Checking if the user input is valid #####
if not ValidMove(g_x, g_y, obs_space):
print("Goal node is out of bounds")
exit(-1)
### Timer to calculate computational time ###
timer_start = time.time()
##### Creating start_node and goal_node objects
start_node = Node(s_x, s_y, 0.0, -1)
goal_node = Node(g_x, g_y, 0.0, -1)
all_nodes,flag = dijkstra(start_node, goal_node,obs_space)
##### Plot shortest path only when goal node is reached #####
if (flag)==1:
x_path,y_path = Backtrack(goal_node)
else:
print("No path was found")
plot(start_node,goal_node,x_path,y_path,all_nodes,obs_space)
timer_stop = time.time()
C_time = timer_stop - timer_start
print("The Total Runtime is: ", C_time)