AtsushiSakai/PythonRobotics · error · Exception

Number of obstacles is greater than grid size!

Error message

Number of obstacles is greater than grid size!

What it means

Raised in GridWithDynamicObstacles.__init__ when num_obstacles exceeds the total number of cells in the grid (grid_size[0] * grid_size[1]). The grid cannot physically hold more obstacles than it has cells, so construction fails fast rather than generating an invalid scenario.

Source

Thrown at PathPlanning/TimeBasedPathPlanning/GridWithDynamicObstacles.py:62

    # Logging control
    verbose = False

    def __init__(
        self,
        grid_size: np.ndarray,
        num_obstacles: int = 40,
        obstacle_avoid_points: list[Position] = [],
        obstacle_arrangement: ObstacleArrangement = ObstacleArrangement.RANDOM,
        time_limit: int = 100,
    ):
        self.obstacle_avoid_points = obstacle_avoid_points
        self.time_limit = time_limit
        self.grid_size = grid_size
        self.reservation_matrix = np.zeros((grid_size[0], grid_size[1], self.time_limit))

        if num_obstacles > self.grid_size[0] * self.grid_size[1]:
            raise Exception("Number of obstacles is greater than grid size!")

        if obstacle_arrangement == ObstacleArrangement.RANDOM:
            self.obstacle_paths = self.generate_dynamic_obstacles(num_obstacles)
        elif obstacle_arrangement == ObstacleArrangement.ARRANGEMENT1:
            self.obstacle_paths = self.obstacle_arrangement_1(num_obstacles)
        elif obstacle_arrangement == ObstacleArrangement.NARROW_CORRIDOR:
            self.obstacle_paths = self.generate_narrow_corridor_obstacles(num_obstacles)

        for i, path in enumerate(self.obstacle_paths):
            obs_idx = i + 1  # avoid using 0 - that indicates free space in the grid
            for t, position in enumerate(path):
                # Reserve old & new position at this time step
                if t > 0:
                    self.reservation_matrix[path[t - 1].x, path[t - 1].y, t] = obs_idx
                self.reservation_matrix[position.x, position.y, t] = obs_idx

    """
    Generate dynamic obstacles that move around the grid. Initial positions and movements are random

View on GitHub (pinned to 1fe4fb980f)

Solutions

  1. Reduce num_obstacles to at most grid_size[0] * grid_size[1]
  2. Or increase grid_size dimensions to accommodate the obstacle count
  3. Add validation/clamping at the call site (e.g. num_obstacles = min(num_obstacles, grid_size[0]*grid_size[1])) before constructing the grid

Example fix

# before
grid = GridWithDynamicObstacles(grid_size=(10, 10), num_obstacles=150, ...)

# after
grid = GridWithDynamicObstacles(grid_size=(10, 10), num_obstacles=min(150, 10*10), ...)
Defensive patterns

Strategy: validation

Validate before calling

max_cells = grid_size[0] * grid_size[1]
assert num_obstacles <= max_cells, f"num_obstacles ({num_obstacles}) must be <= {max_cells}"

Prevention

When it happens

Trigger: Constructing GridWithDynamicObstacles with num_obstacles greater than grid_size[0]*grid_size[1], e.g. a 10x10 grid (100 cells) with num_obstacles=150.

Common situations: Scaling up obstacle counts for stress tests without scaling grid_size; reading num_obstacles from a config/CLI arg without clamping; randomly generated scenarios with unbounded obstacle counts.

Related errors


AI-assisted analysis of AtsushiSakai/PythonRobotics@1fe4fb980f (2026-08-28). Data as JSON: /api/errors/9df0952325384c10. Report an issue: GitHub.