def test_sample_saltelli_second(): parameters = { 'a': ap.IntRange(1, 2), 'b': ap.Range(3, 3.5), 'c': ap.Values(*'xyz'), 'd': True } sample = ap.Sample(parameters, n=2, method='saltelli', calc_second_order=True) problem = { 'num_vars': 3, 'names': ['a', 'b', 'c'], 'bounds': [ [1., 3.], [3., 3.5], [0., 3.], ] } param_values = saltelli.sample(problem, 2, calc_second_order=True) for s1, s2 in zip(sample, param_values): assert s1['a'] == int(s2[0]) assert s1['b'] == s2[1] assert s1['c'] == parameters['c'].values[int(s2[2])] assert s1['d'] == parameters['d']
def test_linspace_zip(): parameters = { 'a': ap.Range(), # 0, 1 Default 'b': ap.Values(3, 4), } sample = ap.Sample(parameters, n=2, product=False) assert list(sample) == [{'a': 0.0, 'b': 3}, {'a': 1.0, 'b': 4}]
def test_repr(): v = ap.Values(1, 2) assert v.__repr__() == "Set of 2 parameter values" r = ap.Range(1, 2) assert r.__repr__() == "Parameter range from 1 to 2" ir = ap.IntRange(1, 2) assert ir.__repr__() == "Integer parameter range from 1 to 2" s = ap.Sample({'x': r}, 10) assert s.__repr__() == "Sample of 10 parameter combinations"
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_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_default_parameter_choice(): parameters = {'x': ap.Range(1, 2), 'y': ap.Values(1, 2)} model = ap.Model(parameters) assert model.p == {'x': 1, 'y': 1}
def test_linspace_product(): parameters = { 'a': ap.IntRange(1, 2), 'b': ap.Range(3, 3.5), 'c': ap.Values(*'xyz'), 'd': True } sample = ap.Sample(parameters, n=3) assert list(sample) == [{ 'a': 1, 'b': 3.0, 'c': 'x', 'd': True }, { 'a': 1, 'b': 3.0, 'c': 'y', 'd': True }, { 'a': 1, 'b': 3.0, 'c': 'z', 'd': True }, { 'a': 1, 'b': 3.25, 'c': 'x', 'd': True }, { 'a': 1, 'b': 3.25, 'c': 'y', 'd': True }, { 'a': 1, 'b': 3.25, 'c': 'z', 'd': True }, { 'a': 1, 'b': 3.5, 'c': 'x', 'd': True }, { 'a': 1, 'b': 3.5, 'c': 'y', 'd': True }, { 'a': 1, 'b': 3.5, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'z', 'd': True }]