Repository navigation
Place grids in shared coordinates with cell_size and origin - #4114
Conversation
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
This comment was marked as off-topic.
This comment was marked as off-topic.
|
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 |
This comment was marked as off-topic.
This comment was marked as off-topic.
codebreaker32
left a comment
There was a problem hiding this comment.
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.
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
dfb541c to
460416b
Compare
|
@quaquel you're ok? |
|
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. |
|
Thanks, appreciated. Productive day though, 10% of the lifetime merged PRs of the EMAworkbench in a single day! Safe travels! |
Pre-PR Checklist
Approval Link
#2585 (comment), on the proposal that includes
cell_size/originand cell centers. Split out of #3940, which builds on it.Summary
Grids take
cell_sizeandorigin, 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_sizeandorigin, 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 cellholds at any cell size.Implementation
cell_size(one number or one per dimension) andoriginmap cell coordinates to positions.Cell.positionof a grid cell is its center, computed on first use and read-only;cell_sizeandoriginare read-only too.ContinuousSpace, also when rounding in a cell size such as1 / 49puts the computed edge just below it.find_nearest_cellfloors plain floats instead of a numpy array.Usage Examples
Additional Notes
Breaking change.
Cell.positionof an orthogonal grid cell is the cell center, sofind_nearest_cell(cell.position) is cellholds at anycell_size: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 usescell.coordinate.Grid construction, BoltzmannWealth and Schelling run times are unchanged within noise (same seeds, alternating main and branch).
Assisted by Claude Opus 5.5.