Skip to content

Commit

Permalink
Fix grid boundaries checks in OccupancyGridObservation
Browse files Browse the repository at this point in the history
Fix #541
  • Loading branch information
eleurent committed Dec 16, 2023
1 parent a897328 commit 7234d6b
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions highway_env/envs/common/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ def observe(self) -> np.ndarray:
)
cell = self.pos_to_index((x, y), relative=not self.absolute)
if (
0 <= cell[1] < self.grid.shape[-2]
and 0 <= cell[0] < self.grid.shape[-1]
0 <= cell[0] < self.grid.shape[-2]
and 0 <= cell[1] < self.grid.shape[-1]
):
self.grid[layer, cell[1], cell[0]] = vehicle[feature]
self.grid[layer, cell[0], cell[1]] = vehicle[feature]
elif feature == "on_road":
self.fill_road_layer_by_lanes(layer)

Expand Down Expand Up @@ -428,17 +428,19 @@ def pos_to_index(self, position: Vector, relative: bool = False) -> Tuple[int, i
self.observer_vehicle.heading
)
position = np.array([[c, s], [-s, c]]) @ position
return int(
np.floor((position[0] - self.grid_size[0, 0]) / self.grid_step[0])
), int(np.floor((position[1] - self.grid_size[1, 0]) / self.grid_step[1]))
return (
int(np.floor((position[0] - self.grid_size[0, 0]) / self.grid_step[0])),
int(np.floor((position[1] - self.grid_size[1, 0]) / self.grid_step[1])),
)

def index_to_pos(self, index: Tuple[int, int]) -> np.ndarray:
position = np.array(
[
(index[1] + 0.5) * self.grid_step[0] + self.grid_size[0, 0],
(index[0] + 0.5) * self.grid_step[1] + self.grid_size[1, 0],
(index[0] + 0.5) * self.grid_step[0] + self.grid_size[0, 0],
(index[1] + 0.5) * self.grid_step[1] + self.grid_size[1, 0],
]
)

if self.align_to_vehicle_axes:
c, s = np.cos(-self.observer_vehicle.heading), np.sin(
-self.observer_vehicle.heading
Expand Down Expand Up @@ -475,10 +477,10 @@ def fill_road_layer_by_lanes(
for waypoint in waypoints:
cell = self.pos_to_index(lane.position(waypoint, 0))
if (
0 <= cell[1] < self.grid.shape[-2]
and 0 <= cell[0] < self.grid.shape[-1]
0 <= cell[0] < self.grid.shape[-2]
and 0 <= cell[1] < self.grid.shape[-1]
):
self.grid[layer_index, cell[1], cell[0]] = 1
self.grid[layer_index, cell[0], cell[1]] = 1

def fill_road_layer_by_cell(self, layer_index) -> None:
"""
Expand Down

0 comments on commit 7234d6b

Please sign in to comment.