def test_basics(): exp = ap.Experiment(MyModel, [{'steps': 1}] * 3) results = exp.run() assert 'variables' not in results assert exp.name == 'MyModel' exp = ap.Experiment(MyModel, [{'steps': 1}] * 3, record=True) results = exp.run() assert 'variables' in results
def test_parallel_processing(): exp = ap.Experiment(MyModel, [{'steps': 1}] * 3) pool = mp.Pool(mp.cpu_count()) results = exp.run(pool) exp2 = ap.Experiment(MyModel, [{'steps': 1}] * 3) results2 = exp2.run() del results.info del results2.info assert results == results2
def test_sobol(): param_ranges = {'x': (0., 1.)} sample = ap.sample_saltelli(param_ranges, n=10) results = ap.Experiment(MyModel, sample).run() ap.sensitivity_sobol(results, param_ranges, measures='x') assert results.sensitivity['S1'][0] == 0.6607875959282447 # Test if a non-varied parameter causes errors param_ranges = {'x': (0., 1.), 'y': 1} sample = ap.sample_saltelli(param_ranges, n=10) results = ap.Experiment(MyModel, sample).run() ap.sensitivity_sobol(results, param_ranges) assert results.sensitivity['S1'][0] == 0.6607875959282447
def test_calc_sobol(): si = 1.2380952380952384 parameters = {'x': ap.Range(0., 1.)} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=False) results = ap.Experiment(SobolModel, sample).run(display=False) results.calc_sobol(reporters='x') assert results.sensitivity.sobol['S1'][0] == si # Test if a non-varied parameter causes errors parameters = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=False) results = ap.Experiment(SobolModel, sample).run(display=False) results.calc_sobol() assert results.sensitivity.sobol['S1'][0] == si # Test wrong sample type raises error parameters = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(parameters, n=8) results = ap.Experiment(SobolModel, sample).run(display=False) with pytest.raises(AgentpyError): results.calc_sobol() # Test merging iterations # TODO Improve parameters = {'x': ap.Range(0., 1.)} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=False) results = ap.Experiment(SobolModel, sample, iterations=10).run(display=False) results.calc_sobol(reporters='x') assert results.sensitivity.sobol['S1'][0] == si # Test calc_second_order parameters = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=True) results = ap.Experiment(SobolModel, sample).run(display=False) results.calc_sobol() assert results.sensitivity.sobol[('S2', 'x')][0].__repr__() == 'nan'
def test_interactive(): """Test only for errors.""" def interactive_plot(m): print("x =", m.p.x) param_ranges = {'x': (0., 1.)} sample = ap.sample(param_ranges, n=10) exp = ap.Experiment(ap.Model, sample) exp.interactive(interactive_plot) assert True
def test_random(): parameters = {'steps': 0, 'seed': ap.Values(1, 1, 2)} class Model(ap.Model): def setup(self): self.report('x', self.model.random.random()) sample = ap.Sample(parameters) exp = ap.Experiment(Model, sample, iterations=2, randomize=True) results = exp.run() l = list(results.reporters['x']) assert l[0] != l[1] assert l[0:2] == l[2:4] assert l[0:2] != l[4:6] sample = ap.Sample(parameters) exp = ap.Experiment(Model, sample, iterations=2, randomize=False) results = exp.run() l = list(results.reporters['x']) assert l[0] == l[1] assert l[0:2] == l[2:4] assert l[0:2] != l[4:6] exp = ap.Experiment(Model, parameters, iterations=2) results = exp.run() l1 = list(results.reporters['x']) assert l1 == [0.03542265363082542, 0.08363464439430013] parameters['seed'] = 1 exp = ap.Experiment(Model, parameters, iterations=2) results = exp.run() l2 = list(results.reporters['x']) assert l1 == l2
def test_arrange_scenarios(): class MyModel(ap.Model): def step(self): self.record('test', 1) self.measure('test', 1) self.stop() exp = ap.Experiment(MyModel, scenarios=['sc1', 'sc2'], record=True) results = exp.run() assert results.arrange() is None assert results.arrange(variables='all', scenarios='sc1').shape == (1, 4) assert results.arrange(measures='all', scenarios='sc1').shape == (1, 3)
def test_testing_model(): parameters = {'steps': 2, 'px': ap.Values(1, 2)} sample = ap.Sample(parameters) settings = {'iterations': 2, 'record': True} pytest.model_instance = model = ModelType0(list(sample)[0]) pytest.model_results = model_results = model.run(display=False) exp = ap.Experiment(ModelType0, sample, **settings) pytest.exp_results = exp_results = exp.run(display=False) type_list = ['AgentType1', 'AgentType2', 'EnvType3', 'EnvType4'] assert list(model_results.variables.keys()) == type_list assert list(exp_results.variables.keys()) == type_list
def test_repr(): param_ranges = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(param_ranges, n=10) results = ap.Experiment(MyModel, sample, record=True).run() assert results.__repr__() == repr
def step(self): self.agents.record('wealth') self.agents.wealth_transfer() # Perform single run parameters = {'n': 10, 'steps': 10} model = MoneyModel(parameters) results = model.run() # Perform multiple runs variable_params = { 'n': ap.IntRange(10, 500), 'steps': 10 } sample = ap.Sample(variable_params, n=49) exp = ap.Experiment( MoneyModel, sample, iterations=5, record=True ) results = exp.run()