From 53b886819ca2908e611f4ec7fd5e92cda809b18f Mon Sep 17 00:00:00 2001 From: tpike3 Date: Mon, 31 Aug 2020 09:18:07 -0400 Subject: [PATCH] add only fixed variables and 1 processor debug tests --- mesa/batchrunner.py | 33 ++++++++++++++-------- tests/test_batchrunner.py | 38 ++++++++++++++++++++++--- tests/test_batchrunnerMP.py | 55 +++++++++++++++++++++++++++++-------- 3 files changed, 99 insertions(+), 27 deletions(-) diff --git a/mesa/batchrunner.py b/mesa/batchrunner.py index 4decc66f202..6a480117c37 100644 --- a/mesa/batchrunner.py +++ b/mesa/batchrunner.py @@ -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 @@ -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): diff --git a/tests/test_batchrunner.py b/tests/test_batchrunner.py index 1f3f627f370..24de7015dc2 100644 --- a/tests/test_batchrunner.py +++ b/tests/test_batchrunner.py @@ -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 @@ -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 @@ -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): """ @@ -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 = { diff --git a/tests/test_batchrunnerMP.py b/tests/test_batchrunnerMP.py index 9fc0743de6a..1a45e171ddb 100644 --- a/tests/test_batchrunnerMP.py +++ b/tests/test_batchrunnerMP.py @@ -115,6 +115,25 @@ 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): """ @@ -122,6 +141,13 @@ def model_runs(self): """ 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 @@ -129,19 +155,14 @@ def test_model_level_vars(self): 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) @@ -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