forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request projectmesa#924 from projectmesa/NewBatchRunner
Re-Implementation of BatchRunner
- Loading branch information
Showing
3 changed files
with
333 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from mesa.agent import Agent | ||
from mesa.batchrunner import _make_model_kwargs, batch_run | ||
from mesa.datacollection import DataCollector | ||
from mesa.model import Model | ||
from mesa.time import BaseScheduler | ||
|
||
|
||
def test_make_model_kwargs(): | ||
assert _make_model_kwargs({"a": 3, "b": 5}) == [{"a": 3, "b": 5}] | ||
assert _make_model_kwargs({"a": 3, "b": range(3)}) == [ | ||
{"a": 3, "b": 0}, | ||
{"a": 3, "b": 1}, | ||
{"a": 3, "b": 2}, | ||
] | ||
assert _make_model_kwargs({"a": range(2), "b": range(2)}) == [ | ||
{"a": 0, "b": 0}, | ||
{"a": 0, "b": 1}, | ||
{"a": 1, "b": 0}, | ||
{"a": 1, "b": 1}, | ||
] | ||
|
||
|
||
class MockAgent(Agent): | ||
""" | ||
Minimalistic agent implementation for testing purposes | ||
""" | ||
|
||
def __init__(self, unique_id, model, val): | ||
super().__init__(unique_id, model) | ||
self.unique_id = unique_id | ||
self.val = val | ||
self.local = 0 | ||
|
||
def step(self): | ||
self.val += 1 | ||
self.local += 0.25 | ||
|
||
|
||
class MockModel(Model): | ||
""" | ||
Minimalistic model for testing purposes | ||
""" | ||
|
||
def __init__( | ||
self, | ||
variable_model_param=None, | ||
variable_agent_param=None, | ||
fixed_model_param=None, | ||
schedule=None, | ||
**kwargs | ||
): | ||
super().__init__() | ||
self.schedule = BaseScheduler(self) if schedule is None else schedule | ||
self.variable_model_param = variable_model_param | ||
self.variable_agent_param = variable_agent_param | ||
self.fixed_model_param = fixed_model_param | ||
self.n_agents = 3 | ||
self.datacollector = DataCollector( | ||
model_reporters={"reported_model_param": self.get_local_model_param}, | ||
agent_reporters={"agent_id": "unique_id", "agent_local": "local"}, | ||
) | ||
self.running = True | ||
self.init_agents() | ||
|
||
def init_agents(self): | ||
if self.variable_agent_param is None: | ||
agent_val = 1 | ||
else: | ||
agent_val = self.variable_agent_param | ||
for i in range(self.n_agents): | ||
self.schedule.add(MockAgent(i, self, agent_val)) | ||
|
||
def get_local_model_param(self): | ||
return 42 | ||
|
||
def step(self): | ||
self.datacollector.collect(self) | ||
self.schedule.step() | ||
|
||
|
||
def test_batch_run(): | ||
result = batch_run(MockModel, {}) | ||
assert result == [ | ||
{ | ||
"RunId": 0, | ||
"iteration": 0, | ||
"Step": 1000, | ||
"reported_model_param": 42, | ||
"AgentID": 0, | ||
"agent_id": 0, | ||
"agent_local": 250.0, | ||
}, | ||
{ | ||
"RunId": 0, | ||
"iteration": 0, | ||
"Step": 1000, | ||
"reported_model_param": 42, | ||
"AgentID": 1, | ||
"agent_id": 1, | ||
"agent_local": 250.0, | ||
}, | ||
{ | ||
"RunId": 0, | ||
"iteration": 0, | ||
"Step": 1000, | ||
"reported_model_param": 42, | ||
"AgentID": 2, | ||
"agent_id": 2, | ||
"agent_local": 250.0, | ||
}, | ||
] | ||
|
||
|
||
def test_batch_run_with_params(): | ||
batch_run( | ||
MockModel, | ||
{ | ||
"variable_model_params": range(5), | ||
"variable_agent_params": ["H", "E", "L", "L", "O"], | ||
}, | ||
) | ||
|
||
|
||
def test_batch_run_single_core(): | ||
batch_run(MockModel, {}, nr_processes=1, iterations=10) |