Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def multi_source_optimizer():
mock_acquisition_optimizer = mock.create_autospec(AcquisitionOptimizer)
mock_acquisition_optimizer.optimize.return_value = (np.array([[0.]]), None)
space = ParameterSpace([ContinuousParameter('x', 0, 1), InformationSourceParameter(2)])
return MultiSourceAcquisitionOptimizer(mock_acquisition_optimizer, space)
def multi_source_entropy_search_acquisition(gpy_model):
space = ParameterSpace([ContinuousParameter('x1', 0, 1), InformationSourceParameter(2)])
return MultiInformationSourceEntropySearch(gpy_model, space, num_representer_points=10)
def test_single_value_in_domain_information_source_parameter():
param = InformationSourceParameter(5)
assert param.check_in_domain(2) is True
assert param.check_in_domain(7) is False
def test_two_information_source_parameters_fail():
with pytest.raises(ValueError):
ParameterSpace([InformationSourceParameter(2), InformationSourceParameter(2)])
def test_local_search_acquisition_optimizer_with_context(simple_square_acquisition):
space = ParameterSpace([CategoricalParameter('x', OrdinalEncoding(np.arange(0, 100))),
InformationSourceParameter(10)])
optimizer = LocalSearchAcquisitionOptimizer(space, 1000, 3)
source_encoding = 1
opt_x, opt_val = optimizer.optimize(simple_square_acquisition, {'source': source_encoding})
np.testing.assert_array_equal(opt_x, np.array([[1., source_encoding]]))
np.testing.assert_array_equal(opt_val, np.array([[0. + source_encoding]]))
"""
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
def test_information_source_parameter():
param = InformationSourceParameter(5)
assert param.name == 'source'
assert param.check_in_domain(np.array([0, 1])) is True
assert param.check_in_domain(np.array([4])) is True
assert param.check_in_domain(np.array([5])) is False
def test_multi_source_sequential_with_context():
# Check that we can fix a non-information source parameter with context
mock_acquisition = mock.create_autospec(Acquisition)
mock_acquisition.has_gradients = False
mock_acquisition.evaluate = lambda x: np.sum(x**2, axis=1)[:, None]
space = ParameterSpace([ContinuousParameter('x', 0, 1),
ContinuousParameter('y', 0, 1),
InformationSourceParameter(2)])
acquisition_optimizer = AcquisitionOptimizer(space)
multi_source_acquisition_optimizer = MultiSourceAcquisitionOptimizer(acquisition_optimizer, space)
loop_state_mock = mock.create_autospec(LoopState)
seq = SequentialPointCalculator(mock_acquisition, multi_source_acquisition_optimizer)
next_points = seq.compute_next_points(loop_state_mock, context={'x': 0.25})
# "SequentialPointCalculator" should only ever return 1 value
assert(len(next_points) == 1)
# Context value should be what we set
assert np.isclose(next_points[0, 0], 0.25)
def _get_information_source_parameter(self) -> InformationSourceParameter:
"""
:return: The parameter containing the index of the information source
"""
source_parameter = [param for param in self.space.parameters if isinstance(param, InformationSourceParameter)]
if len(source_parameter) == 0:
raise ValueError('No source parameter found')
return source_parameter[0]
def _find_source_parameter(space):
# Find information source parameter in parameter space
info_source_parameter = None
source_idx = None
for i, param in enumerate(space.parameters):
if isinstance(param, InformationSourceParameter):
info_source_parameter = param
source_idx = i
if info_source_parameter is None:
raise ValueError('No information source parameter found in the parameter space')
return info_source_parameter, source_idx