def test_select_parameters(self): """Test the function _select_next_parameters when there is no retry.""" np.random.seed(10) bb_obj = BBOptimizer( black_box=self.parabola, heuristic="surrogate_model", max_iteration=1, initial_sample_size=2, parameter_space=parameter_space, next_parameter_strategy=expected_improvement, regression_model=GaussianProcessRegressor, ) bb_obj._initialize() parameter = bb_obj._select_next_parameters() np.testing.assert_array_equal(parameter, np.array([-5, 2, -6]))
def test_initialize(self): """Tests that the initialization step happens correctly, by appending the proper number of fitness and parametrization values.""" bb_obj = BBOptimizer( black_box=self.parabola, heuristic="surrogate_model", max_iteration=nbr_iteration, initial_sample_size=2, parameter_space=parameter_space, next_parameter_strategy=expected_improvement, regression_model=GaussianProcessRegressor, ) bb_obj._initialize(callbacks=[lambda x: x]) self.assertTrue(len(bb_obj.history["fitness"]), 2) self.assertTrue(len(bb_obj.history["parameters"]), 2)
def test_initialize_callback(self): """Tests that the initialization step happens correctly when using a callback function, which prints the square root of the sum of the fitness values and the sum of the parameters""" np.random.seed(10) # Capture the stdout to check if the print happenned correctly captured_output = io.StringIO() sys.stdout = captured_output bb_obj = BBOptimizer( black_box=self.parabola, heuristic="surrogate_model", max_iteration=nbr_iteration, initial_sample_size=2, parameter_space=parameter_space, next_parameter_strategy=expected_improvement, regression_model=GaussianProcessRegressor, ) bb_obj._initialize(callbacks=[mock_callback_1]) # Redirect the stdout to not mess with the system sys.stdout = sys.__stdout__ expected = "Result: 3 + 5\nResult: 4 + 6\n" self.assertEqual(expected, captured_output.getvalue())