def test_iteration_end_event(): space = ParameterSpace([ContinuousParameter('x', 0, 1)]) def user_function(x): return x x_test = np.linspace(0, 1)[:, None] y_test = user_function(x_test) x_init = np.linspace(0, 1, 5)[:, None] y_init = user_function(x_init) gpy_model = GPy.models.GPRegression(x_init, y_init) model = GPyModelWrapper(gpy_model) mse = [] def compute_mse(self, loop_state): mse.append(np.mean(np.square(model.predict(x_test)[0] - y_test))) loop_state = create_loop_state(x_init, y_init) acquisition = ModelVariance(model) acquisition_optimizer = AcquisitionOptimizer(space) candidate_point_calculator = SequentialPointCalculator( acquisition, acquisition_optimizer) model_updater = FixedIntervalUpdater(model) loop = OuterLoop(candidate_point_calculator, model_updater, loop_state) loop.iteration_end_event.append(compute_mse) loop.run_loop(user_function, 5) assert len(mse) == 5
def test_condition_invalid_type(mock_next_point_calculator, mock_updater, mock_user_function): invalid_n_iter = 10.0 loop = OuterLoop(mock_next_point_calculator, mock_updater) with pytest.raises(ValueError): loop.run_loop(mock_user_function, invalid_n_iter)
def test_multi_source_batch_experimental_design(): objective, space = multi_fidelity_forrester_function() # Create initial data random_design = RandomDesign(space) x_init = random_design.get_samples(10) intiial_results = objective.evaluate(x_init) y_init = np.array([res.Y for res in intiial_results]) # Create multi source acquisition optimizer acquisition_optimizer = GradientAcquisitionOptimizer(space) multi_source_acquisition_optimizer = MultiSourceAcquisitionOptimizer( acquisition_optimizer, space) # Create GP model gpy_model = GPy.models.GPRegression(x_init, y_init) model = GPyModelWrapper(gpy_model) # Create acquisition acquisition = ModelVariance(model) # Create batch candidate point calculator batch_candidate_point_calculator = GreedyBatchPointCalculator( model, acquisition, multi_source_acquisition_optimizer, batch_size=5) initial_loop_state = LoopState(intiial_results) loop = OuterLoop(batch_candidate_point_calculator, FixedIntervalUpdater(model, 1), initial_loop_state) loop.run_loop(objective, 10) assert loop.loop_state.X.shape[0] == 60
def test_default_condition(mock_next_point_calculator, mock_updater, mock_user_function): n_iter = 10 loop = OuterLoop(mock_next_point_calculator, mock_updater) loop.run_loop(mock_user_function, n_iter) assert (loop.loop_state.iteration == n_iter)
def test_accept_non_wrapped_function(mock_next_point_calculator, mock_updater): stopping_condition = mock.create_autospec(StoppingCondition) stopping_condition.should_stop.side_effect = [False, False, True] user_function = lambda x: np.array([[0]]) loop = OuterLoop(mock_next_point_calculator, mock_updater) loop.run_loop(user_function, stopping_condition) assert (loop.loop_state.iteration == 2) assert (np.array_equal(loop.loop_state.X, np.array([[0], [0]])))
def test_outer_loop(mock_next_point_calculator, mock_updater, mock_user_function): """ Example of automatic outer loop """ stopping_condition = mock.create_autospec(StoppingCondition) stopping_condition.should_stop.side_effect = [False, False, True] loop = OuterLoop(mock_next_point_calculator, mock_updater) loop.run_loop(mock_user_function, stopping_condition) assert (loop.loop_state.iteration == 2) assert (np.array_equal(loop.loop_state.X, np.array([[0], [0]])))
def test_outer_loop_model_update(mock_next_point_calculator, mock_user_function): """ Checks the model has the correct number of data points """ class MockModelUpdater(ModelUpdater): def __init__(self, model): self.model = model def update(self, loop_state): self.model.set_data(loop_state.X, loop_state.Y) class MockModel(IModel): @property def X(self): return self._X @property def Y(self): return self._Y def predict(self, x): pass def set_data(self, x, y): self._X = x self._Y = y def optimize(self): pass mock_model = MockModel() model_updater = MockModelUpdater(mock_model) loop = OuterLoop(mock_next_point_calculator, model_updater) loop.run_loop(mock_user_function, 2) # Check update was last called with a loop state with all the collected data points assert mock_model.X.shape[0] == 2 assert mock_model.Y.shape[0] == 2