예제 #1
0
 def test_tree_ei(self):
     """
     Tests that using EI with DecisionTree works properly, by decomposing the process into each of its step.
     """
     # Define a tree and fit it on the historical data
     dtstdr = DecisionTreeSTDRegressor()
     dtstdr.fit(fake_history["parameters"], fake_history["fitness"])
     # Predict the regression values over the whole grid
     combination_ranges = np.array(np.meshgrid(*ranges)).T.reshape(
         -1, len(ranges))
     predictions_means, predictions_std = dtstdr.predict(combination_ranges,
                                                         return_std=True)
     # Compute the EI and save the max
     current_optimum = np.min(fake_history["fitness"])
     computed_ei = compute_expected_improvement(current_optimum,
                                                predictions_means,
                                                predictions_std)
     max_ei_index = np.argmax(computed_ei)
     expected_next_parameter = combination_ranges[max_ei_index]
     # Compare it to the normal process
     surrogate_model = SurrogateModel(
         regression_model=DecisionTreeSTDRegressor,
         next_parameter_strategy=expected_improvement,
     )
     next_parameter = surrogate_model.choose_next_parameter(
         fake_history, ranges)
     np.testing.assert_array_equal(next_parameter, expected_next_parameter)
예제 #2
0
 def test_choose_next_parameter_categorical(self):
     """Tests that the selection of the next parameter behaves as expected
     whenever there are any categorical variables.
     """
     ranges = np.array(
         [[1, 2, 3, 4, 5, 6], ["titi", "toto", "tutu"], ["popo", "jojo"]],
         dtype=object)
     fake_history = {
         "fitness":
         np.array([10, 5, 4, 2, 15]),
         "parameters":
         np.array(
             [[1, "tutu", "popo"], [2, "toto", "popo"], [4, "toto", "popo"],
              [2, "titi", "jojo"], [3, "tutu", "popo"]],
             dtype=object),
         "truncated":
         np.array([True, True, False, False, False]),
     }
     expected_new_parameter = np.array([1, 'titi', 'jojo'], dtype=object)
     surrogate_model = SurrogateModel(
         regression_model=GaussianProcessRegressor,
         next_parameter_strategy=expected_improvement,
     )
     real_new_parameter = surrogate_model.choose_next_parameter(
         fake_history, ranges)
     assert_array_equal(expected_new_parameter, real_new_parameter)
예제 #3
0
 def test_fit_return_prediction_function(self):
     """
     Tests that there is no error when computing the regression function.
     """
     surrogate_model = SurrogateModel(
         regression_model=GaussianProcessRegressor,
         next_parameter_strategy=expected_improvement,
     )
     surrogate_model.regression_function(fake_history, ranges)
예제 #4
0
 def test_evaluate_quality(self):
     """
     Tests that the RMSE is properly returned.
     """
     expected_score = -2.184857
     surrogate_model = SurrogateModel(
         regression_model=GaussianProcessRegressor,
         next_parameter_strategy=expected_improvement,
     )
     surrogate_model.choose_next_parameter(fake_history, ranges)
     real_score = surrogate_model.evaluate_quality(fake_history)
     np.testing.assert_array_almost_equal(expected_score, real_score)
예제 #5
0
 def test_censored_bayesian(self):
     """
     Tests that censored bayesian + EI work properly
     """
     expected_new_parameter = [0, 3]
     surrogate_model = SurrogateModel(
         regression_model=CensoredGaussianProcesses,
         next_parameter_strategy=expected_improvement,
     )
     real_new_parameter = surrogate_model.choose_next_parameter(
         fake_history, ranges)
     np.testing.assert_array_equal(real_new_parameter,
                                   expected_new_parameter)
예제 #6
0
 def test_choose_next_parameter_ei(self):
     """
     Checks that the selection of the next parameter works properly when using EI.
     """
     expected_new_parameter = [4, 4]
     surrogate_model = SurrogateModel(
         regression_model=GaussianProcessRegressor,
         next_parameter_strategy=expected_improvement,
     )
     real_new_parameter = surrogate_model.choose_next_parameter(
         fake_history, ranges)
     np.testing.assert_array_equal(real_new_parameter,
                                   expected_new_parameter)
예제 #7
0
 def test_hot_encoding(self):
     """Tests that the hot encoding feature works as expected by
     hot encoding an array.
     """
     surrogate_model = SurrogateModel(
         regression_model=GaussianProcessRegressor,
         next_parameter_strategy=expected_improvement,
     )
     ranges = np.array(
         [[1, 2, 3], ["titi", "toto", "tutu"], ["popo", "jojo"]],
         dtype=object)
     # manually get categorical ranges
     surrogate_model.get_categorical_ranges(ranges)
     parameter_array = np.array(
         [[1, "tutu", "popo"], [2, "toto", "popo"], [4, "toto", "popo"]],
         dtype=object)
     hot_encoded = surrogate_model.hot_encode(parameter_array)
     assert_array_equal(
         [[1, 0, 0, 1, 1, 0], [2, 0, 1, 0, 1, 0], [4, 0, 1, 0, 1, 0]],
         hot_encoded)
예제 #8
0
    def test_regression_model_no_predict(self):
        """
        Tests that when the regression model does not have a predict method, an error is raised.
        """

        # class without predict
        class AintGotNoPredict:
            """
            Fake class that doesn't have a predict method.
            """
            def fit(self):
                """
                Only a fit method.
                """
                print("I don't have no predict !")

        with self.assertRaises(AttributeError):
            SurrogateModel(
                regression_model=AintGotNoPredict,
                next_parameter_strategy=expected_improvement,
            )