1010import numpy as np
1111import copy
1212import operator
13- from math import cos , sin , atan2 , pi
13+ import math
1414import matplotlib .pyplot as plt
1515
1616show_animation = True
@@ -26,8 +26,8 @@ def __init__(self, x, y):
2626class BITStar ():
2727
2828 def __init__ (self , start , goal ,
29- obstacleList , randArea ,
30- expandDis = 0.5 , goalSampleRate = 10 , maxIter = 200 , eta ):
29+ obstacleList , randArea , eta = 2.0 ,
30+ expandDis = 0.5 , goalSampleRate = 10 , maxIter = 200 ):
3131 self .start = Node (start [0 ], start [1 ])
3232 self .goal = Node (goal [0 ], goal [1 ])
3333 self .minrand = randArea [0 ]
@@ -43,7 +43,7 @@ def __init__(self, start, goal,
4343 self .f_scores = dict ()
4444 self .r = float ('inf' )
4545 self .eta = eta # tunable parameter
46- self .unit_ball_measure = #TODO
46+ self .unit_ball_measure = 1
4747 self .old_vertices = []
4848
4949 def plan (self , animation = True ):
@@ -52,10 +52,10 @@ def plan(self, animation=True):
5252 plan = None
5353 iterations = 0
5454 # max length we expect to find in our 'informed' sample space, starts as infinite
55- cBest = float ('inf' )
56- pathLen = float ('inf' )
57- solutionSet = set ()
58- path = None
55+ cBest = float ('inf' )
56+ pathLen = float ('inf' )
57+ solutionSet = set ()
58+ path = None
5959
6060 # Computing the sampling space
6161 cMin = math .sqrt (pow (self .start .x - self .goal .x , 2 ) +
@@ -72,22 +72,22 @@ def plan(self, animation=True):
7272 C = np .dot (np .dot (U , np .diag (
7373 [1.0 , 1.0 , np .linalg .det (U ) * np .linalg .det (np .transpose (Vh ))])), Vh )
7474
75- # while (iterations < self.maxIter):
75+ while (iterations < self .maxIter ):
76+ if len (self .vertex_queue ) == 0 and len (self .edge_queue ) == 0 :
77+ samples = self .informedSample (100 , cBest , cMin , xCenter , C )
78+ # prune the tree
7679
77- if len (self .vertex_queue ) == 0 and len (self .edge_queue ) == 0 :
78- print ("Batch: " , iterations )
79- samples = self .informedSample (100 , cBest , cMin , xCenter , C )
80- # prune the tree
80+ if animation :
81+ self .drawGraph (xCenter = xCenter , cBest = cBest ,
82+ cMin = cMin , etheta = etheta , samples = samples )
8183
82- if animation :
83- self .drawGraph (xCenter = xCenter , cBest = cBest ,
84- cMin = cMin , etheta = etheta , rnd = samples )
84+ iterations += 1
8585
86+ return plan
8687
88+ # def expandVertex(self, vertex):
8789
88- def expandVertex (self , vertex ):
89-
90- def prune (self , c ):
90+ # def prune(self, c):
9191
9292 def radius (self , q ):
9393 dim = len (start ) #dimensions
@@ -98,26 +98,26 @@ def radius(self, q):
9898 return min_radius * pow (numpy .log (q )/ q , 1 / dim )
9999
100100 # Return the closest sample
101- def getNearestSample (self ):
101+ # def getNearestSample(self):
102102
103103 # Sample free space confined in the radius of ball R
104104 def informedSample (self , m , cMax , cMin , xCenter , C ):
105- if cMax < float ( 'inf' ):
106- samples = []
107- for i in range (m ):
108- r = [cMax / 2.0 ,
109- math .sqrt (cMax ** 2 - cMin ** 2 ) / 2.0 ,
110- math .sqrt (cMax ** 2 - cMin ** 2 ) / 2.0 ]
111- L = np .diag (r )
112- xBall = self .sampleUnitBall ()
113- rnd = np .dot (np .dot (C , L ), xBall ) + xCenter
114- rnd = [rnd [(0 , 0 )], rnd [(1 , 0 )]]
115- samples .append (rnd )
116- else :
117- for i in range (m ):
118- rnd = self .sampleFreeSpace ()
119- samples .append (rnd )
120-
105+ samples = []
106+ if cMax < float ( 'inf' ):
107+ for i in range (m ):
108+ r = [cMax / 2.0 ,
109+ math .sqrt (cMax ** 2 - cMin ** 2 ) / 2.0 ,
110+ math .sqrt (cMax ** 2 - cMin ** 2 ) / 2.0 ]
111+ L = np .diag (r )
112+ xBall = self .sampleUnitBall ()
113+ rnd = np .dot (np .dot (C , L ), xBall ) + xCenter
114+ rnd = [rnd [(0 , 0 )], rnd [(1 , 0 )]]
115+ samples .append (rnd )
116+ else :
117+ for i in range (m ):
118+ rnd = self .sampleFreeSpace ()
119+ samples .append (rnd )
120+ return samples
121121
122122 # Sample point in a unit ball
123123 def sampleUnitBall (self ):
@@ -131,27 +131,27 @@ def sampleUnitBall(self):
131131 b * math .sin (2 * math .pi * a / b ))
132132 return np .array ([[sample [0 ]], [sample [1 ]], [0 ]])
133133
134- def sampleFreeSpace (self ):
134+ def sampleFreeSpace (self ):
135135 if random .randint (0 , 100 ) > self .goalSampleRate :
136136 rnd = [random .uniform (self .minrand , self .maxrand ),
137137 random .uniform (self .minrand , self .maxrand )]
138- else :
138+ else :
139139 rnd = [self .goal .x , self .goal .y ]
140140
141141 return rnd
142142
143- def bestVertexQueueValue (self ):
144-
145- def bestEdgeQueueValue (self ):
143+ # def bestVertexQueueValue(self):
146144
147- def bestInEdgeQueue (self ):
145+ # def bestEdgeQueueValue (self):
148146
149- def bestInVertexQueue (self ):
147+ # def bestInEdgeQueue (self):
150148
151- def updateGraph (self ):
149+ # def bestInVertexQueue (self):
152150
153- def drawGraph (self , xCenter = None , cBest = None , cMin = None , etheta = None , rnd = None ):
151+ # def updateGraph (self):
154152
153+ def drawGraph (self , xCenter = None , cBest = None , cMin = None , etheta = None , samples = None ):
154+ print ("Plotting Graph" )
155155 plt .clf ()
156156 for rnd in samples :
157157 if rnd is not None :
@@ -172,7 +172,7 @@ def drawGraph(self, xCenter=None, cBest=None, cMin=None, etheta=None, rnd=None):
172172 plt .plot (self .goal .x , self .goal .y , "xr" )
173173 plt .axis ([- 2 , 15 , - 2 , 15 ])
174174 plt .grid (True )
175- plt .pause (0.01 )
175+ plt .pause (5 )
176176
177177 def plot_ellipse (self , xCenter , cBest , cMin , etheta ):
178178
@@ -207,6 +207,7 @@ def main():
207207 bitStar = BITStar (start = [0 , 0 ], goal = [5 , 10 ],
208208 randArea = [- 2 , 15 ], obstacleList = obstacleList )
209209 path = bitStar .plan (animation = show_animation )
210+ print ("Done" )
210211
211- if show_animation :
212- bitStar . drawGraph ()
212+ if __name__ == '__main__' :
213+ main ()
0 commit comments