def test_perturb_real_factor(self, factor): explore = PerturbExplore(factor=factor) rng = RNGStub() rng.random = lambda: 1.0 assert explore.perturb_real(rng, 1.0, (0.1, 2.0)) == factor rng.random = lambda: 0.0 assert explore.perturb_real(rng, 1.0, (0.1, 2.0)) == 1.0 / factor
def test_perturb_int_factor(self, factor): explore = PerturbExplore(factor=factor) rng = RNGStub() rng.random = lambda: 1.0 assert explore.perturb_int(rng, 5, (0, 10)) == int(numpy.round(5 * factor)) rng.random = lambda: 0.0 assert explore.perturb_int(rng, 5, (0, 10)) == int(numpy.round(5 / factor))
def test_resample_probability(self, space): explore = ResampleExplore(probability=0.5) rng = RNGStub() rng.randint = lambda low, high, size: [1] rng.random = lambda: 0.5 params = {"x": 1.0, "y": 2, "z": 0, "f": 10} assert explore(rng, space, params) is params rng.random = lambda: 0.4 assert explore(rng, space, params) is not params
def test_perturb_int_no_out_of_bounds(self): explore = PerturbExplore(factor=0.75, volatility=0) rng = RNGStub() rng.random = lambda: 1.0 rng.normal = lambda mean, variance: variance assert explore.perturb_int(rng, 0, (0, 10)) == 0 rng.random = lambda: 0.0 rng.normal = lambda mean, variance: variance assert explore.perturb_int(rng, 10, (0, 10)) == 10
def test_perturb_int_no_duplicate_below(self): explore = PerturbExplore(factor=0.75) rng = RNGStub() rng.random = lambda: 1.0 assert explore.perturb_int(rng, 1, (0, 10)) == 0
def test_perturb_int_duplicate_equal(self): explore = PerturbExplore(factor=1.0) rng = RNGStub() rng.random = lambda: 1.0 assert explore.perturb_int(rng, 1, (0, 10)) == 1
def test_perturb_real_volatility_above(self, volatility): explore = PerturbExplore(factor=1.0, volatility=volatility) rng = RNGStub() rng.random = lambda: 1.0 rng.normal = lambda mean, variance: variance assert explore.perturb_real(rng, 3.0, (1.0, 2.0)) == 2.0 - volatility
def test_perturb_real_above_interval_cap(self): explore = PerturbExplore(factor=1.0, volatility=0) rng = RNGStub() rng.random = lambda: 1.0 rng.normal = lambda mean, variance: variance assert explore.perturb_real(rng, 3.0, (1.0, 2.0)) == 2.0 explore.volatility = 1000 assert explore.perturb_real(rng, 3.0, (1.0, 2.0)) == 1.0
def test_perturb(self, space): explore = PerturbExplore() rng = RNGStub() rng.randint = lambda low, high, size: [1] rng.random = lambda: 1.0 rng.normal = lambda mean, variance: 0.0 rng.choice = lambda choices: choices[0] params = {"x": 1.0, "y": 2, "z": 0, "f": 10} new_params = explore(rng, space, params) for key in space.keys(): assert new_params[key] in space[key]
def test_perturb_hierarchical_params(self, hspace): explore = PerturbExplore() rng = RNGStub() rng.randint = lambda low, high, size: [1] rng.random = lambda: 1.0 rng.normal = lambda mean, variance: 0.0 rng.choice = lambda choices: choices[0] params = {"numerical": {"x": 1.0, "y": 2, "f": 10}, "z": 0} new_params = explore(rng, hspace, params) assert "numerical" in new_params assert "x" in new_params["numerical"] for key in hspace.keys(): assert flatten(new_params)[key] in hspace[key]