Skip to content

Commit 7034d5f

Browse files
authored
Add rng provide function for PRM planner (AtsushiSakai#607)
* Add rng provide function * fix CI error * remove unused import
1 parent 680ecda commit 7034d5f

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

PathPlanning/ProbabilisticRoadMap/probabilistic_road_map.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
77
"""
88

9-
import random
109
import math
1110
import numpy as np
1211
import matplotlib.pyplot as plt
13-
from scipy.spatial import cKDTree
12+
from scipy.spatial import KDTree
1413

1514
# parameter
1615
N_SAMPLE = 500 # number of sample_points
@@ -36,19 +35,35 @@ def __str__(self):
3635
str(self.cost) + "," + str(self.parent_index)
3736

3837

39-
def prm_planning(sx, sy, gx, gy, ox, oy, rr):
40-
41-
obstacle_kd_tree = cKDTree(np.vstack((ox, oy)).T)
38+
def prm_planning(start_x, start_y, goal_x, goal_y,
39+
obstacle_x_list, obstacle_y_list, robot_radius, *, rng=None):
40+
"""
41+
Run probabilistic road map planning
42+
43+
:param start_x:
44+
:param start_y:
45+
:param goal_x:
46+
:param goal_y:
47+
:param obstacle_x_list:
48+
:param obstacle_y_list:
49+
:param robot_radius:
50+
:param rng:
51+
:return:
52+
"""
53+
obstacle_kd_tree = KDTree(np.vstack((obstacle_x_list, obstacle_y_list)).T)
4254

43-
sample_x, sample_y = sample_points(sx, sy, gx, gy,
44-
rr, ox, oy, obstacle_kd_tree)
55+
sample_x, sample_y = sample_points(start_x, start_y, goal_x, goal_y,
56+
robot_radius,
57+
obstacle_x_list, obstacle_y_list,
58+
obstacle_kd_tree, rng)
4559
if show_animation:
4660
plt.plot(sample_x, sample_y, ".b")
4761

48-
road_map = generate_road_map(sample_x, sample_y, rr, obstacle_kd_tree)
62+
road_map = generate_road_map(sample_x, sample_y,
63+
robot_radius, obstacle_kd_tree)
4964

5065
rx, ry = dijkstra_planning(
51-
sx, sy, gx, gy, road_map, sample_x, sample_y)
66+
start_x, start_y, goal_x, goal_y, road_map, sample_x, sample_y)
5267

5368
return rx, ry
5469

@@ -88,13 +103,13 @@ def generate_road_map(sample_x, sample_y, rr, obstacle_kd_tree):
88103
89104
sample_x: [m] x positions of sampled points
90105
sample_y: [m] y positions of sampled points
91-
rr: Robot Radius[m]
106+
robot_radius: Robot Radius[m]
92107
obstacle_kd_tree: KDTree object of obstacles
93108
"""
94109

95110
road_map = []
96111
n_sample = len(sample_x)
97-
sample_kd_tree = cKDTree(np.vstack((sample_x, sample_y)).T)
112+
sample_kd_tree = KDTree(np.vstack((sample_x, sample_y)).T)
98113

99114
for (i, ix, iy) in zip(range(n_sample), sample_x, sample_y):
100115

@@ -122,11 +137,11 @@ def dijkstra_planning(sx, sy, gx, gy, road_map, sample_x, sample_y):
122137
"""
123138
s_x: start x position [m]
124139
s_y: start y position [m]
125-
gx: goal x position [m]
126-
gy: goal y position [m]
127-
ox: x position list of Obstacles [m]
128-
oy: y position list of Obstacles [m]
129-
rr: robot radius [m]
140+
goal_x: goal x position [m]
141+
goal_y: goal y position [m]
142+
obstacle_x_list: x position list of Obstacles [m]
143+
obstacle_y_list: y position list of Obstacles [m]
144+
robot_radius: robot radius [m]
130145
road_map: ??? [m]
131146
sample_x: ??? [m]
132147
sample_y: ??? [m]
@@ -215,17 +230,20 @@ def plot_road_map(road_map, sample_x, sample_y): # pragma: no cover
215230
[sample_y[i], sample_y[ind]], "-k")
216231

217232

218-
def sample_points(sx, sy, gx, gy, rr, ox, oy, obstacle_kd_tree):
233+
def sample_points(sx, sy, gx, gy, rr, ox, oy, obstacle_kd_tree, rng):
219234
max_x = max(ox)
220235
max_y = max(oy)
221236
min_x = min(ox)
222237
min_y = min(oy)
223238

224239
sample_x, sample_y = [], []
225240

241+
if rng is None:
242+
rng = np.random.default_rng()
243+
226244
while len(sample_x) <= N_SAMPLE:
227-
tx = (random.random() * (max_x - min_x)) + min_x
228-
ty = (random.random() * (max_y - min_y)) + min_y
245+
tx = (rng.random() * (max_x - min_x)) + min_x
246+
ty = (rng.random() * (max_y - min_y)) + min_y
229247

230248
dist, index = obstacle_kd_tree.query([tx, ty])
231249

@@ -241,7 +259,7 @@ def sample_points(sx, sy, gx, gy, rr, ox, oy, obstacle_kd_tree):
241259
return sample_x, sample_y
242260

243261

244-
def main():
262+
def main(rng=None):
245263
print(__file__ + " start!!")
246264

247265
# start and goal position
@@ -280,7 +298,7 @@ def main():
280298
plt.grid(True)
281299
plt.axis("equal")
282300

283-
rx, ry = prm_planning(sx, sy, gx, gy, ox, oy, robot_size)
301+
rx, ry = prm_planning(sx, sy, gx, gy, ox, oy, robot_size, rng=rng)
284302

285303
assert rx, 'Cannot found path'
286304

tests/test_probabilistic_road_map.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import conftest # Add root path to sys.path
2+
import numpy as np
23
from PathPlanning.ProbabilisticRoadMap import probabilistic_road_map
34

45

56
def test1():
67
probabilistic_road_map.show_animation = False
7-
probabilistic_road_map.main()
8+
probabilistic_road_map.main(rng=np.random.default_rng(1234))
89

910

1011
if __name__ == '__main__':

0 commit comments

Comments
 (0)