Repository navigation
Conversation
A Frame is a coordinate system that one or more spaces index. An agent has at most one location per frame, and a write through any member space updates the agent's other spaces in that frame once, or none of them if one rejects the move. This is a first implementation of the design in discussion mesa#2585. - Add mesa.experimental.frames with Frame and the Location descriptor. - Every space takes frame=; without it a space gets a frame of its own and single-space agents keep their existing code path. - Discrete spaces get add, remove, move and cell_of; ContinuousSpace gets add and remove for agents that are not ContinuousSpaceAgents. - Orthogonal grids get cell_size and origin to place them in a frame. Breaking changes, documented in the migration guide: - Cell.position of an orthogonal grid cell is the cell center. - ContinuousSpaceAgent.position is a read-only view. Also restore shared state after unpickling: grid cells get one cell class again, and ContinuousSpace retakes its array views. Co-Authored-By: Claude Haiku 5.5 <[email protected]>
|
Performance benchmarks:
|
Member
Author
|
Overcomplicated, not the direction I want to go. See #2585 (comment). |
This was referenced Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A first implementation of the frames and locations design from #2585 (proposal). This is a pathfinding PR: it is meant to test the design with working code and numbers, not to be merged as is.
What it does
A
Frameis a coordinate system that one or more spaces index. An agent has at most one location per frame. A write through any member space is converted to frame coordinates and applied to the agent's other spaces in that frame once, or to none of them if one rejects it.mesa.experimental.framesholdsFrameand theLocationdescriptor.frame=. Without it, a space gets a frame of its own, andCellAgent.cellandContinuousSpaceAgent.positionkeep their existing code path with one extrais not Nonecheck.add,remove,moveandcell_of.ContinuousSpacegetsaddandremovefor agents that are notContinuousSpaceAgents.cell_sizeandorigin. The mapping is a scale and offset behind two private functions, so it can become an affine transform.ground.add(boid)for aContinuousSpaceAgent,air.add(agent)for aCellAgent.Benchmarks
Single-space performance was the gate.
benchmarks/global_benchmark.py, main against this branch, two runs each in ABBA order on an idle laptop (Windows 11, Python 3.14), pooled and bootstrapped as incompare_timings.py:Positive is slower. By the script's own rule (an interval entirely beyond 3%), no configuration changed in init or run time.
For scale, two runs of main against each other differ by this much on the same machine:
So the run-time differences above (-5.0% to +2.1%) are inside what this machine produces with no code change. The grid models are neither faster nor slower in any way I can resolve here; CI's benchmark run is the better instrument.
Hot paths, best of 25 repeats, alternating main and branch:
agent.cell = cellagent.cellboid.positionboid.position = p+=on main,= position + stepon branch)OrthogonalMooreGrid((40, 40))OrthogonalMooreGrid((3, 3))ContinuousSpace(...)The one measurable cost on a hot path is the relative move: with a read-only position it allocates a new array, about 0.3 µs or 5% of that one statement. It does not show up in the Boids model. Constructing a space costs 2 to 5 µs more, for the frame.
A write that fans out over two spaces costs about the same as doing the same thing by hand:
boid.position = p, continuous space onlyboid.position = p, also in a grid, same cellboid.position = p, also in a grid, new cellfind_nearest_cell, setcellagent.cell = cell, grid onlyagent.cell = cell, also in a continuous spaceWhere this differs from the proposal
Location("physical")takes a name. A frame holds its spaces, so it is a per-model object created inModel.__init__, whileLocationis declared on the class. A module-levelFrameshared by a class would leak spaces across model instances and replications. The name is resolved when the agent joins a space; nomodel.framesregistry is needed.Location(frame)also works and uses the frame's name.agent.position += vchange the array before anything is validated, so the old value is gone when a move is rejected, andagent.position[0] = xskips the other spaces entirely. That cannot be combined with atomic writes. The cost is writingagent.position = agent.position + v; see the benchmark above.cell_size=1/3the round tripfind_nearest_cell(cell.position)lands in the neighbouring cell.space.cell_of(agent)is the general way to ask for a cell.agent.cellstays, but it refers to one space.What turned out awkward
agent.celldoes not generalize. An agent in a ground grid and a district grid has two cells in one frame.agent.cellnow mirrors one "home" space, and assigning a cell of another space raises.Cellhas no reference to its space, so finding the owner of a cell means asking each space in the frame.Framewould be cleaner.ContinuousSpace.in_boundsincludes the upper bound, grids are half-open, hex and Voronoi grids accept any point. A point exactly on the upper edge is valid in the continuous space and rejected by a grid of the same size.Network. An agent in a network and a continuous space can therefore only move by node.ContinuousSpaceAgenthas no "not placed yet" state. Its row is uninitialized memory until the first assignment, soground.add(boid)before that derives a cell from garbage.ContinuousSpace.agent_positionsis still a writable array. Vectorized updates bypass the other spaces.Gridgave every cell its own class (property layers only worked on the first cell), and unpickling aContinuousSpacedetachedagent_positionsfrom its backing array.cell.coordinateso output is unchanged. Drawing a continuous space over a grid needs it to draw in frame coordinates.Open questions
Frameowntorusand the extent, with spaces inheriting them?Networkon request, instead of always raising?agent.cellacceptable, or shouldagent.cellbe single-space only?mesa.experimental.framesthe right place, given that the stablemesa.discrete_spacenow imports it?add/remove/move/cell_of,Frame.location_of,Frame.move.position = (x, y)against row-first movement inGrid2DMovingAgent) is untouched.Not in this PR: multi-cell membership and geometry,
cell_sizefor hex grids,FixedAgentin shared frames (raisesTypeError), change notification throughmesa_signals, multi-space visualization, and example models.Breaking changes
Both are in
docs/migration_guide.md:Cell.positionof an orthogonal grid cell is the cell center:(5, 5)reports[5.5, 5.5].ContinuousSpaceAgent.positionis read-only;+=raises.Tests
tests/experimental/test_frames.pychecks the core invariant on random sequences of point writes, cell moves, joins, leaves and removals, across four space combinations (torus continuous + two grids; bounded continuous + grid + Voronoi + partial grid; discrete only; hex torus + continuous) and three agent kinds. After every operation each member space must agree with the agent's location, and a rejected operation must leave a full snapshot unchanged. Six deliberately injected bugs (no rollback, no torus wrap, old cell not left, and so on) are all caught by it.Not run locally: the Playwright browser tests in
tests/examples/test_examples_viz.py(no Chromium on my machine). CI will show them.🤖 Generated with Claude Code