-
Notifications
You must be signed in to change notification settings - Fork 14
Fix Sugarscape example compatibility with Mesa 3.0 #131
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
base: main
Are you sure you want to change the base?
Changes from all commits
8f62e0c
5efb10c
ad592e5
5360b18
ef2d60d
168c35c
9fd253f
d8b85f6
c6536dd
b696779
2085e38
aadfc4b
9f219e9
e10dfca
81eb16e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
""" | ||
Pytest tests for the Sugarscape example with Mesa 3.x. | ||
""" | ||
|
||
import sys | ||
import os | ||
from pathlib import Path | ||
|
||
# Add the parent directory to sys.path to allow imports from ss_mesa package | ||
current_dir = Path(__file__).parent | ||
examples_dir = current_dir.parent | ||
root_dir = examples_dir.parent | ||
|
||
# Add root directory to sys.path if not already there | ||
if str(root_dir) not in sys.path: | ||
sys.path.insert(0, str(root_dir)) | ||
|
||
# Add the examples directory to sys.path if not already there | ||
if str(examples_dir) not in sys.path: | ||
sys.path.insert(0, str(examples_dir)) | ||
|
||
import mesa | ||
import pytest | ||
from ss_mesa.model import SugarscapeMesa | ||
from ss_mesa.agents import AntMesa, Sugar | ||
|
||
|
||
@pytest.fixture | ||
def sugarscape_model(): | ||
"""Create a standard Sugarscape model for testing.""" | ||
return SugarscapeMesa(10, width=10, height=10) | ||
|
||
|
||
def test_model_creation(sugarscape_model): | ||
"""Test that the model can be created properly with Mesa 3.x""" | ||
model = sugarscape_model | ||
|
||
# Count agents with isinstance | ||
total_agents = len(model.agents) | ||
ant_count = sum(1 for agent in model.agents if isinstance(agent, AntMesa)) | ||
sugar_count = sum(1 for agent in model.agents if isinstance(agent, Sugar)) | ||
|
||
# Check that we have the expected number of agents | ||
assert total_agents == (10 * 10 + 10), "Unexpected total agent count" | ||
assert ant_count == 10, "Unexpected AntMesa agent count" | ||
assert sugar_count == 10 * 10, "Unexpected Sugar agent count" | ||
|
||
|
||
def test_model_step(sugarscape_model): | ||
"""Test that the model can be stepped with Mesa 3.x""" | ||
model = sugarscape_model | ||
|
||
# Count agents before stepping | ||
ant_count_before = sum(1 for agent in model.agents if isinstance(agent, AntMesa)) | ||
|
||
# Step the model | ||
model.step() | ||
|
||
# Count agents after stepping | ||
ant_count_after = sum(1 for agent in model.agents if isinstance(agent, AntMesa)) | ||
|
||
# In this basic test, we just verify the step completes without errors | ||
# and the number of ants doesn't unexpectedly change | ||
assert ant_count_after >= 0, "Expected at least some ants to survive" | ||
|
||
|
||
@pytest.fixture | ||
def simple_model(): | ||
"""Create a simplified model with just a few agents to isolate behavior""" | ||
|
||
class SimpleModel(mesa.Model): | ||
def __init__(self, seed=None): | ||
super().__init__(seed=seed) | ||
self.space = mesa.space.MultiGrid(5, 5, torus=False) | ||
|
||
# Add sugar agents to all cells | ||
self.sugars = [] | ||
for x in range(5): | ||
for y in range(5): | ||
sugar = Sugar(self, 5) | ||
self.space.place_agent(sugar, (x, y)) | ||
self.sugars.append(sugar) | ||
|
||
# Create one ant agent | ||
self.ant = AntMesa(self, False, 10, 2, 3) | ||
self.space.place_agent(self.ant, (2, 2)) # Place in the middle | ||
|
||
def step(self): | ||
# Step the sugar agents | ||
for sugar in self.sugars: | ||
sugar.step() | ||
|
||
# Step the ant agent | ||
self.ant.step() | ||
|
||
return SimpleModel() | ||
|
||
|
||
def test_simple_model_creation(simple_model): | ||
"""Test that the simple model is created with the correct agents.""" | ||
# Check agents | ||
assert len(simple_model.agents) == 26, "Expected 26 total agents (25 sugar + 1 ant)" | ||
|
||
ant_count = sum(1 for agent in simple_model.agents if isinstance(agent, AntMesa)) | ||
sugar_count = sum(1 for agent in simple_model.agents if isinstance(agent, Sugar)) | ||
|
||
assert ant_count == 1, "Expected exactly 1 AntMesa agent" | ||
assert sugar_count == 25, "Expected exactly 25 Sugar agents" | ||
|
||
|
||
def test_sugar_step(simple_model): | ||
"""Test that sugar agents can step without errors.""" | ||
for sugar in simple_model.sugars: | ||
sugar.step() | ||
# If we get here without exceptions, the test passes | ||
|
||
|
||
def test_ant_step(simple_model): | ||
"""Test that ant agents can step without errors.""" | ||
simple_model.ant.step() | ||
# If we get here without exceptions, the test passes | ||
|
||
|
||
def test_simple_model_step(simple_model): | ||
"""Test that the simple model can step without errors.""" | ||
simple_model.step() | ||
# If we get here without exceptions, the test passes |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put >= than the last version of mesa (3.1.4) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adamamer20, I think mesa does not use python 3.11, so instead of just changing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not actually adding any agents to the model so everytime you run_model from
performance_comparison.py
, the number of agents is always 0 and you don't actually do anything.