Skip to content

Improve speed and readability of is_obstacle in DStarLite #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions PathPlanning/DStarLite/d_star_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ def __init__(self, ox: list, oy: list):
self.y_max = int(abs(max(oy) - self.y_min_world))
self.obstacles = [Node(x - self.x_min_world, y - self.y_min_world)
for x, y in zip(ox, oy)]
self.obstacles_xy = np.array(
[[obstacle.x, obstacle.y] for obstacle in self.obstacles]
)
self.obstacles_xy = {(obstacle.x, obstacle.y) for obstacle in self.obstacles}
self.start = Node(0, 0)
self.goal = Node(0, 0)
self.U = list() # type: ignore
self.km = 0.0
self.kold = 0.0
self.rhs = self.create_grid(float("inf"))
self.g = self.create_grid(float("inf"))
self.detected_obstacles_xy = np.empty((0, 2))
self.detected_obstacles_xy: set[tuple[int, int]] = set()
self.xy = np.empty((0, 2))
if show_animation:
self.detected_obstacles_for_plotting_x = list() # type: ignore
Expand All @@ -84,18 +82,8 @@ def create_grid(self, val: float):
return np.full((self.x_max, self.y_max), val)

def is_obstacle(self, node: Node):
x = np.array([node.x])
y = np.array([node.y])
obstacle_x_equal = self.obstacles_xy[:, 0] == x
obstacle_y_equal = self.obstacles_xy[:, 1] == y
is_in_obstacles = (obstacle_x_equal & obstacle_y_equal).any()

is_in_detected_obstacles = False
if self.detected_obstacles_xy.shape[0] > 0:
is_x_equal = self.detected_obstacles_xy[:, 0] == x
is_y_equal = self.detected_obstacles_xy[:, 1] == y
is_in_detected_obstacles = (is_x_equal & is_y_equal).any()

is_in_obstacles = (node.x, node.y) in self.obstacles_xy
is_in_detected_obstacles = (node.x, node.y) in self.detected_obstacles_xy
return is_in_obstacles or is_in_detected_obstacles

def c(self, node1: Node, node2: Node):
Expand Down Expand Up @@ -157,7 +145,7 @@ def initialize(self, start: Node, goal: Node):
self.g = self.create_grid(math.inf)
self.rhs[self.goal.x][self.goal.y] = 0
self.U.append((self.goal, self.calculate_key(self.goal)))
self.detected_obstacles_xy = np.empty((0, 2))
self.detected_obstacles_xy = set()

def update_vertex(self, u: Node):
if not compare_coordinates(u, self.goal):
Expand Down Expand Up @@ -215,12 +203,7 @@ def detect_changes(self):
compare_coordinates(spoofed_obstacle, self.goal):
continue
changed_vertices.append(spoofed_obstacle)
self.detected_obstacles_xy = np.concatenate(
(
self.detected_obstacles_xy,
[[spoofed_obstacle.x, spoofed_obstacle.y]]
)
)
self.detected_obstacles_xy.add((spoofed_obstacle.x, spoofed_obstacle.y))
if show_animation:
self.detected_obstacles_for_plotting_x.append(
spoofed_obstacle.x + self.x_min_world)
Expand All @@ -241,12 +224,7 @@ def detect_changes(self):
compare_coordinates(new_obs, self.goal):
return changed_vertices
changed_vertices.append(Node(x, y))
self.detected_obstacles_xy = np.concatenate(
(
self.detected_obstacles_xy,
[[x, y]]
)
)
self.detected_obstacles_xy.add((x, y))
if show_animation:
self.detected_obstacles_for_plotting_x.append(x +
self.x_min_world)
Expand Down