Repository navigation
Fix #3543: VoronoiGrid no longer overwrites explicit capacity with area function - #3544
Conversation
…h area function
|
Performance benchmarks:
|
| 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. |
There was a problem hiding this comment.
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.
…aise if both given
|
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. |
| self.voronoi_coordinates = None | ||
| self.capacity_function = capacity_function | ||
|
|
||
| if capacity is not None and capacity_function is not round_float: |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
- The fix logic in voronoi.py makes sense — using explicit capacity when provided and falling back to capacity_function when capacity is None.
- 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.
- Tests are thorough — covering explicit capacity, None capacity, runtime enforcement, custom functions, and the conflict case. Good coverage.
- Agree with @quaquel's suggestion to consider making capacity_function default to None instead of round_float for cleaner logic.
|
|
Fixed AttributeError by moving |
| random: Random | None = None, | ||
| cell_klass: type[Cell] = Cell, | ||
| capacity_function: callable = round_float, | ||
| capacity_function: Callable | None = None, |
There was a problem hiding this comment.
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.
| # --------------------------------------------------------------------------- | ||
| # VoronoiGrid capacity fix — Issue #3543 | ||
| # --------------------------------------------------------------------------- |
There was a problem hiding this comment.
| # --------------------------------------------------------------------------- | |
| # VoronoiGrid capacity fix — Issue #3543 | |
| # --------------------------------------------------------------------------- |
just remove this.
|
@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. |
…h area function (mesa#3544)
…h area function (mesa#3544)
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.
Summary
Closes #3543
VoronoiGrid._build_cell_polygonswas unconditionally overwriting everycell's
capacitywith a value derived from polygon area viacapacity_function, silently discarding any explicitcapacityargumentpassed by the user to
__init__.Root cause
Fix
Single conditional guard — one line change:
When
capacityis explicitly provided, it is preserved. Whencapacity=None(the default),capacity_functionis applied as before —no change to existing behaviour.
Tests
Added
tests/discrete_space/test_voronoi_capacity.pywith 5 tests:capacity=5preserved across all cellscapacity=Nonestill produces area-derived int capacitiesCellFullExceptionfires at runtime with explicitcapacity=1capacity_functionis ignored when capacity is explicitround_floatproduces non-negative int capacities