ContinuousSpaceAgent accepts positions with incorrect dimensionality
Describe the bug ContinuousSpaceAgent.position accepts positions with an incorrect number of dimensions. For example, in a 2D ContinuousSpace, assigning a one-dimensional position such as np.array([5.0]) does not raise a ValueError.
This happens because the current bounds validation allows NumPy broadcasting, so an invalid position can pass validation and be assigned to the agent.
Expected behavior The position assigned to an agent should have exactly the same number of dimensions as the ContinuousSpace.
For a 2D space, assigning a position with one or three coordinates should raise a ValueError with a clear message indicating the expected number of dimensions.
To Reproduce
import numpy as np
import pytest
from mesa import Model
from mesa.space import ContinuousSpace, ContinuousSpaceAgent
model = Model(rng=42)
space = ContinuousSpace([[0, 10], [0, 10]], random=model.random)
agent = ContinuousSpaceAgent(space, model)
# This should raise ValueError because the space is 2D,
# but the provided position has only one coordinate.
agent.position = np.array([5.0])
Additional context I discovered this issue while reviewing the ContinuousSpaceAgent.position setter in Mesa, rather than while debugging a specific simulation model.
The behavior can be reproduced using mesa.experimental.continuous_space:
model = Model(rng=42)
space = ContinuousSpace([[0, 10], [0, 10]], random=model.random)
agent = ContinuousSpaceAgent(space, model)
agent.position = np.array([5.0])
In a 2D space, the one-dimensional input [5.0] is silently broadcast to [5.0, 5.0] instead of raising a ValueError.
I haven't identified a specific real-world model affected by this behavior. However, silently accepting malformed position data can place agents at unintended coordinates and potentially affect subsequent spatial calculations, including distance and neighborhood queries.
The proposed fix validates the position dimensionality before bounds checking and assignment.
hey @quaquel , Could you take a look and share your thoughts on this issue?
I took a quick look at ContinuousSpace and ContinuousSpaceAgent on main, and the issue seems to come from NumPy broadcasting in the current validation path.
In a 2D space, ContinuousSpace.in_bounds() does element-wise comparisons like:
(np.asanyarray(point) >= self.dimensions[:, 0]) & (point <= self.dimensions[:, 1])
Because NumPy broadcasting is allowed here, malformed inputs can slip through or produce inconsistent errors when their dimensionality is wrong:
np.array([5.0]) (shape (1,)) or scalar 5.0 (shape ()) can broadcast against (2,) and pass in_bounds(). self.space.agent_positions[self._mesa_index] = value then broadcasts again, so the agent ends up with [5.0, 5.0] instead of raising an error.
np.array([[5.0, 2.0]]) (shape (1, 2)) passes in_bounds() via broadcasting and then fails during row assignment with a generic NumPy broadcast error.
np.array([5.0, 2.0, 1.0]) (shape (3,)) raises a generic NumPy broadcasting error during comparison rather than a clear dimensionality error.
This also matters for the torus path. In ContinuousSpaceAgent.position, when torus=True, an out-of-bounds point falls through to torus_correct(), which does:
self.dimensions[:, 0] + np.mod(np.asanyarray(point) - self.dimensions[:, 0], self.size)
That means a malformed 1D input can also be silently wrapped into a 2D coordinate instead of failing immediately with a clear message.
Validating the shape before running any bounds or torus logic would prevent this, e.g.:
point = np.asanyarray(value)
if point.shape != (self.space.ndims,):
raise ValueError(
f"Expected position with {self.space.ndims} dimensions, got shape {point.shape}"
)
The most direct place for this check is ContinuousSpaceAgent.position, though @quaquel may also want to weigh in on whether ContinuousSpace.in_bounds() / torus_correct() should guard against mismatched shapes for direct callers as well.
This would make the error explicit and consistent across invalid inputs like (1,), (1, 2), scalar values, and 3-element arrays.