forked from mayurj747/oraclenet-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinformed_rrt.py
376 lines (307 loc) · 9.33 KB
/
informed_rrt.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
"""
Path Planning Sample Code with RRT*
author: AtsushiSakai(@Atsushi_twi)
"""
import random
import math
import copy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import time
from data_loader import load_test_dataset
import pickle
import pygame
show_animation = True
obc,obstacles, paths, path_lengths= load_test_dataset()
'''XDIM = 40
YDIM = 40
windowSize = [XDIM, YDIM]
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode(windowSize)
white = 255, 255, 255
black = 0, 0, 0
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
cyan = 0,180,105
dark_green = 0, 102, 0'''
class RRT():
"""
Class for RRT Planning
"""
def __init__(self, start, goal, obstacleList, randArea,
expandDis=0.6, goalSampleRate=0, maxIter=2000):
"""
Setting Parameter
start:Start Position [x,y]
goal:Goal Position [x,y]
obstacleList:obstacle Positions [[x,y,size],...]
randArea:Ramdom Samping Area [min,max]
"""
self.start = Node(start[0], start[1])
self.end = Node(goal[0], goal[1])
self.minrand = randArea[0]
self.maxrand = randArea[1]
self.expandDis = expandDis
self.goalSampleRate = goalSampleRate
self.maxIter = maxIter
self.obstacleList = obstacleList
def Planning(self, animation=False):
"""
Pathplanning
animation: flag for animation on or off
"""
self.nodeList = [self.start]
for i in range(self.maxIter):
rnd = self.get_centroid_point()
#rnd=self.get_random_point()
nind = self.GetNearestListIndex(self.nodeList, rnd)
newNode = self.steer(rnd, nind)
# print(newNode.cost)
if self.__CollisionCheck(newNode, self.obstacleList):
nearinds = self.find_near_nodes(newNode)
newNode = self.choose_parent(newNode, nearinds)
self.nodeList.append(newNode)
self.rewire(newNode, nearinds)
if animation:
self.DrawGraph(rnd)
# generate coruse
#print "I am here"
lastIndex = self.get_best_last_index()
if lastIndex !=None:
print "iterations: "+str(i)
break
if lastIndex !=None:
path = self.gen_final_course(lastIndex)
return path, self.nodeList[lastIndex].cost
else:
return 0,0
def choose_parent(self, newNode, nearinds):
if len(nearinds) == 0:
return newNode
dlist = []
for i in nearinds:
dx = newNode.x - self.nodeList[i].x
dy= newNode.y - self.nodeList[i].y
theta = math.atan2(dy,dx)
d = math.sqrt(dx ** 2 + dy** 2)
if self.check_collision_extend(self.nodeList[i], theta, d):
dlist.append(self.nodeList[i].cost + d)
else:
dlist.append(float("inf"))
mincost = min(dlist)
minind = nearinds[dlist.index(mincost)]
if mincost == float("inf"):
print("mincost is inf")
return newNode
newNode.cost = mincost
newNode.parent = minind
return newNode
def steer(self, rnd, nind):
# expand tree
nearestNode = self.nodeList[nind]
theta = math.atan2(rnd[1] - nearestNode.y, rnd[0] - nearestNode.x)
newNode = copy.deepcopy(nearestNode)
newNode.x += self.expandDis * math.cos(theta)
newNode.y += self.expandDis * math.sin(theta)
newNode.cost += self.expandDis
newNode.parent = nind
return newNode
def get_random_point(self):
if random.randint(0, 100) > self.goalSampleRate:
rnd = [random.uniform(self.minrand, self.maxrand),
random.uniform(self.minrand, self.maxrand)]
else: # goal point sampling
rnd = [self.end.x, self.end.y]
return rnd
def get_centroid_point(self):
if random.randint(0, 100) > self.goalSampleRate:
rnd = [random.uniform(self.minrand, self.maxrand),random.uniform(self.minrand, self.maxrand)]
dsg= math.sqrt((self.start.x-self.end.x)** 2 + (self.start.y-self.end.y)** 2)
drg= math.sqrt((rnd[0]-self.end.x)** 2 + (rnd[1]-self.end.y)** 2)
drs= math.sqrt((rnd[0]-self.start.x)** 2 + (rnd[1]-self.start.y)** 2)
rnd[0]=(rnd[0]*dsg+self.start.x*drg+self.end.x*drs)/(dsg+drg+drs)
rnd[1]=(rnd[1]*dsg+self.start.y*drg+self.end.y*drs)/(dsg+drg+drs)
else: # goal point sampling
rnd = [self.end.x, self.end.y]
return rnd
def get_best_last_index(self):
#print "I am here 1"
disglist = [self.calc_dist_to_goal(node.x, node.y) for node in self.nodeList]
#print "I am here 2"
goalinds = [disglist.index(i) for i in disglist if i <= self.expandDis]
#print(goalinds)
if not goalinds:
#print "Empty"
return None
#print "I am here 3"
mincost = min([self.nodeList[i].cost for i in goalinds])
#print "I am here 4"
for i in goalinds:
if self.nodeList[i].cost == mincost:
return i
return None
def gen_final_course(self, goalind):
path = [[self.end.x, self.end.y]]
while self.nodeList[goalind].parent is not None:
node = self.nodeList[goalind]
path.append([node.x, node.y])
goalind = node.parent
path.append([self.start.x, self.start.y])
return path
def calc_dist_to_goal(self, x, y):
return np.linalg.norm([x - self.end.x, y - self.end.y])
def find_near_nodes(self, newNode):
nnode = len(self.nodeList)
r = 40.0 * math.sqrt((math.log(nnode) / nnode))
# r = self.expandDis * 5.0
dlist = [(node.x - newNode.x) ** 2 +(node.y - newNode.y) ** 2 for node in self.nodeList]
nearinds = [dlist.index(i) for i in dlist if i <= r ** 2]
return nearinds
def rewire(self, newNode, nearinds):
nnode = len(self.nodeList)
for i in nearinds:
nearNode = self.nodeList[i]
dx = newNode.x - nearNode.x
dy = newNode.y - nearNode.y
d = math.sqrt(dx ** 2 + dy ** 2)
scost = newNode.cost + d
if nearNode.cost > scost:
theta = math.atan2(dy, dx)
if self.check_collision_extend(nearNode, theta, d):
nearNode.parent = nnode - 1
nearNode.cost = scost
def check_collision_extend(self, nearNode, theta, d):
tmpNode = copy.deepcopy(nearNode)
for i in range(int(d / self.expandDis)):
tmpNode.x += self.expandDis * math.cos(theta)
tmpNode.y += self.expandDis * math.sin(theta)
if not self.__CollisionCheck(tmpNode, self.obstacleList):
return False
return True
def DrawGraph(self, rnd=None):
u"""
Draw Graph
"""
plt.clf()
if rnd is not None:
plt.plot(rnd[0], rnd[1], "^k")
for node in self.nodeList:
if node.parent is not None:
plt.plot([node.x, self.nodeList[node.parent].x], [node.y, self.nodeList[node.parent].y], "-g")
for (ox, oy, sizex,sizey) in self.obstacleList:
plt.plot(ox, oy, "sk", ms=7.2*sizex)
'''fig3 = plt.figure()
ax3 = fig3.add_subplot(111, aspect='equal')
for (ox, oy, size) in self.obstacleList:
p=patches.Rectangle((ox,oy),size,size)
ax3.add_patch(p)
#input()'''
plt.plot(self.start.x, self.start.y, "xr")
plt.plot(self.end.x, self.end.y, "xr")
plt.axis([-20, 20, -20, 20])
plt.grid(True)
plt.pause(0.01)
def GetNearestListIndex(self, nodeList, rnd):
dlist = [(node.x - rnd[0]) ** 2 + (node.y - rnd[1])** 2 for node in nodeList]
minind = dlist.index(min(dlist))
return minind
def __CollisionCheck(self, node, obstacleList):
for (ox, oy, sizex,sizey) in obstacleList:
dx = abs(ox - node.x)
dy = abs(oy - node.y)
CF = False
if dx > sizex/2.0 or dy > sizey/2.0:
CF= True #safe
if CF==False:
return False # collision
return True # safe'''
class Node():
"""
RRT Node
"""
def __init__(self, x, y):
self.x = x
self.y = y
self.cost = 0.0
self.parent = None
'''def init_obstacles(obstacleList): #initialized the obstacle
rectObs = []
for (ox,oy,sizex,sizey) in obstacleList:
obs=np.zeros(2,dtype=np.float32)
X=np.zeros(2,dtype=np.float32)
Y=np.zeros(2,dtype=np.float32)
X[0]=1.0*sizex/2.0
X[1]=0.0
Y[0]=0.0
Y[1]=1.0*sizey/2.0
obs[0]=ox-X[0]+Y[0]
obs[1]=oy-X[1]+Y[1]
rectObs.append(pygame.Rect((obs[0],obs[1]),(sizex,sizey)))
for rect in rectObs:
pygame.draw.rect(screen, black, rect)
fpsClock.tick(10)'''
def main():
print("Start rrt planning")
min_t=1000.0
max_t=0.0
m_time=0.0
count=0.0
e=[]
ce=[]
for i in range(0,10):
print "i="+str(i)
tenv=[]
cenv=[]
for j in range(0,20):
print "i= "+str(i)+" j= "+str(j)
if path_lengths[i][j]>0:
# ====Search Path with RRT====
obstacleList = [
(obc[i][0][0], obc[i][0][1], 5.0, 5.0),
(obc[i][1][0], obc[i][1][1], 5.0, 5.0),
(obc[i][2][0], obc[i][2][1], 5.0, 5.0),
(obc[i][3][0], obc[i][3][1], 5.0, 5.0),
(obc[i][4][0], obc[i][4][1], 5.0, 5.0),
(obc[i][5][0], obc[i][5][1], 5.0, 5.0),
(obc[i][6][0], obc[i][6][1], 5.0, 5.0)
] # [x,y,size(radius)]
# Set Initial parameters
#init_obstacles(obstacleList)
tt=0.0
for s in range(0,1):
rrt = RRT(start=[paths[i][j][0][0], paths[i][j][0][1]], goal=[paths[i][j][path_lengths[i][j]-1][0], paths[i][j][path_lengths[i][j]-1][1]],
randArea=[-20, 20], obstacleList=obstacleList)
tic = time.clock()
path, cost = rrt.Planning(animation=False)
toc = time.clock()
t=toc-tic
tt=tt+t
m_time=(tt/float(1))
tenv.append(m_time)
cenv.append(cost)
if m_time>max_t:
max_t=m_time
if m_time<min_t:
min_t=m_time
count=count+1
e.append(tenv)
ce.append(cenv)
pickle.dump(e, open("time_s2D_unseen_irrt.p", "wb" ))
pickle.dump(ce, open("cost_s2D_unseen_irrt.p", "wb" ))
print "mean time: "+ str(m_time/float(count))
print "max time: "+ str(max_t)
print "min time: "+ str(min_t)
#print path
print "cost" + str(cost)
# Draw final path
if show_animation:
rrt.DrawGraph()
plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r')
plt.grid(True)
plt.pause(0.01) # Need for Mac
plt.show()
if __name__ == '__main__':
main()