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

Test examples #65

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added examples/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion examples/bank_reserves/bank_reserves/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"""

import mesa
from bank_reserves.random_walk import RandomWalker

from .random_walk import RandomWalker


class Bank(mesa.Agent):
Expand Down
3 changes: 2 additions & 1 deletion examples/bank_reserves/bank_reserves/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import mesa
import numpy as np
from bank_reserves.agents import Bank, Person

from .agents import Bank, Person

"""
If you want to perform a parameter sweep, call batch_run.py instead of run.py.
Expand Down
5 changes: 3 additions & 2 deletions examples/bank_reserves/bank_reserves/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mesa
from bank_reserves.agents import Person
from bank_reserves.model import BankReserves

from .agents import Person
from .model import BankReserves

"""
Citation:
Expand Down
3 changes: 2 additions & 1 deletion examples/charts/charts/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"""

import mesa
from charts.random_walk import RandomWalker

from .random_walk import RandomWalker


class Bank(mesa.Agent):
Expand Down
3 changes: 2 additions & 1 deletion examples/charts/charts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import mesa
import numpy as np
from charts.agents import Bank, Person

from .agents import Bank, Person

"""
If you want to perform a parameter sweep, call batch_run.py instead of run.py.
Expand Down
11 changes: 11 additions & 0 deletions examples/epstein_civil_violence/epstein_civil_violence/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,14 @@ def count_jailed(model):
if agent.breed == "citizen" and agent.jail_sentence > 0:
count += 1
return count

@staticmethod
def count_cops(model):
"""
Helper method to count jailed agents.
"""
count = 0
for agent in model.schedule.agents:
if agent.breed == "cop":
count += 1
return count
5 changes: 3 additions & 2 deletions examples/hex_snowflake/hex_snowflake/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mesa
from hex_snowflake.cell import Cell

from .cell import Cell


class HexSnowflake(mesa.Model):
Expand All @@ -22,7 +23,7 @@ def __init__(self, width=50, height=50):
self.schedule = mesa.time.SimultaneousActivation(self)

# Use a hexagonal grid, where edges wrap around.
self.grid = mesa.space.HexGrid(width, height, torus=True)
self.grid = mesa.space.HexSingleGrid(width, height, torus=True)

# Place a dead cell at each location.
for contents, pos in self.grid.coord_iter():
Expand Down
4 changes: 3 additions & 1 deletion examples/sugarscape_cg/sugarscape_cg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
Northwestern University, Evanston, IL.
"""

from pathlib import Path

import mesa

from .agents import SsAgent, Sugar
Expand Down Expand Up @@ -43,7 +45,7 @@ def __init__(self, width=50, height=50, initial_population=100):
# Create sugar
import numpy as np

sugar_distribution = np.genfromtxt("sugarscape_cg/sugar-map.txt")
sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt")
agent_id = 0
for _, (x, y) in self.grid.coord_iter():
max_sugar = sugar_distribution[x, y]
Expand Down
4 changes: 3 additions & 1 deletion examples/sugarscape_g1mt/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import mesa
import numpy as np

Expand Down Expand Up @@ -85,7 +87,7 @@ def __init__(
)

# read in landscape file from supplmentary material
sugar_distribution = np.genfromtxt("sugarscape_g1mt/sugar-map.txt")
sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt")
spice_distribution = np.flip(sugar_distribution, 1)

agent_id = 0
Expand Down
1 change: 0 additions & 1 deletion examples/wolf_sheep/wolf_sheep/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mesa

from wolf_sheep.agents import GrassPatch, Sheep, Wolf
from wolf_sheep.model import WolfSheep

Expand Down
1 change: 0 additions & 1 deletion examples/wolf_sheep/wolf_sheep/test_random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from mesa.space import MultiGrid
from mesa.time import RandomActivation
from mesa.visualization.TextVisualization import TextGrid, TextVisualization

from wolf_sheep.random_walk import RandomWalker


Expand Down
1 change: 0 additions & 1 deletion gis/agents_and_networks/src/visualization/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class ClockElement(mesa.visualization.TextElement):
def __init__(self):
super().__init__()
pass

def render(self, model):
return f"Day {model.day}, {model.hour:02d}:{model.minute:02d}"
Expand Down
34 changes: 34 additions & 0 deletions test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import importlib
import os

import pytest
from mesa import Model


def get_models(directory):
models = []
for root, dirs, files in os.walk(directory):
for file in files:
if file == "model.py":
module_name = os.path.relpath(os.path.join(root, file[:-3])).replace(
os.sep, "."
)

module = importlib.import_module(module_name)
for item in dir(module):
obj = getattr(module, item)
if (
isinstance(obj, type)
and issubclass(obj, Model)
and obj is not Model
):
models.append(obj)

return models


@pytest.mark.parametrize("model_class", get_models("examples"))
def test_model_steps(model_class):
model = model_class() # Assume no arguments are needed
for _ in range(10):
model.step()