Skip to content

Place grids in shared coordinates with cell_size and origin - #4114

Merged
EwoutH merged 2 commits into
mainfrom
grid-coordinates
Nov 15, 2026
Merged

EwoutH merged 2 commits into
mainfrom
grid-coordinates

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Pre-PR Checklist

  • I linked an issue/discussion where a maintainer approved this enhancement/feature for implementation.

Approval Link

#2585 (comment), on the proposal that includes cell_size/origin and cell centers. Split out of #3940, which builds on it.

Summary

Grids take cell_size and origin, so they can share coordinates with a continuous space or with each other. Orthogonal cell positions become cell centers (breaking, see below).

Motive

Agents in one space often need what's in another: a bird asks what's in the ground cell below it, or an agent reads fertility from a coarse grid under positions in metres. With cell_size and origin, a grid covers the same area as a continuous space or another grid, so such questions need no new concept: ground.find_nearest_cell(bird.position). This is the first layer of the smaller direction in #2585, split out of #3940, which builds locations on top of it.

Cell positions become centers because only a center round-trips: a corner sits on a cell boundary, so with cell_size=1/3, find_nearest_cell(cell.position) can land in the neighbouring cell. With centers, find_nearest_cell(cell.position) is cell holds at any cell size.

Implementation

  • cell_size (one number or one per dimension) and origin map cell coordinates to positions. Cell.position of a grid cell is its center, computed on first use and read-only; cell_size and origin are read-only too.
  • A bounded grid includes its upper edge, like ContinuousSpace, also when rounding in a cell size such as 1 / 49 puts the computed edge just below it.
  • Unpickled grids keep their mapping. find_nearest_cell floors plain floats instead of a numpy array.
  • Visualization keeps drawing orthogonal grids in cell coordinates.

Usage Examples

model.air = ContinuousSpace([[0, 100], [0, 100]], random=model.random)
model.ground = OrthogonalMooreGrid((50, 50), cell_size=2.0, random=model.random)   # same area, 2 by 2 cells
model.regions = OrthogonalMooreGrid((5, 5), cell_size=20.0, random=model.random)   # same area, coarse

model.ground[10, 20].position                             # array([21., 41.]), the cell center
model.ground.find_nearest_cell([21.3, 40.2])              # model.ground[10, 20]
model.regions.find_nearest_cell([21.3, 40.2]).fertility   # data from the coarse grid

Additional Notes

Breaking change. Cell.position of an orthogonal grid cell is the cell center, so find_nearest_cell(cell.position) is cell holds at any cell_size:

OrthogonalMooreGrid((10, 10), random=rng)[5, 5].position   # array([5.5, 5.5]); was array([5., 5.])

It's in the migration guide, without a deprecation cycle since 4.0 is in alpha. AshNicolus checked that none of the 14 mesa-examples models on an orthogonal grid read cell.position; in Mesa itself only the visualization did, and it now uses cell.coordinate.

Grid construction, BoltzmannWealth and Schelling run times are unchanged within noise (same seeds, alternating main and branch).

Assisted by Claude Opus 5.5.

Orthogonal grids take cell_size and origin, so a grid can cover the same
area as a ContinuousSpace or another grid at a different resolution.
Queries across spaces then need no extra machinery:
ground.find_nearest_cell(bird.position) finds the cell below a bird.

- Cell.position of a grid cell is its center, computed on first use and
  read-only. A corner sits on a cell boundary, so only a center
  round-trips through find_nearest_cell at every cell size.
- A bounded grid includes its upper edge, like ContinuousSpace, so a
  point valid in a continuous space of the same extent has a cell, also
  when rounding in a cell size such as 1 / 49 puts the computed edge
  just below it.
- Unpickled grids keep their cell_size and origin.
- find_nearest_cell floors plain floats instead of a numpy array.
- Visualization keeps drawing orthogonal grids in cell coordinates.

Breaking: Cell.position of an orthogonal grid cell moves from the corner
to the center. Documented in the migration guide.

Assisted-by: Claude Opus 5.5
@github-actions github-actions Bot added docs Release notes label testing Release notes label visualization labels Nov 15, 2026
@github-actions

This comment was marked as off-topic.

@EwoutH
EwoutH marked this pull request as ready for review November 15, 2026 16:16
@EwoutH EwoutH added the breaking Release notes label label Nov 15, 2026
@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Since the cell center part is breaking, I split it out from #3940 so that it doesn't disappear in noise and we can refer to it explictly in the migration guide and release notes.

This was already the first commit of #3940 for a long time, nothing new. If there are any points or feedback let me know, otherwise I will merge tomorrow.

@mesa/maintainers

@EwoutH EwoutH added enhancement Release notes label and removed docs Release notes label testing Release notes label labels Nov 15, 2026
@github-actions

This comment was marked as off-topic.

@codebreaker32 codebreaker32 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Centers computed as coordinate + 0.5 is what I originally proposed in #3268, so glad to see it land.
One note for the follow-up: the renderers now special-case orthogonal grids and draw from cell.coordinate, so a grid with cell_size=2 is still drawn in cell units. Fine for now, but drawing a grid and a continuous space together will need the drawer to work in positions instead.

Comment thread mesa/discrete_space/grid.py Resolved
Comment thread mesa/discrete_space/grid.py Resolved
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
find_nearest_cell reads the mapping from plain lists, while cell centers
use these arrays. Changing them in place, as in grid.origin[:] = 10,
moved the centers of unread cells but not the lookup, so a cell's own
center could fall outside the grid. Both arrays are now read-only, also
after unpickling, which doesn't keep the flag. They are also properties
without a setter, so assigning a new array raises instead of being
silently ignored.

cell_size now also accepts numpy scalars such as np.int64, which failed
with "object is not iterable".

Assisted-by: Claude Opus 5.5
@github-actions github-actions Bot added docs Release notes label testing Release notes label labels Nov 15, 2026
@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

@quaquel you're ok?

@quaquel

quaquel commented Nov 15, 2026

Copy link
Copy Markdown
Member

I quickly looked through the code. I am at an airport right now and aviation is still a mess in the US, so no time for a thorough review. I am fine with merging this.

@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Thanks, appreciated.

Productive day though, 10% of the lifetime merged PRs of the EMAworkbench in a single day!

Safe travels!

@EwoutH
EwoutH merged commit 460416b into main Nov 15, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Release notes label docs Release notes label enhancement Release notes label testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants