Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell_space: Allow CellCollection to be empty #2502

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Nov 13, 2024

Sometimes a CellCollection needs to be empty, especially after filtering with .select(). However, the code would raise a StopIteration error trying to get capacity from the first cell. This PR handles empty collections gracefully.

Summary

Fix StopIteration error when creating empty CellCollections by allowing capacity to be None when there are no cells.

Bug / Issue

Creating a CellCollection with no cells (after filtering) raises StopIteration:

# This raises StopIteration if no cells meet the condition
cells = neighbors.select(lambda cell: not any(isinstance(obj, Wolf) for obj in cell.agents))

Implementation

Check for empty collection before accessing first cell's capacity:

self._capacity: int | None = (
    next(iter(self._cells.keys())).capacity if self._cells else None
)

Testing

  • Verify empty CellCollection creation
  • Test select() with no matching cells
  • Check behavior of empty collection methods

Sometimes it's useful to have any empty cell collection in the cell space, which can happen after selection with no cells that meet the selection requirements.

However, self._capacity would give an error, because there were no cell to derive the capacity from. This PR resolves that error.
@EwoutH EwoutH added bug Release notes label experimental Release notes label labels Nov 13, 2024
Copy link

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.6% [-1.0%, -0.1%] 🔵 -0.3% [-0.4%, -0.1%]
BoltzmannWealth large 🔵 -0.9% [-1.2%, -0.5%] 🔵 -1.0% [-1.2%, -0.8%]
Schelling small 🔵 -1.4% [-1.7%, -1.1%] 🔵 +0.1% [-0.2%, +0.3%]
Schelling large 🔵 -1.2% [-1.4%, -0.9%] 🔵 +0.5% [+0.2%, +1.0%]
WolfSheep small 🔵 +0.0% [-0.2%, +0.3%] 🔵 +0.8% [+0.6%, +0.9%]
WolfSheep large 🔵 +0.1% [-0.5%, +0.8%] 🔵 +2.5% [+1.6%, +3.8%]
BoidFlockers small 🔵 -0.5% [-1.1%, +0.1%] 🔵 +0.3% [-0.6%, +1.3%]
BoidFlockers large 🔵 -0.2% [-1.2%, +0.8%] 🔵 +0.6% [-0.2%, +1.3%]

@EwoutH EwoutH merged commit e8b64cc into projectmesa:main Nov 13, 2024
12 of 14 checks passed
EwoutH added a commit that referenced this pull request Nov 13, 2024
Sometimes it's useful to have any empty cell collection in the cell space, which can happen after selection with no cells that meet the selection requirements.

However, self._capacity would give an error, because there were no cell to derive the capacity from. This PR resolves that error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Release notes label experimental Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants