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

Better Typing support for (Cell)agents #2072

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

Corvince
Copy link
Contributor

@Corvince Corvince commented Mar 6, 2024

I recently enabled Pyright for my editor, its a python type checker which is much faster then mypy and therefore actually usable. At least thats my experience.

Anyway, my experience with Mesa was very humbling. Take for example this code snippet from the schelling benchmark model

        neighborhood = self.cell.neighborhood(radius=self.radius)
        for neighbor in neighborhood.agents:
            if neighbor.type == self.type:
                similar += 1
            [...]
            self.model.happy += 1

Lets start from bottom to top. The first typechecking error comes from self.model.happy. Since we hardcoded the model to be of type mesa.Model, it doesn't know that model.happy exists and is an integer. This was rather easy to fix with my first commit, where the Agent/CellAgent now can take a generic parameter for the model class. So you just have to define your Agent as

class SchellingAgent(CellAgent["Schelling"])

The next issue was that the ´neighbor.type` was unknown. This was much harder to fix and occupied me for quite a while. There are now two hoops you have to jump through to make it work (if you are determined to enable type checking).
First you have to again add this information to the CellAgent as before

class SchellingAgent(CellAgent["Schelling", "SchellingAgent"])

and you have to define the cell and agent class when you define the OrthogonalGrid like so

        self.grid = OrthogonalMooreGrid(
            [height, width],
            torus=True,
            capacity=1,
            random=self.random,
            cell_klass=Cell[SchellingAgent],
            agent_class=SchellingAgent,
        )

I think this is not really optimal, but so far the only solution I could find. Maybe someone who knows the Python type system better then me can help improve this PR

Copy link

github-actions bot commented Mar 6, 2024

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
Schelling small 🔴 +24.0% [+23.4%, +24.4%] 🔵 -0.3% [-0.7%, +0.1%]
Schelling large 🔴 +23.8% [+22.4%, +25.5%] 🔵 -4.7% [-6.4%, -2.9%]
WolfSheep small 🔵 +1.3% [-0.5%, +3.1%] 🔵 +1.5% [+1.0%, +1.9%]
WolfSheep large 🔵 +2.9% [+1.5%, +4.3%] 🔵 -0.4% [-2.5%, +1.6%]
BoidFlockers small 🔵 +1.1% [+0.3%, +2.0%] 🔵 +2.7% [+2.0%, +3.4%]
BoidFlockers large 🔵 +0.8% [-0.0%, +1.5%] 🔵 +1.6% [+1.0%, +2.1%]

@EwoutH EwoutH requested review from quaquel and rht March 9, 2024 13:38
@EwoutH EwoutH added the enhancement Release notes label label Mar 9, 2024
@EwoutH
Copy link
Contributor

EwoutH commented Mar 10, 2024

Sorry for the late reply. In general, I like the idea of typing in major Mesa components. I don't know if I would like it to be included in user-facing examples (not this PR though). More importantly, I have to read up on typing in Python, especially Generic Types, TypeVar and the square brackets stuff.

I will try to do that in the upcoming week. How quick do you want to move with this? And what's your vision on typing in Mesa in general?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants