Skip to content

fix: validate ContinuousSpaceAgent position dimensions - #4126

Merged
quaquel merged 1 commit into
mesa:mainfrom
sangitabera:continuous-space-agent-position-dimensions-clean
Nov 20, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
sangitabera:continuous-space-agent-position-dimensions-clean

Conversation

@sangitabera

@sangitabera sangitabera commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Fixes #4115 .

This PR fixes a validation issue in ContinuousSpaceAgent.position where positions with an incorrect number of dimensions could be accepted due to NumPy broadcasting.

Bug / Issue

Previously, assigning a position with fewer dimensions than the ContinuousSpace allowed NumPy broadcasting to occur during bounds checking and assignment.

For example, assigning np.array([5.0]) to an agent in a 2D space did not raise a ValueError, even though the position should contain two coordinates.

Implementation

Added an explicit position shape validation in the ContinuousSpaceAgent.position setter.

  • The setter now checks that the position shape matches (self.space.ndims,).
  • Raises a ValueError when the supplied position has an incorrect number of dimensions.
  • Uses the validated NumPy array for bounds checking, torus correction, and assignment.

Testing

  • Creates a 2D ContinuousSpace.
  • Creates a ContinuousSpaceAgent.
  • Attempts to assign a one-dimensional position using np.array([5.0]).
  • Verifies that a ValueError is raised.

Also ran the relevant ContinuousSpace test suite to check for regressions.

Additional Notes

This is a focused validation fix and does not change the existing bounds-checking or torus behavior for valid positions.

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 16, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -8.0% [-9.1%, -7.0%] 🔵 +0.2% [-0.3%, +0.8%]
BoltzmannWealth large 🟢 -5.8% [-7.1%, -4.5%] 🟢 -21.3% [-25.4%, -16.8%]
Schelling small 🟢 -5.1% [-6.0%, -4.2%] 🔵 -1.4% [-2.5%, -0.4%]
Schelling large 🔵 -0.4% [-2.1%, +1.3%] 🔵 -5.4% [-10.2%, -0.6%]
WolfSheep small 🟢 -6.6% [-7.8%, -5.3%] 🔵 -5.7% [-9.9%, -1.3%]
WolfSheep large 🔵 -4.4% [-7.3%, -1.7%] 🔵 -6.7% [-11.8%, -2.4%]
SugarscapeG1mt small 🟢 -16.3% [-18.9%, -13.7%] 🟢 -8.6% [-10.1%, -7.0%]
SugarscapeG1mt large 🟢 -16.7% [-18.4%, -14.8%] 🟢 -8.6% [-11.0%, -6.1%]
BoidFlockers small 🟢 -8.8% [-9.7%, -7.8%] 🔵 -0.5% [-1.2%, +0.2%]
BoidFlockers large 🟢 -5.8% [-6.2%, -5.3%] 🔵 -0.8% [-1.6%, -0.1%]
MultiLevelAllianceModel small 🟢 -14.9% [-16.3%, -13.4%] 🟢 -4.8% [-5.7%, -3.9%]
MultiLevelAllianceModel large 🟢 -12.7% [-14.6%, -10.4%] 🟢 -30.3% [-31.6%, -28.7%]

@mesa-triage-bot

Copy link
Copy Markdown

Thanks for adding the dimensionality check and regression test, @sangitabera. I reproduced the silent conversion of [5.0] to [5.0, 5.0] on main using mesa.experimental.continuous_space; neither this PR nor #4115 explains how this arose in a real model.

Please add that context to #4115: what model or workflow produced the malformed position, and how the silent broadcasting affected it. That will establish the practical impact needed for review.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@sangitabera

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I've added an additional context comment to issue #4115 explaining how I discovered the issue, the reproducible example, and its potential impact.

I haven't encountered this in a specific real-world model, so I've made that distinction clear.

@mesa-triage-bot

Copy link
Copy Markdown

Thanks for clarifying how you found this, @sangitabera.

@quaquel, #4115 now confirms this came from code inspection, with no affected real model identified. Should this small validation change proceed on the reproducer alone, or wait for demonstrated modelling impact? I'd wait for that evidence, following the developer guide's problem alignment guidance.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@sangitabera

Copy link
Copy Markdown
Contributor Author

hey @quaquel could you please review this PR when you have time ?

@sangitabera

Copy link
Copy Markdown
Contributor Author

@quaquel Thank you for reviewing and approving my PR! All checks are passing. Could you please merge it when convenient?

@quaquel
quaquel merged commit b70fbfe into mesa:main Nov 20, 2026
18 checks passed
@sangitabera
sangitabera deleted the continuous-space-agent-position-dimensions-clean branch November 22, 2026 03:05
EwoutH pushed a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
EwoutH added a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
Add mesa#3913, mesa#4061, mesa#4072, mesa#4105, mesa#4126, mesa#4136, mesa#4141, mesa#4157, mesa#4161 and the Starlette 1.x support backported from mesa#4007, and move the release date to 2026-11-22.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ContinuousSpaceAgent accepts positions with incorrect dimensionality

2 participants