Skip to content

Commit f6d05f9

Browse files
committed
try code cleanup
1 parent de57e64 commit f6d05f9

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

PathPlanning/BatchInformedRRTStar/batch_informed_rrtstar.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
22
Batch Informed Trees based path planning:
3-
Uses a heuristic to efficiently search increasingly dense
4-
RGGs while reusing previous information. Provides faster
3+
Uses a heuristic to efficiently search increasingly dense
4+
RGGs while reusing previous information. Provides faster
55
convergence that RRT*, Informed RRT* and other sampling based
6-
methods.
6+
methods.
77
8-
Uses lazy connecting by combining sampling based methods and A*
9-
like incremental graph search algorithms.
8+
Uses lazy connecting by combining sampling based methods and A*
9+
like incremental graph search algorithms.
1010
1111
author: Karan Chawla(@karanchawla)
1212
@@ -15,8 +15,6 @@
1515

1616
import random
1717
import numpy as np
18-
import copy
19-
import operator
2018
import math
2119
import matplotlib.pyplot as plt
2220

@@ -101,8 +99,7 @@ def gridCoordToRealWorldCoord(self, coord):
10199
start = self.lowerLimit[i]
102100
# step from the coordinate in the grid
103101
grid_step = self.resolution * coord[i]
104-
half_step = self.resolution / 2 # To get to middle of the grid
105-
config[i] = start + grid_step # + half_step
102+
config[i] = start + grid_step
106103
return config
107104

108105
def nodeIdToGridCoord(self, node_id):
@@ -156,8 +153,7 @@ def __init__(self, start, goal,
156153
self.tree = RTree(start=start, lowerLimit=lowerLimit,
157154
upperLimit=upperLimit, resolution=0.01)
158155

159-
def plan(self, animation=True):
160-
156+
def setup_planning(self):
161157
self.startId = self.tree.realWorldToNodeId(self.start)
162158
self.goalId = self.tree.realWorldToNodeId(self.goal)
163159

@@ -172,12 +168,8 @@ def plan(self, animation=True):
172168
self.f_scores[self.startId] = self.computeHeuristicCost(
173169
self.startId, self.goalId)
174170

175-
iterations = 0
176171
# max length we expect to find in our 'informed' sample space, starts as infinite
177172
cBest = self.g_scores[self.goalId]
178-
pathLen = float('inf')
179-
solutionSet = set()
180-
plan = []
181173

182174
# Computing the sampling space
183175
cMin = math.sqrt(pow(self.start[0] - self.goal[1], 2) +
@@ -196,6 +188,15 @@ def plan(self, animation=True):
196188

197189
self.samples.update(self.informedSample(
198190
200, cBest, cMin, xCenter, C))
191+
192+
return etheta, cMin, xCenter, C, cBest
193+
194+
def plan(self, animation=True):
195+
196+
etheta, cMin, xCenter, C, cBest = self.setup_planning()
197+
iterations = 0
198+
plan = []
199+
199200
foundGoal = False
200201
# run until done
201202
while (iterations < self.maxIter):
@@ -286,20 +287,17 @@ def plan(self, animation=True):
286287
start=firstCoord, end=secondCoord, tree=self.tree.edges)
287288

288289
for edge in self.edge_queue:
289-
if(edge[0] == bestEdge[1]):
290-
if self.g_scores[edge[0]] + self.computeDistanceCost(edge[0], bestEdge[1]) >= self.g_scores[self.goalId]:
291-
if(edge[0], best_edge[1]) in self.edge_queue:
292-
self.edge_queue.remove(
293-
(edge[0], bestEdge[1]))
294290
if(edge[1] == bestEdge[1]):
295291
if self.g_scores[edge[1]] + self.computeDistanceCost(edge[1], bestEdge[1]) >= self.g_scores[self.goalId]:
296292
if(lastEdge, bestEdge[1]) in self.edge_queue:
297293
self.edge_queue.remove(
298294
(lastEdge, bestEdge[1]))
295+
299296
else:
300297
print("Nothing good")
301298
self.edge_queue = []
302299
self.vertex_queue = []
300+
303301
iterations += 1
304302

305303
print("Finding the path")
@@ -449,6 +447,9 @@ def expandVertex(self, vid):
449447
self.edge_queue.append((vid, sid))
450448

451449
# add the vertex to the edge queue
450+
self.add_vertex_to_edge_queue(vid, currCoord)
451+
452+
def add_vertex_to_edge_queue(self, vid, currCoord):
452453
if vid not in self.old_vertices:
453454
neigbors = []
454455
for v, edges in self.tree.vertices.items():
@@ -459,7 +460,6 @@ def expandVertex(self, vid):
459460

460461
for neighbor in neigbors:
461462
sid = neighbor[0]
462-
scoord = neighbor[1]
463463
estimated_f_score = self.computeDistanceCost(self.startId, vid) + \
464464
self.computeDistanceCost(
465465
vid, sid) + self.computeHeuristicCost(sid, self.goalId)
@@ -472,8 +472,6 @@ def updateGraph(self):
472472
currId = self.startId
473473
openSet.append(currId)
474474

475-
foundGoal = False
476-
477475
while len(openSet) != 0:
478476
# get the element with lowest f_score
479477
currId = min(openSet, key=lambda x: self.f_scores[x])
@@ -484,7 +482,6 @@ def updateGraph(self):
484482
# Check if we're at the goal
485483
if(currId == self.goalId):
486484
self.nodes[self.goalId]
487-
foundGoal = True
488485
break
489486

490487
if(currId not in closedSet):
@@ -497,7 +494,6 @@ def updateGraph(self):
497494
continue
498495
else:
499496
# claculate tentative g score
500-
succesorCoord = self.tree.nodeIdToRealWorldCoord(succesor)
501497
g_score = self.g_scores[currId] + \
502498
self.computeDistanceCost(currId, succesor)
503499
if succesor not in openSet:
@@ -570,7 +566,9 @@ def main():
570566
bitStar = BITStar(start=[-1, 0], goal=[3, 8], obstacleList=obstacleList,
571567
randArea=[-2, 15])
572568
path = bitStar.plan(animation=show_animation)
573-
print(path)
569+
# print(path)
570+
print("Done")
571+
574572
if show_animation:
575573
plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r')
576574
plt.grid(True)

0 commit comments

Comments
 (0)