-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDjikstra_Point_Robot.py
430 lines (307 loc) · 12.5 KB
/
Djikstra_Point_Robot.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python
# coding: utf-8
"""
Created on Fri Mar 6 18:54:30 2020
@author: Arun
@author: Achal
"""
import math
import numpy as np
import cv2 as cv
import time
#Checking the boundary conditions for the obstacle space
def boundary_check(i,j):
if (i<0 or j>199 or j<0 or i>299):
return 0
else:
return 1
#Obstacle Map
def obs_map(x,y):
circle = ((np.square(x-225))+ (np.square(y-50)) <=np.square(25))
ellipse = (((np.square(x-150))/np.square(40))+((np.square(y-100))/np.square(20)) -1 <=0)
rhombus = (x*(-3/5)+y-55<0) and (x*(3/5)+y-325<0) and (x*(-3/5)+y-25>0) and (x*(3/5)+y-295 > 0)
rectangle = ((200-y) - (1.73)*x + 135 > 0 and (200-y) + (0.58)*x - 96.35 <= 0 and (200-y) - (1.73)*x - 15.54 <= 0 and (200-y) + (0.58)*x - 84.81 >= 0)
polygon1 = ((y+13*x-340>0) and x+y-100<0 and y+(-7/5)*x+20>0)#triangle1
polygon2 = (y-15>0 and (7/5)*x+y-120<0 and y+(-7/5)*x+20<0)#triangle2
polygon3 = ((7/5)*x+y-120>0 and (-6/5)*x+y+10<0 and (6/5)*x+y-170<0 and (-7/5)*x+y+90>0)#rhombus
if circle or ellipse or rhombus or rectangle or polygon1 or polygon2 or polygon3 :
obj_val = 0
else:
obj_val = 1
return obj_val
parent_list=[]
for j in range (300):
column=[]
for i in range (200):
column.append(0)
parent_list.append(column)
#Getting the start nodes from the user
x_start=int(input("Please enter start point x coordinate:"))
y_start=int(input("Please enter start point y coordinate:"))
y_start =200-y_start
start_obs=obs_map(x_start,y_start)
start_boundary=boundary_check(x_start,y_start)
while(start_obs and start_boundary!=1):
print("Incorrect start point! Please enter a valid start point:")
x_start=int(input("Please enter start point x coordinate:"))
y_start=int(input("Please enter start point y coordinate:"))
start_obs=obs_map(x_start,y_start)
start_boundary=boundary_check(x_start,y_start)
start=[x_start,y_start]
#Geting the goal nodes from the user
x_goal=int(input("Please enter goal point x coordinate:"))
y_goal=int(input("Please enter goal point y coordinate:"))
y_goal=200-y_goal
goal_obs=obs_map(x_goal,y_goal)
goal_boundary=boundary_check(x_goal,y_goal)
while(goal_obs and goal_boundary!=1):
print("Incorrect goal point! Please enter a valid goal point:")
x_goal=int(input("Please enter another goal point x coordinate:"))
y_goal=int(input("Please enter another goal point y coordinate:"))
goal_obs=obs_map(x_goal,y_goal)
goal_boundary=boundary_check(x_goal,y_goal)
goal=[x_goal,y_goal]
#Initializing cost as infinity
cost_array=np.array(np.ones((300,200)) * np.inf)
#Initializing visited nodes as empty array
visited=np.array(np.zeros((300,200)))
#Size of workspace
size=(200,300)
mapy=np.ones((size),np.uint8)*255
Q=[]
# append start point and initialize it's cost to zero
Q.append([x_start,y_start])
cost_array[x_start][y_start]=0
# Priority Queue Function
def pop(Q):
minimum_index=0
minimum_X = Q[0][0]
minimum_Y = Q[0][1]
for i in range(len(Q)):
x = Q[i][0]
y = Q[i][1]
if cost_array[x,y] < cost_array[minimum_X,minimum_Y]:
minimum_index = i
minimum_X = x
minimum_Y= y
current_node = Q[minimum_index]
Q.remove(Q[minimum_index])
return current_node
# All possible movements
def north(i,j):
new_node=[i,j+1]
return new_node
def south(i,j):
new_node=[i,j-1]
return new_node
def east(i,j):
new_node=[i+1,j]
return new_node
def west(i,j):
new_node=[i-1,j]
return new_node
def NE(i,j):
new_node=[i+1,j+1]
return new_node
def SE(i,j):
new_node=[i+1,j-1]
return new_node
def NW(i,j):
new_node=[i-1,j+1]
return new_node
def SW(i,j):
new_node=[i-1,j-1]
return new_node
#Djikstra Algorithm
start_time=time.time()
visited_node=[]
current_node=[x_start,y_start]
while current_node!=goal:
current_node=pop(Q)
new_north=north(current_node[0],current_node[1])
status=boundary_check(new_north[0],new_north[1])
flag=obs_map(new_north[0],new_north[1])
if (status and flag == 1):
if visited[new_north[0],new_north[1]]==0:
visited[new_north[0],new_north[1]]=1
visited_node.append(new_north)
Q.append(new_north)
parent_list[new_north[0]][new_north[1]]=current_node
cost_array[new_north[0],new_north[1]]=(cost_array[current_node[0],current_node[1]]+1)
else:
if cost_array[new_north[0],new_north[1]]>(cost_array[current_node[0],current_node[1]]+1):
cost_array[new_north[0],new_north[1]]=(cost_array[current_node[0],current_node[1]]+1)
parent_list[new_north[0]][new_north[1]]=current_node
new_south=south(current_node[0],current_node[1])
status=boundary_check(new_south[0],new_south[1])
flag=obs_map(new_south[0],new_south[1])
if (status and flag == 1):
if visited[new_south[0],new_south[1]]==0:
visited[new_south[0],new_south[1]]=1
visited_node.append(new_south)
Q.append(new_south)
parent_list[new_south[0]][new_south[1]]=current_node
cost_array[new_south[0],new_south[1]]=(cost_array[current_node[0],current_node[1]]+1)
else:
if cost_array[new_south[0],new_south[1]]>(cost_array[current_node[0],current_node[1]]+1):
cost_array[new_south[0],new_south[1]]=(cost_array[current_node[0],current_node[1]]+1)
parent_list[new_south[0]][new_south[1]]=current_node
new_east=east(current_node[0],current_node[1])
status=boundary_check(new_east[0],new_east[1])
flag=obs_map(new_east[0],new_east[1])
if (status and flag == 1):
if visited[new_east[0],new_east[1]]==0:
visited[new_east[0],new_east[1]]=1
visited_node.append(new_east)
Q.append(new_east)
parent_list[new_east[0]][new_east[1]]=current_node
cost_array[new_east[0],new_east[1]]=(cost_array[current_node[0],current_node[1]]+1)
else:
if cost_array[new_east[0],new_east[1]]>(cost_array[current_node[0],current_node[1]]+1):
cost_array[new_east[0],new_east[1]]=(cost_array[current_node[0],current_node[1]]+1)
parent_list[new_east[0]][new_east[1]]=current_node
new_west=west(current_node[0],current_node[1])
status=boundary_check(new_west[0],new_west[1])
flag=obs_map(new_west[0],new_west[1])
if (status and flag == 1):
if visited[new_west[0],new_west[1]]==0:
visited[new_west[0],new_west[1]]=1
visited_node.append(new_west)
Q.append(new_west)
parent_list[new_west[0]][new_west[1]]=current_node
cost_array[new_west[0],new_west[1]]=(cost_array[current_node[0],current_node[1]]+1)
else:
if cost_array[new_west[0],new_west[1]]>(cost_array[current_node[0],current_node[1]]+1):
cost_array[new_west[0],new_west[1]]=(cost_array[current_node[0],current_node[1]]+1)
parent_list[new_west[0]][new_west[1]]=current_node
new_NE=NE(current_node[0],current_node[1])
status=boundary_check(new_NE[0],new_NE[1])
flag=obs_map(new_NE[0],new_NE[1])
if (status and flag == 1):
if visited[new_NE[0],new_NE[1]]==0:
visited[new_NE[0],new_NE[1]]=1
visited_node.append(new_NE)
Q.append(new_NE)
parent_list[new_NE[0]][new_NE[1]]=current_node
cost_array[new_NE[0],new_NE[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
else:
if cost_array[new_NE[0],new_NE[1]]>(cost_array[current_node[0],current_node[1]]+math.sqrt(2)):
cost_array[new_NE[0],new_NE[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
parent_list[new_NE[0]][new_NE[1]]=current_node
new_SE=SE(current_node[0],current_node[1])
status=boundary_check(new_SE[0],new_SE[1])
flag=obs_map(new_SE[0],new_SE[1])
if (status and flag == 1):
if visited[new_SE[0],new_SE[1]]==0:
visited[new_SE[0],new_SE[1]]=1
visited_node.append(new_SE)
Q.append(new_SE)
parent_list[new_SE[0]][new_SE[1]]=current_node
cost_array[new_SE[0],new_SE[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
else:
if cost_array[new_SE[0],new_SE[1]]>(cost_array[current_node[0],current_node[1]]+math.sqrt(2)):
cost_array[new_SE[0],new_SE[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
parent_list[new_SE[0]][new_SE[1]]=current_node
new_NW=NW(current_node[0],current_node[1])
status=boundary_check(new_NW[0],new_NW[1])
flag=obs_map(new_NW[0],new_NW[1])
if (status and flag == 1):
if visited[new_NW[0],new_NW[1]]==0:
visited[new_NW[0],new_NW[1]]=1
visited_node.append(new_NW)
Q.append(new_NW)
parent_list[new_NW[0]][new_NW[1]]=current_node
cost_array[new_NW[0],new_NW[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
else:
if cost_array[new_NW[0],new_NW[1]]>(cost_array[current_node[0],current_node[1]]+math.sqrt(2)):
cost_array[new_NW[0],new_NW[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
parent_list[new_NW[0]][new_NW[1]]=current_node
new_SW=SW(current_node[0],current_node[1])
status=boundary_check(new_SW[0],new_SW[1])
flag=obs_map(new_SW[0],new_SW[1])
if (status and flag == 1):
if visited[new_SW[0],new_SW[1]]==0:
visited[new_SW[0],new_SW[1]]=1
visited_node.append(new_SW)
Q.append(new_SW)
parent_list[new_SW[0]][new_SW[1]]=current_node
cost_array[new_SW[0],new_SW[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
else:
if cost_array[new_SW[0],new_SW[1]]>(cost_array[current_node[0],current_node[1]]+math.sqrt(2)):
cost_array[new_SW[0],new_SW[1]]=(cost_array[current_node[0],current_node[1]]+math.sqrt(2))
parent_list[new_SW[0]][new_SW[1]]=current_node
print("Goal reached")
#Pathfinder function
goal=[x_goal,y_goal]
start=[x_start,y_start]
path=[]
def path_find(goal,start):
GN=goal
path.append(goal)
while (GN!=start):
a=parent_list[GN[0]][GN[1]]
path.append(a)
GN=a
path_find(goal,start)
print('The cost of the shortest path is',cost_array[x_goal,y_goal])
def circle(x,y):
if ((np.square(x-225))+ (np.square(y-50)) <=np.square(25)):
return True
else:
return False
def ellipse(x,y):
if (((np.square(x-150))/np.square(40))+((np.square(y-100))/np.square(20)) -1 <=0):
return True
else:
return False
def rectangle(x,y):
if (200-y) - (1.73)*x + 135 > 0 and (200-y) + (0.58)*x - 96.35 <= 0 and (200-y) - (1.73)*x - 15.54 <= 0 and (200-y) + (0.58)*x - 84.81 >= 0:
return True
else:
return False
def rhombus(x,y):
if ((x*(-3/5)+y-55<0) and (x*(3/5)+y-325<0) and (x*(-3/5)+y-25>0) and (x*(3/5)+y-295 > 0)):
return True
else:
return False
#Dividing the non convex polygon into three convex polygons:Two triangles and a rhombus
def polygon1(x,y):#triangle1
if((y+13*x-340>0) and x+y-100<0 and y+(-7/5)*x+20>0):
return True
else:
return False
def polygon2(x,y):#triangle2
if y-15>0 and (7/5)*x+y-120<0 and y+(-7/5)*x+20<0:
return True
else:
return False
def polygon3(x,y):#rhombus
if (7/5)*x+y-120>0 and (-6/5)*x+y+10<0 and (6/5)*x+y-170<0 and (-7/5)*x+y+90>0:
return True
else:
return False
x = 300
y = 200
image = np.ones((y,x,3),np.uint8)*255
for i in range(200):
for j in range(300):
if circle(j,i) or ellipse(j,i) or rectangle(j,i) or rhombus(j,i) or polygon1(j,i) or polygon2(j,i) or polygon3(j,i) :
image[i][j] = 0
pic=cv.resize(image,None,fx=3,fy=3)
#Showing the graphical output
cv.circle(image,(int(goal[0]),int(goal[1])), (1), (0,0,255), -1);
cv.circle(image,(int(start[0]),int(start[1])), (1), (0,0,255), -1);
for i in visited_node:
cv.circle(image,(int(i[0]),int(i[1])), (1), (255,0,0));
pic=cv.resize(image,None,fx=3,fy=3)
cv.imshow('map',pic)
cv.waitKey(1)
for i in path:
cv.circle(image,(int(i[0]),int(i[1])), (1), (150,50,204));
pic=cv.resize(image,None,fx=3,fy=3)
cv.imshow('map',pic)
cv.waitKey(1)
print("Total time:")
print(time.time()-start_time)
cv.waitKey(0)
cv.destroyAllWindows()