Skip to content

Use even-r layout for HexGrid cell positions - #3959

Merged
quaquel merged 2 commits into
mesa:mainfrom
Aeshvarya:fix/hexgrid-position-even-r
Nov 15, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
Aeshvarya:fix/hexgrid-position-even-r

Conversation

@Aeshvarya

@Aeshvarya Aeshvarya commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

HexGrid builds cell.position with odd rows shifted right, but connects cells as even-r (even rows shifted). Drawn at cell.position, every cell has connections that aren't touching hexagons. This makes the positions follow the connections.

Bug / Issue

Fixes #3958. Picture and repro are in the issue.

Implementation

One line in HexGrid._init_hex_geometry: the row offset is now 0.5 * ((row + 1) % 2) instead of 0.5 * (row % 2), so even rows are the shifted ones.

_connect_cells_2d is not touched, so neighbours, movement and HexGridMovingAgent behave exactly as before. Only cell.position (and so find_nearest_cell and the hex drawing) changes. The new positions are the same centres the old mesh in mpl_space_drawing.py uses.

Testing

  • test_hex_grid_lookups pinned the odd-r positions (its comment even said "Odd-R"), so I updated the three expected positions to even-r.
  • Added test_hex_grid_positions_match_connections: every pair of touching hexagons (centres sqrt(3) apart) must be connected, for torus and non-torus. It fails on main for all cells and passes here.
  • Full suite passes locally (868 passed, browser viz tests skipped since I don't have playwright set up). ruff format and ruff check are clean.

Additional Notes

  • It doesn't overlap Draw HexGrid agents at the centre of their hexagon #3952, that one fixes where agents are drawn. With both in, hexagons, agents and connections all line up.
  • I used Claude Code while working on this. I checked the geometry myself against the old drawer and the redblobgames even-r layout, and the figure in the issue is generated from the actual grid connections.

@github-actions github-actions Bot added the testing Release notes label label Oct 20, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.9% [+0.2%, +1.5%] 🔵 +1.1% [+0.8%, +1.4%]
BoltzmannWealth large 🔵 +1.3% [+0.3%, +2.4%] 🔵 +5.5% [+1.1%, +10.0%]
Schelling small 🔵 -0.0% [-0.7%, +0.6%] 🔵 +0.2% [-0.7%, +1.1%]
Schelling large 🔵 -0.4% [-0.9%, +0.1%] 🔵 +1.1% [-1.6%, +3.8%]
WolfSheep small 🔵 -2.7% [-3.3%, -2.0%] 🔵 -0.7% [-4.9%, +3.8%]
WolfSheep large 🔵 -1.5% [-2.2%, -0.7%] 🟢 -5.7% [-7.4%, -3.9%]
SugarscapeG1mt small 🔵 -0.5% [-1.3%, +0.3%] 🔵 -1.5% [-3.2%, +0.1%]
SugarscapeG1mt large 🔵 -2.0% [-3.7%, -0.2%] 🔵 -2.4% [-4.7%, +0.1%]
BoidFlockers small 🔵 -0.9% [-1.6%, -0.2%] 🔵 -0.6% [-1.0%, -0.2%]
BoidFlockers large 🔵 +0.0% [-0.3%, +0.3%] 🔵 -1.1% [-1.4%, -0.7%]
MultiLevelAllianceModel small 🔵 -2.1% [-4.1%, -0.1%] 🔵 -0.4% [-0.9%, +0.1%]
MultiLevelAllianceModel large 🔵 -1.2% [-2.0%, -0.3%] 🔵 -1.0% [-2.3%, +0.5%]

@Aeshvarya
Aeshvarya marked this pull request as ready for review October 22, 2026 12:56
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.3% [-0.6%, +1.2%] 🔵 +2.0% [+1.8%, +2.2%]
BoltzmannWealth large 🟢 -4.4% [-5.8%, -3.0%] 🟢 -6.2% [-9.0%, -3.2%]
Schelling small 🔵 -0.4% [-1.2%, +0.5%] 🔵 +0.1% [-0.6%, +1.0%]
Schelling large 🔵 -1.6% [-2.8%, -0.3%] 🔵 -2.7% [-6.2%, +0.5%]
WolfSheep small 🔵 -0.4% [-0.9%, +0.1%] 🔵 +0.8% [-3.7%, +4.8%]
WolfSheep large 🔵 -3.1% [-4.0%, -2.1%] 🟢 -8.0% [-10.3%, -5.0%]
SugarscapeG1mt small 🔵 -2.5% [-3.2%, -1.8%] 🔵 -1.4% [-3.4%, +0.4%]
SugarscapeG1mt large 🔵 -5.0% [-7.5%, -2.1%] 🔵 -1.8% [-3.9%, +0.1%]
BoidFlockers small 🔵 -2.4% [-3.1%, -1.8%] 🔵 -0.6% [-1.2%, -0.2%]
BoidFlockers large 🔵 -0.1% [-1.3%, +1.2%] 🔵 +0.3% [-0.4%, +1.2%]
MultiLevelAllianceModel small 🔵 -2.4% [-4.6%, +0.1%] 🔵 -0.4% [-0.8%, -0.1%]
MultiLevelAllianceModel large 🔵 +2.2% [-0.1%, +4.9%] 🔵 +1.2% [-3.2%, +6.1%]

@ARYAMANN7279

Copy link
Copy Markdown
Contributor

I checked this branch independently, and it looks right to me.

For HexGrids of several sizes, with and without torus, I compared each cell's neighbors with the cells exactly one hexagon spacing away (distance sqrt(3)). For the torus I included the wrapped images, which the test in this PR skips. On main every cell disagreed. With this branch there are no mismatches, and the two files under tests/discrete_space I ran pass.

While doing this I found a separate problem in the same function, find_nearest_cell on a torus near the edges, so I opened #3979 for it. It only changes the line that builds the KD-tree, so it should not conflict with this one.

@Aeshvarya

Copy link
Copy Markdown
Contributor Author

Thanks for checking it, and good catch on the torus case. I tried #3979 on top of this branch: the grid.py changes merge cleanly, and with both in, tests/discrete_space passes (126). The only overlap is that both PRs add a test right after test_hex_grid_lookups, so whichever goes in second needs a small rebase there. Happy to do it on my side if this one is second.

HexGrid connects cells using even-r offsets (even rows shifted right),
as its docstring says, and the hex mesh in mpl_space_drawing uses the
same layout. cell.position was computed with odd rows shifted instead,
so on every cell some connected neighbours were not touching hexagons
when drawn at cell.position.

Shift even rows in _init_hex_geometry so positions agree with the
connections. Connectivity itself is unchanged.
@Aeshvarya
Aeshvarya force-pushed the fix/hexgrid-position-even-r branch from 736447d to 109c11d Compare October 30, 2026 15:31
@Aeshvarya

Copy link
Copy Markdown
Contributor Author

Rebased onto current main, no conflicts. The earlier examples failure was mesa-examples importing mesa.meta_agents, which had moved; it passes now. @ARYAMANN7279's #3979 edits the same spot in the tests, so I'll rebase on my side if this one merges second. Ready for review.

(Disclosure: I used Claude to write this comment.)

@AshNicolus AshNicolus left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checked this locally. On main every cell's cell.position disagrees with its connections (36/36 on 6x6, 30/30 on 6x5, 28/28 on 7x4). With the one-line change it is 0 for all of them, and the hexagons from HexSpaceDrawer still fit inside its plot limits for odd and even heights.

I also stacked my #3952 (agents drawn at cell.position) on top: its hexagon-centre test passes on both backends, and the full suite passes apart from test_hex_grid_lookups, whose old odd-r values this PR updates. So with both in, hexagons, agents and connections line up, as the description says.

On the changed positions: EwoutH's #3940 lists "cell centers change directly, we're in alpha, so no deprecation cycle" as a decision, so this fits that. A line in the migration guide might still help anyone who stored hex positions.

flowchart LR
    A["_connect_cells_2d<br/>even-r: even rows shifted"] --> C{"do positions<br/>match connections?"}
    B["cell.position<br/>main: odd rows shifted"] --> C
    C -->|main| D["no: neighbours drawn<br/>apart, find_nearest_cell off"]
    B2["cell.position<br/>PR: even rows shifted"] --> C2{"match?"}
    A --> C2
    C2 -->|PR| E["yes: hexagons, agents<br/>and connections line up"]
    E --> F["+ #3952:<br/>agents at cell.position"]
Loading

LGTM.

@Aeshvarya

Copy link
Copy Markdown
Contributor Author

Thanks @AshNicolus for checking it locally and for stacking #3952 on top, good to know hexagons, agents and connections line up with both in. I agree a migration guide line would help anyone who stored hex positions. I can add one to docs/migration_guide.md in this PR if the maintainers want it here.

(Disclosure: I used Claude to write this comment.)

@quaquel
quaquel merged commit e1ca9ea into mesa:main Nov 15, 2026
14 of 15 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
HexGrid connects cells using even-r offsets (even rows shifted right),
as its docstring says, and the hex mesh in mpl_space_drawing uses the
same layout. cell.position was computed with odd rows shifted instead,
so on every cell some connected neighbours were not touching hexagons
when drawn at cell.position.

Shift even rows in _init_hex_geometry so positions agree with the
connections. Connectivity itself is unchanged.

Co-authored-by: Jan Kwakkel <[email protected]>
(cherry picked from commit e1ca9ea)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HexGrid cell.position uses odd-r layout but connections use even-r

4 participants