Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_random_sampling_with_context():
space = ParameterSpace([ContinuousParameter('x', 0, 1), ContinuousParameter('y', 0, 1)])
rs = RandomSampling(space)
loop_state_mock = mock.create_autospec(LoopState)
next_points = rs.compute_next_points(loop_state_mock, context={'x': 0.25})
assert(len(next_points) == 1)
# Context value should be what we set
assert np.isclose(next_points[0, 0], 0.25)
def test_design_with_mixed_domain(encoding):
p1 = ContinuousParameter('p1', 1.0, 5.0)
p2 = CategoricalParameter('p2', encoding)
p3 = DiscreteParameter('p3', [1, 2, 5, 6])
space = ParameterSpace([p1, p2, p3])
points_count = 5
designs = create_model_free_designs(space)
for design in designs:
points = design.get_samples(points_count)
assert points_count == len(points)
# columns count is 1 for continuous plus 1 for discrete plus number of categories
columns_count = 1 + 1 + len(encoding.categories)
assert all([len(p) == columns_count for p in points])
def test_local_search_acquisition_optimizer_neighbours():
np.random.seed(0)
space = ParameterSpace([
CategoricalParameter('a', OneHotEncoding([1, 2, 3])),
CategoricalParameter('b', OrdinalEncoding([0.1, 1, 2])),
CategoricalParameter('c', OrdinalEncoding([0.1, 1, 2])),
DiscreteParameter('d', [0.1, 1.2, 2.3]),
ContinuousParameter('e', 0, 100),
DiscreteParameter('no_neighbours', [1]),
DiscreteParameter('f', [0.1, 1.2, 2.3]),
])
x = np.array([1, 0, 0, 1.6, 2.9, 0.1, 50, 1.2, 1.])
optimizer = LocalSearchAcquisitionOptimizer(space, 1000, 3, num_continuous=1)
neighbourhood = optimizer._neighbours_per_parameter(x, space.parameters)
assert_equal(np.array([[0, 1, 0], [0, 0, 1]]), neighbourhood[0])
assert_equal(np.array([[1], [3]]), neighbourhood[1])
assert_equal(np.array([[2]]), neighbourhood[2])
assert_equal(np.array([[1.2]]), neighbourhood[3])
def test_random_design_returns_correct_number_of_points():
p = ContinuousParameter('c', 1.0, 5.0)
space = ParameterSpace([p])
points_count = 5
points = RandomDesign(space).get_samples(points_count)
assert points_count == len(points)
def test_local_search_acquisition_optimizer(simple_square_acquisition):
space = ParameterSpace([CategoricalParameter('x', OrdinalEncoding(np.arange(0, 100)))])
optimizer = LocalSearchAcquisitionOptimizer(space, 1000, 3)
opt_x, opt_val = optimizer.optimize(simple_square_acquisition)
# ordinal encoding is as integers 1, 2, ...
np.testing.assert_array_equal(opt_x, np.array([[1.]]))
np.testing.assert_array_equal(opt_val, np.array([[0.]]))
class UnknownParameter(Parameter):
def __init__(self, name: str):
self.name = name
def sample_uniform(num_points):
return np.random.randint(0, 1, (num_points, 1))
space.parameters.append(UnknownParameter('y'))
with pytest.raises(TypeError):
t = 1 / (8\pi)
"""
def branin_medium_fidelity(x):
x1 = x[:, 0]
x2 = x[:, 1]
result = (10.0 * np.sqrt(_branin(x - 2.0)[:, 0]) + 2.0 * (x1 - 0.5) - 3.0 * (
3.0 * x2 - 1.0) - 1.0) / 100.
return result[:, None]
def branin_low_fidelity(x):
x2 = x[:, 1]
result = (branin_medium_fidelity(1.2 * (x + 2.0))[:, 0] * 100. - 3.0 * x2 + 1.0) / 100.
return result[:, None]
parameter_space = ParameterSpace([ContinuousParameter('x1', -5, 10), ContinuousParameter('x2', 0, 15),
InformationSourceParameter(3)])
branin_high = lambda x: _branin(x)/100
return MultiSourceFunctionWrapper([branin_low_fidelity, branin_medium_fidelity, branin_high]), parameter_space
where:
.. math::
b = 5.1 / (4 \pi ^ 2)
c = 5 /\pi
r = 6
s = 10
t = 1 / (8\pi)
"""
parameter_space = ParameterSpace([ContinuousParameter('x1', -5, 10), ContinuousParameter('x2', 0, 15)])
return _branin, parameter_space
"""
Two level borehole function.
The Borehole function models water flow through a borehole. Its simplicity and quick evaluation makes it a commonly
used function for testing a wide variety of methods in computer experiments.
See reference for equations:
https://www.sfu.ca/~ssurjano/borehole.html
:param high_noise_std_deviation: Standard deviation of Gaussian observation noise on high fidelity observations.
Defaults to zero.
:param low_noise_std_deviation: Standard deviation of Gaussian observation noise on low fidelity observations.
Defaults to zero.
:return: Tuple of user function object and parameter space
"""
parameter_space = ParameterSpace([
ContinuousParameter('borehole_radius', 0.05, 0.15),
ContinuousParameter('radius_of_influence', 100, 50000),
ContinuousParameter('upper_aquifer_transmissivity', 63070, 115600),
ContinuousParameter('upper_aquifer_head', 990, 1110),
ContinuousParameter('lower_aquifer_transmissivity', 63.1, 116),
ContinuousParameter('lower_aquifer_head', 700, 820),
ContinuousParameter('borehole_length', 1120, 1680),
ContinuousParameter('hydraulic_conductivity', 9855, 12045),
InformationSourceParameter(2)])
user_function = MultiSourceFunctionWrapper([
lambda x: _borehole_low(x, low_noise_std_deviation),
lambda x: _borehole_high(x, high_noise_std_deviation)])
return user_function, parameter_space