Skip to content

Commit 23c7356

Browse files
committed
Optimize the obstacle cost calculation.
The calculation was too slow to work with more obstacles. The calculation function is vectorized to increase the speed.
1 parent 0cca27d commit 23c7356

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

PathPlanning/DynamicWindowApproach/dynamic_window_approach.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,14 @@ def calc_obstacle_cost(traj, ob, config):
139139
calc obstacle cost inf: collision
140140
"""
141141

142-
skip_n = 2 # for speed up
143-
minr = float("inf")
144-
145-
for ii in range(0, len(traj[:, 1]), skip_n):
146-
for i in range(len(ob[:, 0])):
147-
ox = ob[i, 0]
148-
oy = ob[i, 1]
149-
dx = traj[ii, 0] - ox
150-
dy = traj[ii, 1] - oy
151-
152-
r = math.sqrt(dx**2 + dy**2)
153-
if r <= config.robot_radius:
154-
return float("Inf") # collision
155-
156-
if minr >= r:
157-
minr = r
142+
ox = ob[:, 0]
143+
oy = ob[:, 1]
144+
dx = traj[:, 0] - ox[:, None]
145+
dy = traj[:, 1] - oy[:, None]
146+
r = np.sqrt(np.square(dx) + np.square(dy))
147+
if (r <= config.robot_radius).any():
148+
return float("Inf")
149+
minr = np.min(r)
158150

159151
return 1.0 / minr # OK
160152

0 commit comments

Comments
 (0)