Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and quaquel committed Sep 4, 2024
1 parent e7ba8e6 commit 959a557
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
3 changes: 1 addition & 2 deletions benchmarks/WolfSheep/wolf_sheep.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def random_move(self):
def spawn_offspring(self):
self.energy /= 2
offspring = self.__class__(
self.
self.model,
self.self.model,
self.energy,
self.p_reproduce,
self.energy_from_food,
Expand Down
2 changes: 2 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __missing__(self, key):
res = self[key] = itertools.count(1)
return res


class Agent:
"""
Base class for a model agent in Mesa.
Expand All @@ -41,6 +42,7 @@ class Agent:
model (Model): A reference to the model instance.
self.pos: Position | None = None
"""

_ids = TestClassDict()

def __init__(self, *args, **kwargs) -> None:
Expand Down
32 changes: 16 additions & 16 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ def test_agent_membership():
agentset = AgentSet(agents, model)

assert agents[0] in agentset
assert TestAgent( model) not in agentset
assert TestAgent(model) not in agentset


def test_agent_add_remove_discard():
model = Model()
agent = TestAgent( model)
agent = TestAgent(model)
agentset = AgentSet([], model)

agentset.add(agent)
Expand All @@ -165,7 +165,7 @@ def test_agent_add_remove_discard():

def test_agentset_get_item():
model = Model()
agents = [TestAgent( model) for _ in range(10)]
agents = [TestAgent(model) for _ in range(10)]
agentset = AgentSet(agents, model)

assert agentset[0] == agents[0]
Expand All @@ -178,7 +178,7 @@ def test_agentset_get_item():

def test_agentset_do_str():
model = Model()
agents = [TestAgent( model) for _ in range(10)]
agents = [TestAgent(model) for _ in range(10)]
agentset = AgentSet(agents, model)

with pytest.raises(AttributeError):
Expand All @@ -201,7 +201,7 @@ def test_agentset_do_str():

# setup
model = Model()
agents = [TestAgentDo( model) for _ in range(10)]
agents = [TestAgentDo(model) for _ in range(10)]
agentset = AgentSet(agents, model)
for agent in agents:
agent.agent_set = agentset
Expand All @@ -212,7 +212,7 @@ def test_agentset_do_str():

def test_agentset_do_callable():
model = Model()
agents = [TestAgent( model) for _ in range(10)]
agents = [TestAgent(model) for _ in range(10)]
agentset = AgentSet(agents, model)

# Test callable with non-existent function
Expand All @@ -226,7 +226,7 @@ def test_agentset_do_callable():
# setup for lambda function tests
n = 10
model = Model()
agents = [TestAgentDo( model) for _ in range(n)]
agents = [TestAgentDo(model) for _ in range(n)]
agentset = AgentSet(agents, model)
for agent in agents:
agent.agent_set = agentset
Expand All @@ -237,7 +237,7 @@ def test_agentset_do_callable():

# setup again for lambda function tests
model = Model()
agents = [TestAgentDo( model) for _ in range(10)]
agents = [TestAgentDo(model) for _ in range(10)]
agentset = AgentSet(agents, model)
for agent in agents:
agent.agent_set = agentset
Expand All @@ -255,7 +255,7 @@ def remove_function(agent):

# setup again for actual function tests
model = Model()
agents = [TestAgentDo( model) for _ in range(n)]
agents = [TestAgentDo(model) for _ in range(n)]
agentset = AgentSet(agents, model)
for agent in agents:
agent.agent_set = agentset
Expand All @@ -266,7 +266,7 @@ def remove_function(agent):

# setup again for actual function tests
model = Model()
agents = [TestAgentDo( model) for _ in range(10)]
agents = [TestAgentDo(model) for _ in range(10)]
agentset = AgentSet(agents, model)
for agent in agents:
agent.agent_set = agentset
Expand Down Expand Up @@ -349,7 +349,7 @@ def test_agentset_map_str():

def test_agentset_map_callable():
model = Model()
agents = [TestAgent( model) for _ in range(10)]
agents = [TestAgent(model) for _ in range(10)]
agentset = AgentSet(agents, model)

# Test callable with non-existent function
Expand All @@ -366,7 +366,7 @@ def test_agentset_map_callable():

def test_agentset_get_attribute():
model = Model()
agents = [TestAgent( model) for _ in range(10)]
agents = [TestAgent(model) for _ in range(10)]
agentset = AgentSet(agents, model)

unique_ids = agentset.get("unique_id")
Expand All @@ -378,7 +378,7 @@ def test_agentset_get_attribute():
model = Model()
agents = []
for i in range(10):
agent = TestAgent( model)
agent = TestAgent(model)
agent.i = i**2
agents.append(agent)
agentset = AgentSet(agents, model)
Expand All @@ -402,8 +402,8 @@ def get_unique_identifier(self):
def test_agentset_select_by_type():
model = Model()
# Create a mix of agents of two different types
test_agents = [TestAgent( model) for _ in range(4)]
other_agents = [OtherAgentType( model) for _ in range(6)]
test_agents = [TestAgent(model) for _ in range(4)]
other_agents = [OtherAgentType(model) for _ in range(6)]

# Combine the two types of agents
mixed_agents = test_agents + other_agents
Expand All @@ -427,7 +427,7 @@ def test_agentset_select_by_type():

def test_agentset_shuffle():
model = Model()
test_agents = [TestAgent( model) for _ in range(12)]
test_agents = [TestAgent(model) for _ in range(12)]

agentset = AgentSet(test_agents, model=model)
agentset = agentset.shuffle()
Expand Down
8 changes: 2 additions & 6 deletions tests/test_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def __init__(self, agent_lifetime=1, n_agents=10):
self.schedule = RandomActivation(self)

for _ in range(n_agents):
self.schedule.add(
FiniteLifeAgent(self.agent_lifetime, self)
)
self.schedule.add(FiniteLifeAgent(self.agent_lifetime, self))

def step(self):
"""Add agents back to n_agents in each step"""
Expand All @@ -40,9 +38,7 @@ def step(self):

if len(self.schedule.agents) < self.n_agents:
for _ in range(self.n_agents - len(self.schedule.agents)):
self.schedule.add(
FiniteLifeAgent(self.agent_lifetime, self)
)
self.schedule.add(FiniteLifeAgent(self.agent_lifetime, self))

def run_model(self, step_count=100):
for _ in range(step_count):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TestAgent(Agent):
pass

model = Model()
test_agent = TestAgent( model)
test_agent = TestAgent(model)
assert test_agent in model.agents
assert type(test_agent) in model.agent_types

Expand Down
8 changes: 4 additions & 4 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class TestRandomActivation(TestCase):

def test_init(self):
model = Model()
agents = [MockAgent( model) for _ in range(10)]
agents = [MockAgent(model) for _ in range(10)]

scheduler = RandomActivation(model, agents)
assert all(agent in scheduler.agents for agent in agents)
Expand Down Expand Up @@ -227,7 +227,7 @@ def test_not_sequential(self):
model = MockModel(activation=RANDOM)
# Create 10 agents
for _ in range(10):
model.schedule.add(MockAgent( model))
model.schedule.add(MockAgent(model))
# Run 3 steps
for _ in range(3):
model.step()
Expand Down Expand Up @@ -273,8 +273,8 @@ class TestRandomActivationByType(TestCase):

def test_init(self):
model = Model()
agents = [MockAgent( model) for _ in range(10)]
agents += [Agent( model) for _ in range(10)]
agents = [MockAgent(model) for _ in range(10)]
agents += [Agent(model) for _ in range(10)]

scheduler = RandomActivationByType(model, agents)
assert all(agent in scheduler.agents for agent in agents)
Expand Down

0 comments on commit 959a557

Please sign in to comment.