Skip to content

Fix #3543: VoronoiGrid no longer overwrites explicit capacity with area function - #3544

Merged
quaquel merged 10 commits into
mesa:mainfrom
DipayanDasgupta:fix/voronoi-capacity-overwrite
Mar 16, 2026
Merged

quaquel merged 10 commits into
mesa:mainfrom
DipayanDasgupta:fix/voronoi-capacity-overwrite

Conversation

@DipayanDasgupta

Copy link
Copy Markdown
Contributor

Summary

Closes #3543

VoronoiGrid._build_cell_polygons was unconditionally overwriting every
cell's capacity with a value derived from polygon area via
capacity_function, silently discarding any explicit capacity argument
passed by the user to __init__.

Root cause

# Before — always overwrites, even when user passed capacity=1:
self._cells[region].capacity = self.capacity_function(polygon_area)

Fix

Single conditional guard — one line change:

if self.capacity is None:
    self._cells[region].capacity = self.capacity_function(polygon_area)

When capacity is explicitly provided, it is preserved. When
capacity=None (the default), capacity_function is applied as before —
no change to existing behaviour.

Tests

Added tests/discrete_space/test_voronoi_capacity.py with 5 tests:

  • Explicit capacity=5 preserved across all cells
  • capacity=None still produces area-derived int capacities
  • CellFullException fires at runtime with explicit capacity=1
  • Custom capacity_function is ignored when capacity is explicit
  • Default round_float produces non-negative int capacities

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.7% [-1.2%, -0.2%] 🔵 -1.8% [-2.1%, -1.4%]
BoltzmannWealth large 🔵 -1.4% [-2.0%, -0.7%] 🔵 -3.1% [-3.7%, -2.6%]
Schelling small 🔵 -2.4% [-2.8%, -1.9%] 🔵 +0.0% [-0.7%, +0.8%]
Schelling large 🔵 +0.0% [-0.6%, +0.7%] 🔵 +1.0% [-0.3%, +2.3%]
WolfSheep small 🔵 +0.7% [+0.2%, +1.1%] 🔵 +4.3% [+0.5%, +8.0%]
WolfSheep large 🔵 +1.7% [+0.8%, +2.5%] 🔵 +3.4% [+1.7%, +5.1%]
SugarscapeG1mt small 🔵 +0.3% [-0.5%, +1.0%] 🔵 -0.5% [-2.2%, +1.2%]
SugarscapeG1mt large 🔵 -0.2% [-1.4%, +1.4%] 🔵 +2.1% [+0.8%, +3.8%]
BoidFlockers small 🔵 +0.5% [+0.3%, +0.7%] 🔵 +0.5% [+0.1%, +0.9%]
BoidFlockers large 🔵 +1.2% [+0.9%, +1.5%] 🔵 +0.5% [+0.2%, +0.8%]

@quaquel quaquel added bug Release notes label backport-candidate PRs we might want to backport to an earlier branch labels Mar 15, 2026
Comment thread mesa/discrete_space/voronoi.py Outdated
polygon_area = self._compute_polygon_area(polygon)
self._cells[region].properties["area"] = polygon_area
self._cells[region].capacity = self.capacity_function(polygon_area)
# Fix: only derive capacity from area when the user did NOT supply one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I disagree with this fix. None can also mean that there is no capacity. Rather, use capacity if provided or None. Only switch to capacity function if specified and capacity is None. If both are provided raise an appropriate exception.

@DipayanDasgupta

Copy link
Copy Markdown
Contributor Author

Thanks for the clarification @quaquel — updated. Now: uses capacity directly if provided, applies capacity_function only when capacity=None, and raises ValueError if both a non-default capacity_function and an explicit capacity are passed simultaneously.

Comment thread mesa/discrete_space/voronoi.py Outdated
self.voronoi_coordinates = None
self.capacity_function = capacity_function

if capacity is not None and capacity_function is not round_float:

@quaquel quaquel Mar 16, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not correct, I now cannot pass my own callable.

Also, I am wondering whether we should change the default behavior of this class. That is, in __init__, change capacity_function to be None. The default round_found function makes no sense to me.

@B2prakash B2prakash left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  1. The fix logic in voronoi.py makes sense — using explicit capacity when provided and falling back to capacity_function when capacity is None.
  2. As @quaquel pointed out, the check capacity_function is not round_float is too restrictive — it blocks custom callables even when no explicit capacity is passed. This needs rework.
  3. Tests are thorough — covering explicit capacity, None capacity, runtime enforcement, custom functions, and the conflict case. Good coverage.
  4. Agree with @quaquel's suggestion to consider making capacity_function default to None instead of round_float for cleaner logic.

@quaquel

quaquel commented Mar 16, 2026

Copy link
Copy Markdown
Member
  1. Do not add new test files; find the relevant tests in the appropriate file and update/expand those.
  2. Tests are currently failing. Please ensure all tests pass.

@DipayanDasgupta

DipayanDasgupta commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@quaquel

Fixed AttributeError by moving self.capacity_function = ... before _validate_parameters()
Replaced callable | None → Callable | None (fixes windows 3.14 CI)
Moved all Voronoi capacity tests into test_discrete_space.py (deleted separate file)
Default is now capacity_function: Callable | None = None
Ruff + pre-commit clean
Ready for another look whenever you have time, thank you for your reviews and feedback.

Comment thread mesa/discrete_space/voronoi.py Outdated
random: Random | None = None,
cell_klass: type[Cell] = Cell,
capacity_function: callable = round_float,
capacity_function: Callable | None = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sorry, one more thought. What about simplifying it even further. Let capacity take an interger, a callable or None and just drop the function. Makes stuff even simpler. But does require an update of the docstring.

Comment on lines +1257 to +1259
# ---------------------------------------------------------------------------
# VoronoiGrid capacity fix — Issue #3543
# ---------------------------------------------------------------------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# ---------------------------------------------------------------------------
# VoronoiGrid capacity fix — Issue #3543
# ---------------------------------------------------------------------------

just remove this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@DipayanDasgupta

Copy link
Copy Markdown
Contributor Author

@quaquel — simplified as suggested. capacity now accepts int | Callable | None: pass an int for fixed per-cell limit, a callable for area-derived limits, or None for unlimited. capacity_function parameter removed entirely. Docstring updated.

@quaquel quaquel added breaking Release notes label and removed GSoC: Needs peer review labels Mar 16, 2026
@quaquel
quaquel merged commit 7bade1d into mesa:main Mar 16, 2026
12 checks passed
PietroMondini pushed a commit to PietroMondini/mesa that referenced this pull request Mar 19, 2026
codebreaker32 pushed a commit to codebreaker32/mesa that referenced this pull request Apr 1, 2026
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Backport of the bug fix from #3544 (fixes #3543), adapted for 3.5.x.
VoronoiGrid._build_cell_polygons always overwrote each cell's capacity
with capacity_function(area), discarding an explicit capacity argument.

On main, #3544 also removed the capacity_function keyword and changed the
default to unlimited capacity. Those are breaking changes, so this backport
only adds the guard: area-derived capacity is applied when capacity is None,
and capacity_function and the default behaviour are unchanged. Also fixes
the docstring for capacity and capacity_function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate PRs we might want to backport to an earlier branch breaking Release notes label bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: VoronoiGrid silently overwrites user-provided capacity with area-derived value

4 participants