Skip to content

Commit

Permalink
reactivate ruff for advanced examples and include them in tests (#2414)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Oct 24, 2024
1 parent c63ed4c commit a41914e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 35 deletions.
4 changes: 4 additions & 0 deletions mesa/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence
from mesa.examples.advanced.pd_grid.model import PdGrid
from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt
from mesa.examples.advanced.wolf_sheep.model import WolfSheep
from mesa.examples.basic.boid_flockers.model import BoidFlockers
from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealthModel
from mesa.examples.basic.conways_game_of_life.model import ConwaysGameOfLife
Expand All @@ -14,4 +16,6 @@
"VirusOnNetwork",
"EpsteinCivilViolence",
"PdGrid",
"SugarscapeG1mt",
"WolfSheep",
]
3 changes: 1 addition & 2 deletions mesa/examples/advanced/pd_grid/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ def pd_agent_portrayal(agent):
model_params=model_params,
name="Spatial Prisoner's Dilemma",
)

page
page # noqa B018
9 changes: 3 additions & 6 deletions mesa/examples/advanced/sugarscape_g1mt/agents.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import math

from mesa.experimental.cell_space import CellAgent
from mesa.experimental.cell_space import FixedAgent

from mesa.experimental.cell_space import CellAgent, FixedAgent


# Helper function
Expand All @@ -18,6 +16,8 @@ def get_distance(cell_1, cell_2):
dx = x1 - x2
dy = y1 - y2
return math.sqrt(dx**2 + dy**2)


class Resource(FixedAgent):
"""
Resource:
Expand All @@ -43,9 +43,6 @@ def step(self):
self.spice_amount = min([self.max_spice, self.spice_amount + 1])





class Trader(CellAgent):
"""
Trader:
Expand Down
11 changes: 7 additions & 4 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import sys
import os.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../../')))
import sys

sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../"))
)


import numpy as np
import solara
from matplotlib.figure import Figure
from mesa.visualization import SolaraViz, make_plot_measure

from sugarscape_g1mt.model import SugarscapeG1mt
from sugarscape_g1mt.trader_agents import Trader

from mesa.visualization import SolaraViz, make_plot_measure


def SpaceDrawer(model):
def portray(g):
Expand Down
9 changes: 4 additions & 5 deletions mesa/examples/advanced/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from pathlib import Path

import mesa
import numpy as np
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid

import mesa
from mesa.examples.advanced.sugarscape_g1mt.agents import Resource, Trader

from mesa.experimental.cell_space import OrthogonalVonNeumannGrid


# Helper Functions
Expand Down Expand Up @@ -53,7 +52,7 @@ def __init__(
vision_min=1,
vision_max=5,
enable_trade=True,
seed=None
seed=None,
):
super().__init__(seed=seed)
# Initiate width and height of sugarscape
Expand Down Expand Up @@ -177,5 +176,5 @@ def step(self):
self.datacollector._agent_records[self.steps] = agent_trades

def run_model(self, step_count=1000):
for i in range(step_count):
for _ in range(step_count):
self.step()
3 changes: 2 additions & 1 deletion mesa/examples/advanced/sugarscape_g1mt/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from scipy import stats
from .model import SugarscapeG1mt, flatten

from .agents import Trader
from .model import SugarscapeG1mt, flatten


def check_slope(y, increasing):
Expand Down
17 changes: 6 additions & 11 deletions mesa/examples/advanced/wolf_sheep/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mesa.examples.advanced.wolf_sheep.agents import Wolf, Sheep, GrassPatch
from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf
from mesa.examples.advanced.wolf_sheep.model import WolfSheep
from mesa.visualization import (
Slider,
Expand Down Expand Up @@ -37,21 +37,18 @@ def wolf_sheep_portrayal(agent):

return portrayal


model_params = {
# The following line is an example to showcase StaticText.
"grass": {
"type": "Select",
"value": True,
"values": [True, False],
"label": "grass regrowth enabled?"
"label": "grass regrowth enabled?",
},
"grass_regrowth_time": Slider("Grass Regrowth Time", 20, 1, 50),
"initial_sheep": Slider(
"Initial Sheep Population", 100, 10, 300
),
"sheep_reproduce": Slider(
"Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01
),
"initial_sheep": Slider("Initial Sheep Population", 100, 10, 300),
"sheep_reproduce": Slider("Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01),
"initial_wolves": Slider("Initial Wolf Population", 10, 5, 100),
"wolf_reproduce": Slider(
"Wolf Reproduction Rate",
Expand All @@ -60,9 +57,7 @@ def wolf_sheep_portrayal(agent):
1.0,
0.01,
),
"wolf_gain_from_food": Slider(
"Wolf Gain From Food Rate", 20, 1, 50
),
"wolf_gain_from_food": Slider("Wolf Gain From Food Rate", 20, 1, 50),
"sheep_gain_from_food": Slider("Sheep Gain From Food", 4, 1, 10),
}

Expand Down
13 changes: 8 additions & 5 deletions mesa/examples/advanced/wolf_sheep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"""

import mesa
from mesa.experimental.cell_space import OrthogonalMooreGrid

from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf
from mesa.experimental.cell_space import OrthogonalMooreGrid


class WolfSheep(mesa.Model):
Expand Down Expand Up @@ -81,13 +80,17 @@ def __init__(
collectors = {
"Wolves": lambda m: len(m.agents_by_type[Wolf]),
"Sheep": lambda m: len(m.agents_by_type[Sheep]),
"Grass": lambda m: len(m.agents_by_type[GrassPatch].select(lambda a:a.fully_grown)) if m.grass else -1,
"Grass": lambda m: len(
m.agents_by_type[GrassPatch].select(lambda a: a.fully_grown)
)
if m.grass
else -1,
}

self.datacollector = mesa.DataCollector(collectors)

# Create sheep:
for i in range(self.initial_sheep):
for _ in range(self.initial_sheep):
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
energy = self.random.randrange(2 * self.sheep_gain_from_food)
Expand Down Expand Up @@ -130,5 +133,5 @@ def step(self):
self.datacollector.collect(self)

def run_model(self, step_count=200):
for i in range(step_count):
for _ in range(step_count):
self.step()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ path = "mesa/__init__.py"
# Hardcode to Python 3.10.
# Reminder to update mesa-examples if the value below is changed.
target-version = "py310"
extend-exclude = ["docs", "build", "mesa/examples/advanced"] # TODO: Remove examples/advanced
extend-exclude = ["docs", "build"]

[tool.ruff.lint]
select = [
Expand Down
32 changes: 32 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
BoidFlockers,
BoltzmannWealthModel,
ConwaysGameOfLife,
EpsteinCivilViolence,
PdGrid,
Schelling,
SugarscapeG1mt,
VirusOnNetwork,
WolfSheep,
)


Expand Down Expand Up @@ -38,3 +42,31 @@ def test_boid_flockers(): # noqa: D103

for _i in range(10):
model.step()


def test_epstein(): # noqa: D103
model = EpsteinCivilViolence(seed=42)

for _i in range(10):
model.step()


def test_pd_grid(): # noqa: D103
model = PdGrid(seed=42)

for _i in range(10):
model.step()


def test_sugarscape_g1mt(): # noqa: D103
model = SugarscapeG1mt(seed=42)

for _i in range(10):
model.step()


def test_wolf_sheep(): # noqa: D103
model = WolfSheep(seed=42)

for _i in range(10):
model.step()

0 comments on commit a41914e

Please sign in to comment.