Skip to content

Commit 4ffd8e7

Browse files
authored
Optimize is_obstacle using set (#1223)
1 parent a8a4503 commit 4ffd8e7

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

PathPlanning/DStarLite/d_star_lite.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@ def __init__(self, ox: list, oy: list):
6363
self.y_max = int(abs(max(oy) - self.y_min_world))
6464
self.obstacles = [Node(x - self.x_min_world, y - self.y_min_world)
6565
for x, y in zip(ox, oy)]
66-
self.obstacles_xy = np.array(
67-
[[obstacle.x, obstacle.y] for obstacle in self.obstacles]
68-
)
66+
self.obstacles_xy = {(obstacle.x, obstacle.y) for obstacle in self.obstacles}
6967
self.start = Node(0, 0)
7068
self.goal = Node(0, 0)
7169
self.U = list() # type: ignore
7270
self.km = 0.0
7371
self.kold = 0.0
7472
self.rhs = self.create_grid(float("inf"))
7573
self.g = self.create_grid(float("inf"))
76-
self.detected_obstacles_xy = np.empty((0, 2))
74+
self.detected_obstacles_xy: set[tuple[int, int]] = set()
7775
self.xy = np.empty((0, 2))
7876
if show_animation:
7977
self.detected_obstacles_for_plotting_x = list() # type: ignore
@@ -84,18 +82,8 @@ def create_grid(self, val: float):
8482
return np.full((self.x_max, self.y_max), val)
8583

8684
def is_obstacle(self, node: Node):
87-
x = np.array([node.x])
88-
y = np.array([node.y])
89-
obstacle_x_equal = self.obstacles_xy[:, 0] == x
90-
obstacle_y_equal = self.obstacles_xy[:, 1] == y
91-
is_in_obstacles = (obstacle_x_equal & obstacle_y_equal).any()
92-
93-
is_in_detected_obstacles = False
94-
if self.detected_obstacles_xy.shape[0] > 0:
95-
is_x_equal = self.detected_obstacles_xy[:, 0] == x
96-
is_y_equal = self.detected_obstacles_xy[:, 1] == y
97-
is_in_detected_obstacles = (is_x_equal & is_y_equal).any()
98-
85+
is_in_obstacles = (node.x, node.y) in self.obstacles_xy
86+
is_in_detected_obstacles = (node.x, node.y) in self.detected_obstacles_xy
9987
return is_in_obstacles or is_in_detected_obstacles
10088

10189
def c(self, node1: Node, node2: Node):
@@ -157,7 +145,7 @@ def initialize(self, start: Node, goal: Node):
157145
self.g = self.create_grid(math.inf)
158146
self.rhs[self.goal.x][self.goal.y] = 0
159147
self.U.append((self.goal, self.calculate_key(self.goal)))
160-
self.detected_obstacles_xy = np.empty((0, 2))
148+
self.detected_obstacles_xy = set()
161149

162150
def update_vertex(self, u: Node):
163151
if not compare_coordinates(u, self.goal):
@@ -215,12 +203,7 @@ def detect_changes(self):
215203
compare_coordinates(spoofed_obstacle, self.goal):
216204
continue
217205
changed_vertices.append(spoofed_obstacle)
218-
self.detected_obstacles_xy = np.concatenate(
219-
(
220-
self.detected_obstacles_xy,
221-
[[spoofed_obstacle.x, spoofed_obstacle.y]]
222-
)
223-
)
206+
self.detected_obstacles_xy.add((spoofed_obstacle.x, spoofed_obstacle.y))
224207
if show_animation:
225208
self.detected_obstacles_for_plotting_x.append(
226209
spoofed_obstacle.x + self.x_min_world)
@@ -241,12 +224,7 @@ def detect_changes(self):
241224
compare_coordinates(new_obs, self.goal):
242225
return changed_vertices
243226
changed_vertices.append(Node(x, y))
244-
self.detected_obstacles_xy = np.concatenate(
245-
(
246-
self.detected_obstacles_xy,
247-
[[x, y]]
248-
)
249-
)
227+
self.detected_obstacles_xy.add((x, y))
250228
if show_animation:
251229
self.detected_obstacles_for_plotting_x.append(x +
252230
self.x_min_world)

0 commit comments

Comments
 (0)