Skip to content

Commit 6a252de

Browse files
committed
for good measure
1 parent f6d05f9 commit 6a252de

File tree

1 file changed

+80
-69
lines changed

1 file changed

+80
-69
lines changed

PathPlanning/BatchInformedRRTStar/batch_informed_rrtstar.py

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ def setup_planning(self):
191191

192192
return etheta, cMin, xCenter, C, cBest
193193

194+
def setup_sample(self, iterations, foundGoal, cMin, xCenter, C, cBest):
195+
print("Batch: ", iterations)
196+
# Using informed rrt star way of computing the samples
197+
self.r = 2.0
198+
if iterations != 0:
199+
if foundGoal:
200+
# a better way to do this would be to make number of samples
201+
# a function of cMin
202+
m = 200
203+
self.samples = dict()
204+
self.samples[self.goalId] = self.goal
205+
else:
206+
m = 100
207+
cBest = self.g_scores[self.goalId]
208+
self.samples.update(self.informedSample(
209+
m, cBest, cMin, xCenter, C))
210+
return cBest
211+
212+
return cBest
213+
194214
def plan(self, animation=True):
195215

196216
etheta, cMin, xCenter, C, cBest = self.setup_planning()
@@ -201,22 +221,8 @@ def plan(self, animation=True):
201221
# run until done
202222
while (iterations < self.maxIter):
203223
if len(self.vertex_queue) == 0 and len(self.edge_queue) == 0:
204-
print("Batch: ", iterations)
205-
# Using informed rrt star way of computing the samples
206-
self.r = 2.0
207-
if iterations != 0:
208-
if foundGoal:
209-
# a better way to do this would be to make number of samples
210-
# a function of cMin
211-
m = 200
212-
self.samples = dict()
213-
self.samples[self.goalId] = self.goal
214-
else:
215-
m = 100
216-
cBest = self.g_scores[self.goalId]
217-
self.samples.update(self.informedSample(
218-
m, cBest, cMin, xCenter, C))
219-
224+
cBest = self.setup_sample(iterations,
225+
foundGoal, cMin, xCenter, C, cBest)
220226
# make the old vertices the new vertices
221227
self.old_vertices += self.tree.vertices.keys()
222228
# add the vertices to the vertex queue
@@ -241,57 +247,54 @@ def plan(self, animation=True):
241247
actualCostOfEdge = self.g_scores[bestEdge[0]] + \
242248
self.computeDistanceCost(bestEdge[0], bestEdge[1])
243249

244-
if(estimatedCostOfVertex < self.g_scores[self.goalId]):
245-
if(estimatedCostOfEdge < self.g_scores[self.goalId]):
246-
if(actualCostOfEdge < self.g_scores[self.goalId]):
247-
# connect this edge
248-
firstCoord = self.tree.nodeIdToRealWorldCoord(
249-
bestEdge[0])
250-
secondCoord = self.tree.nodeIdToRealWorldCoord(
251-
bestEdge[1])
252-
path = self.connect(firstCoord, secondCoord)
253-
lastEdge = self.tree.realWorldToNodeId(secondCoord)
254-
if path is None or len(path) == 0:
255-
continue
256-
nextCoord = path[len(path) - 1, :]
257-
nextCoordPathId = self.tree.realWorldToNodeId(
258-
nextCoord)
259-
bestEdge = (bestEdge[0], nextCoordPathId)
260-
if(bestEdge[1] in self.tree.vertices.keys()):
261-
continue
262-
else:
263-
try:
264-
del self.samples[bestEdge[1]]
265-
except(KeyError):
266-
pass
267-
eid = self.tree.addVertex(nextCoord)
268-
self.vertex_queue.append(eid)
269-
if eid == self.goalId or bestEdge[0] == self.goalId or bestEdge[1] == self.goalId:
270-
print("Goal found")
271-
foundGoal = True
272-
273-
self.tree.addEdge(bestEdge[0], bestEdge[1])
274-
275-
g_score = self.computeDistanceCost(
276-
bestEdge[0], bestEdge[1])
277-
self.g_scores[bestEdge[1]] = g_score + \
278-
self.g_scores[bestEdge[0]]
279-
self.f_scores[bestEdge[1]] = g_score + \
280-
self.computeHeuristicCost(bestEdge[1], self.goalId)
281-
self.updateGraph()
282-
283-
# visualize new edge
284-
if animation:
285-
self.drawGraph(xCenter=xCenter, cBest=cBest,
286-
cMin=cMin, etheta=etheta, samples=self.samples.values(),
287-
start=firstCoord, end=secondCoord, tree=self.tree.edges)
288-
289-
for edge in self.edge_queue:
290-
if(edge[1] == bestEdge[1]):
291-
if self.g_scores[edge[1]] + self.computeDistanceCost(edge[1], bestEdge[1]) >= self.g_scores[self.goalId]:
292-
if(lastEdge, bestEdge[1]) in self.edge_queue:
293-
self.edge_queue.remove(
294-
(lastEdge, bestEdge[1]))
250+
f1 = estimatedCostOfVertex < self.g_scores[self.goalId]
251+
f2 = estimatedCostOfEdge < self.g_scores[self.goalId]
252+
f3 = actualCostOfEdge < self.g_scores[self.goalId]
253+
254+
if f1 and f2 and f3:
255+
# connect this edge
256+
firstCoord = self.tree.nodeIdToRealWorldCoord(
257+
bestEdge[0])
258+
secondCoord = self.tree.nodeIdToRealWorldCoord(
259+
bestEdge[1])
260+
path = self.connect(firstCoord, secondCoord)
261+
lastEdge = self.tree.realWorldToNodeId(secondCoord)
262+
if path is None or len(path) == 0:
263+
continue
264+
nextCoord = path[len(path) - 1, :]
265+
nextCoordPathId = self.tree.realWorldToNodeId(
266+
nextCoord)
267+
bestEdge = (bestEdge[0], nextCoordPathId)
268+
if(bestEdge[1] in self.tree.vertices.keys()):
269+
continue
270+
else:
271+
try:
272+
del self.samples[bestEdge[1]]
273+
except(KeyError):
274+
pass
275+
eid = self.tree.addVertex(nextCoord)
276+
self.vertex_queue.append(eid)
277+
if eid == self.goalId or bestEdge[0] == self.goalId or bestEdge[1] == self.goalId:
278+
print("Goal found")
279+
foundGoal = True
280+
281+
self.tree.addEdge(bestEdge[0], bestEdge[1])
282+
283+
g_score = self.computeDistanceCost(
284+
bestEdge[0], bestEdge[1])
285+
self.g_scores[bestEdge[1]] = g_score + \
286+
self.g_scores[bestEdge[0]]
287+
self.f_scores[bestEdge[1]] = g_score + \
288+
self.computeHeuristicCost(bestEdge[1], self.goalId)
289+
self.updateGraph()
290+
291+
# visualize new edge
292+
if animation:
293+
self.drawGraph(xCenter=xCenter, cBest=cBest,
294+
cMin=cMin, etheta=etheta, samples=self.samples.values(),
295+
start=firstCoord, end=secondCoord, tree=self.tree.edges)
296+
297+
self.remove_queue(lastEdge, bestEdge)
295298

296299
else:
297300
print("Nothing good")
@@ -311,9 +314,17 @@ def plan(self, animation=True):
311314
plan = plan[::-1] # reverse the plan
312315
return plan
313316

317+
def remove_queue(self, lastEdge, bestEdge):
318+
for edge in self.edge_queue:
319+
if(edge[1] == bestEdge[1]):
320+
if self.g_scores[edge[1]] + self.computeDistanceCost(edge[1], bestEdge[1]) >= self.g_scores[self.goalId]:
321+
if(lastEdge, bestEdge[1]) in self.edge_queue:
322+
self.edge_queue.remove(
323+
(lastEdge, bestEdge[1]))
324+
314325
def connect(self, start, end):
315-
# A function which attempts to extend from a start coordinates
316-
# to goal coordinates
326+
# A function which attempts to extend from a start coordinates
327+
# to goal coordinates
317328
steps = self.computeDistanceCost(self.tree.realWorldToNodeId(
318329
start), self.tree.realWorldToNodeId(end)) * 10
319330
x = np.linspace(start[0], end[0], num=steps)

0 commit comments

Comments
 (0)