def test_multi_source_function_wrapper_evaluation_with_multiple_extra_arguments( ): functions = [ lambda x: (2 * x, np.array([[1]] * x.shape[0]), np.array([[1]] * x.shape[0])), lambda x: (4 * x, np.array([[2]] * x.shape[0]), np.array([[1]] * x.shape[0])) ] function_input = np.array([[1, 0], [2, 1], [3, 0], [4, 0], [5, 1]]) source_index = -1 msfw = MultiSourceFunctionWrapper( functions, source_index, extra_output_names=['cost', 'constraint']) output = msfw.evaluate(function_input) assert len(output) == function_input.shape[0] for i, record in enumerate(output): assert_array_equal(output[i].X, function_input[i]) this_function = functions[function_input[i, source_index]] this_function_input = np.delete(function_input[i], source_index) assert_array_equal(output[i].Y, this_function(this_function_input)[0]) assert_array_equal(output[i].cost, this_function(this_function_input)[1][0]) assert_array_equal(output[i].constraint, this_function(this_function_input)[2][0])
def test_multi_source_function_wrapper_too_few_outputs_outputs_fails(): functions = [lambda x: 2 * x, lambda x: 4 * x] function_input = np.array([[1, 0], [2, 1], [3, 0], [4, 0], [5, 1]]) source_index = -1 msfw = MultiSourceFunctionWrapper(functions, source_index, extra_output_names=['cost']) with pytest.raises(ValueError): msfw.evaluate(function_input)
def test_multi_source_function_wrapper_too_many_outputs_outputs_fails(): functions = [lambda x: (2 * x, np.array([[1]] * x.shape[0]), np.array([[1]] * x.shape[0])), lambda x: (4 * x, np.array([[2]] * x.shape[0]), np.array([[1]] * x.shape[0]))] function_input = np.array([[1, 0], [2, 1], [3, 0], [4, 0], [5, 1]]) source_index = -1 msfw = MultiSourceFunctionWrapper(functions, source_index) with pytest.raises(ValueError): msfw.evaluate(function_input)
def test_multi_source_function_wrapper_evaluation_single_output(): functions = [lambda x: 2 * x, lambda x: 4 * x] function_input = np.array([[1, 0], [2, 1], [3, 0], [4, 0], [5, 1]]) source_index = -1 msfw = MultiSourceFunctionWrapper(functions, source_index) output = msfw.evaluate(function_input) assert len(output) == function_input.shape[0] for i, record in enumerate(output): assert_array_equal(output[i].X, function_input[i]) this_function = functions[function_input[i, source_index]] this_function_input = np.delete(function_input[i], source_index) assert_array_equal(output[i].Y, this_function(this_function_input))
def test_multi_source_function_wrapper_evaluation_no_cost(): functions = [lambda x: 2 * x, lambda x: 4 * x] function_input = np.array([[1, 0], [2, 1], [3, 0], [4, 0], [5, 1]]) source_index = -1 msfw = MultiSourceFunctionWrapper(functions, source_index) output = msfw.evaluate(function_input) assert len(output) == function_input.shape[0] for i, record in enumerate(output): assert_array_equal(output[i].X, function_input[i]) this_function = functions[function_input[i, source_index]] this_function_input = np.delete(function_input[i], source_index) assert_array_equal(output[i].Y, this_function(this_function_input)) assert output[i].cost is None
def multi_fidelity_non_linear_sin(high_fidelity_noise_std_deviation=0, low_fidelity_noise_std_deviation=0): """ Two level non-linear sin function where high fidelity is given by: .. math:: f_{high}(x) = (x - \sqrt{2}) f_{low}(x)^2 and the low fidelity is: .. math:: f_{low}(x) = \sin(8 \pi x) Reference: Nonlinear information fusion algorithms for data-efficient multi-fidelity modelling. P. Perdikaris, M. Raissi, A. Damianou, N. D. Lawrence and G. E. Karniadakis (2017) http://web.mit.edu/parisp/www/assets/20160751.full.pdf """ parameter_space = ParameterSpace( [ContinuousParameter("x1", -5, 10), InformationSourceParameter(2)]) user_function = MultiSourceFunctionWrapper([ lambda x: nonlinear_sin_low(x, low_fidelity_noise_std_deviation), lambda x: nonlinear_sin_high(x, high_fidelity_noise_std_deviation), ]) return user_function, parameter_space
def multi_fidelity_borehole_function(high_noise_std_deviation=0, low_noise_std_deviation=0): """ 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
def multi_fidelity_forrester_function(high_fidelity_noise_std_deviation=0, low_fidelity_noise_std_deviation=0): """ Two-level multi-fidelity forrester function where the high fidelity is given by: .. math:: f(x) = (6x - 2)^2 \sin(12x - 4) and the low fidelity approximation given by: .. math:: f_{low}(x) = 0.5 f_{high}(x) + 10 (x - 0.5) + 5 :param high_fidelity_noise_std_deviation: Standard deviation of observation noise on high fidelity observations. Defaults to zero. :param low_fidelity_noise_std_deviation: Standard deviation of observation noise on low fidelity observations. Defaults to zero. :return: Tuple of user function object and parameter space object """ parameter_space = ParameterSpace([ContinuousParameter("x", 0, 1), InformationSourceParameter(2)]) user_function = MultiSourceFunctionWrapper( [ lambda x: forrester_low(x, low_fidelity_noise_std_deviation), lambda x: forrester(x, high_fidelity_noise_std_deviation), ] ) return user_function, parameter_space
def test_multi_source_function_wrapper_invalid_input(): # invalid input with pytest.raises(ValueError): functions = [lambda x: 2 * x] function_input = np.array([1, 0]) msfw = MultiSourceFunctionWrapper(functions) msfw.evaluate(function_input) # invalid function output with pytest.raises(ValueError): functions = [lambda x: np.array([2])] function_input = np.array([[1, 0]]) msfw = MultiSourceFunctionWrapper(functions) msfw.evaluate(function_input) # invalid function output type with pytest.raises(ValueError): functions = [lambda x: [2]] function_input = np.array([[1, 0]]) msfw = MultiSourceFunctionWrapper(functions) msfw.evaluate(function_input)
def test_multi_source_function_wrapper_invalid_input(): # invalid input with pytest.raises(ValueError): functions = [lambda x: 2 * x] function_input = np.array([1, 0]) msfw = MultiSourceFunctionWrapper(functions) msfw.evaluate(function_input) # invalid function output with pytest.raises(ValueError): functions = [lambda x: np.array([2])] function_input = np.array([[1, 0]]) msfw = MultiSourceFunctionWrapper(functions) msfw.evaluate(function_input) # invalid function output type with pytest.raises(ValueError): functions = [lambda x: [2]] function_input = np.array([[1, 0]]) msfw = MultiSourceFunctionWrapper(functions) msfw.evaluate(function_input)