Skip to content

Commit

Permalink
add only fixed variables and 1 processor debug tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpike3 committed Aug 31, 2020
1 parent 3543b63 commit 53b8868
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 27 deletions.
33 changes: 22 additions & 11 deletions mesa/batchrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ def __next__(self):
raise StopIteration()


# TODO: No difference- deleting will remove whitespace issue
class BatchRunner(FixedBatchRunner):
""" This class is instantiated with a model class, and model parameters
associated with one or more values. It is also instantiated with model and
Expand Down Expand Up @@ -451,16 +450,28 @@ def __init__(
display_progress: Display progress bar with time estimation?
"""
super().__init__(
model_cls,
ParameterProduct(variable_parameters),
fixed_parameters,
iterations,
max_steps,
model_reporters,
agent_reporters,
display_progress,
)
if variable_parameters is None:
super().__init__(
model_cls,
variable_parameters,
fixed_parameters,
iterations,
max_steps,
model_reporters,
agent_reporters,
display_progress,
)
else:
super().__init__(
model_cls,
ParameterProduct(variable_parameters),
fixed_parameters,
iterations,
max_steps,
model_reporters,
agent_reporters,
display_progress,
)


class BatchRunnerMP(BatchRunner):
Expand Down
38 changes: 34 additions & 4 deletions tests/test_batchrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class MockModel(Model):

def __init__(
self,
variable_model_param,
variable_agent_param,
variable_model_param=None,
variable_agent_param=None,
fixed_model_param=None,
schedule=None,
**kwargs
Expand All @@ -56,8 +56,12 @@ def __init__(
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, self.variable_agent_param))
self.schedule.add(MockAgent(i, self, agent_val))

def get_local_model_param(self):
return 42
Expand Down Expand Up @@ -113,6 +117,20 @@ def launch_batch_processing(self):
batch.run_all()
return batch

def launch_batch_processing_fixed(self):
# Adding second batchrun to test fixed params increase coverage
batch2 = BatchRunner(
self.mock_model,
fixed_parameters={"fixed": "happy"},
iterations=4,
max_steps=self.max_steps,
model_reporters=self.model_reporters,
agent_reporters=None,
)

batch2.run_all()
return batch2

@property
def model_runs(self):
"""
Expand Down Expand Up @@ -163,11 +181,23 @@ def test_model_with_fixed_parameters_as_kwargs(self):
batch = self.launch_batch_processing()
model_vars = batch.get_model_vars_dataframe()
agent_vars = batch.get_agent_vars_dataframe()

self.assertEqual(len(model_vars), len(agent_vars))
self.assertEqual(len(model_vars), self.model_runs)
self.assertEqual(model_vars["reported_fixed_value"].unique(), ["Fixed"])

def test_model_with_only_fixed_parameters(self):
"""
Test that model with only fixed parameters and multiple iterations is
properly handled
"""
batch = self.launch_batch_processing_fixed()
model_vars = batch.get_model_vars_dataframe()
self.assertEqual(len(model_vars), 4)
self.assertEqual(model_vars["fixed"].unique(), ["happy"])

with self.assertRaises(AttributeError):
batch.get_agent_vars_dataframe()

def test_model_with_variable_and_fixed_kwargs(self):
self.mock_model = MockMixedModel
self.model_reporters = {
Expand Down
55 changes: 43 additions & 12 deletions tests/test_batchrunnerMP.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,54 @@ def launch_batch_processing(self):
batch.run_all()
return batch

def launch_batch_processing_debug(self):
'''
Tests with one processor for debugging purposes
'''

batch = BatchRunnerMP(
self.mock_model,
nr_processes=1,
variable_parameters=self.variable_params,
fixed_parameters=self.fixed_params,
iterations=self.iterations,
max_steps=self.max_steps,
model_reporters=self.model_reporters,
agent_reporters=self.agent_reporters,
)

batch.run_all()
return batch

@property
def model_runs(self):
"""
Returns total number of batch runner's iterations.
"""
return reduce(mul, map(len, self.variable_params.values())) * self.iterations

def batch_model_vars(self, results):
model_vars = results.get_model_vars_dataframe()
model_collector = results.get_collector_model()
expected_cols = (len(self.variable_params) + len(self.model_reporters) + 1) # extra column with run index
self.assertEqual(model_vars.shape, (self.model_runs, expected_cols))
self.assertEqual(len(model_collector.keys()), self.model_runs)

def test_model_level_vars(self):
"""
Test that model-level variable collection is of the correct size
"""
batch = self.launch_batch_processing()
assert batch.processes == cpu_count()
assert batch.processes != 1
model_vars = batch.get_model_vars_dataframe()
model_collector = batch.get_collector_model()
expected_cols = (len(self.variable_params) + len(self.model_reporters) + 1) # extra column with run index
self.assertEqual(model_vars.shape, (self.model_runs, expected_cols))
self.assertEqual(len(model_collector.keys()), self.model_runs)
self.batch_model_vars(batch)

def test_agent_level_vars(self):
"""
Test that agent-level variable collection is of the correct size
"""
batch = self.launch_batch_processing()
agent_vars = batch.get_agent_vars_dataframe()
agent_collector = batch.get_collector_agents()
batch2 = self.launch_batch_processing_debug()
self.batch_model_vars(batch2)

def batch_agent_vars(self, result):
agent_vars = result.get_agent_vars_dataframe()
agent_collector = result.get_collector_agents()
# extra columns with run index and agentId
expected_cols = (len(self.variable_params) + len(self.agent_reporters) + 2)
assert "agent_val" in list(agent_vars.columns)
Expand All @@ -158,6 +179,16 @@ def test_agent_level_vars(self):
agent_collector[(0, 1, 0)].shape, (NUM_AGENTS * self.max_steps, 2)
)

def test_agent_level_vars(self):
"""
Test that agent-level variable collection is of the correct size
"""
batch = self.launch_batch_processing()
self.batch_agent_vars(batch)

batch2 = self.launch_batch_processing_debug()
self.batch_agent_vars(batch2)

def test_model_with_fixed_parameters_as_kwargs(self):
"""
Test that model with fixed parameters passed like kwargs is
Expand Down

0 comments on commit 53b8868

Please sign in to comment.