Exemple #1
0
 def test_optimizer_simple_resampling_noresample(self):
     """Tests that the _select_next_parameters_method works as expected
     when using simple resampling strategy and the parameters should not be resampled"""
     np.random.seed(1)
     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,
         resampling_policy="simple_resampling",
         nbr_resamples=2,
     )
     bb_obj.history = {
         "fitness": np.array([10, 5, 4, 2, 15, 20]),
         "parameters": np.array(
             [[1, 2, 3], [2, 3, 3], [1, 3, 3], [1, 5, 3], [1, 1, 3], [1, 5, 3]]
         ),
         "truncated": np.array([True, True, True, True, True, True]),
         "resampled": np.array([True, True, True, True, True, True]),
         "initialization": np.array([True, True, True, True, True, True]),
     }
     parameter = bb_obj._select_next_parameters()
     np.testing.assert_array_equal(parameter, [1, 3, 2])
Exemple #2
0
 def test_fitness_gain_per_iteration(self):
     """
     Tests that the fitness gain per iteration is properly computed.
     """
     history = {
         "fitness": np.array([10, 5, 6, 2, 15, 4]),
         "parameters": np.array([[1, 2], [2, 3], [1, 3], [4, 3], [2, 1], [1, 5]]),
         "truncated": np.array([True, True, True, True, True, True]),
     }
     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.history = history
     bb_obj.launched = True
     expected_gain_per_iteration = np.array([-5, 1, -4, 13, -11])
     np.testing.assert_array_equal(
         expected_gain_per_iteration,
         bb_obj.fitness_gain_per_iteration,
         "Computation of fitness gain per iteration did not work as " "expected.",
     )
Exemple #3
0
    def test_global_exploration_cost(self):
        """
        Tests that the global exploration cost is properly computed.
        """
        history = {
            "fitness": np.array([10, 5, 6, 2, 15, 4]),
            "parameters": np.array([[1, 2], [2, 3], [1, 3], [4, 3], [2, 1], [1, 5]]),
            "truncated": np.array([True, True, True, True, True, True]),
        }

        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.history = history
        expected_number_states = 3
        expected_performance_cost = 16
        real_number_states, real_performance_cost = bb_obj.global_exploration_cost
        self.assertEqual(
            expected_number_states,
            real_number_states,
            "Percentage of static moves " "was not computed properly.",
        )
        self.assertEqual(
            expected_performance_cost,
            real_performance_cost,
            "Percentage of explored " "space was not computed " "properly.",
        )
Exemple #4
0
 def test_size_explored_space(self):
     """
     Tests that the computation of the size of the explored space works properly, as well as
     the percentage of static moves.
     """
     history = {
         "fitness": np.array([10, 5, 4]),
         "parameters": np.array([[1, 2, 3], [2, 3, 4], [1, 2, 3]]),
         "truncated": np.array([True, True, True, True, True, True]),
     }
     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.history = history
     expected_static_moves = 1 / 3 * 100
     expected_explored_space = 2 / 1440 * 100
     real_explored_space, real_static_moves = bb_obj.size_explored_space
     self.assertEqual(
         expected_static_moves,
         real_static_moves,
         "Percentage of static moves " "was not computed properly.",
     )
     self.assertEqual(
         expected_explored_space,
         real_explored_space,
         "Percentage of explored " "space was not computed " "properly.",
     )
Exemple #5
0
 def test_append_performance(self):
     """
     Tests that appending a new fitness value on the performance history works properly.
     """
     # Manually create the history of the BBOptimizer
     bb_obj = BBOptimizer(
         black_box=self.parabola,
         heuristic="surrogate_model",
         max_iteration=nbr_iteration,
         parameter_space=parameter_space,
         next_parameter_strategy=expected_improvement,
         regression_model=GaussianProcessRegressor,
     )
     bb_obj.history = fake_history
     # Tests the append method
     bb_obj._append_fitness(10)
     np.testing.assert_array_equal(
         bb_obj.history["fitness"], np.array(
             np.array([10, 5, 4, 2, 15, 20, 10]))
     )
Exemple #6
0
 def test_append_parameters(self):
     """
     Tests that the parameters are correctly added to the history.
     """
     # Manually create the history of the BBOptimizer
     bb_obj = BBOptimizer(
         black_box=self.parabola,
         heuristic="surrogate_model",
         max_iteration=nbr_iteration,
         parameter_space=parameter_space,
         next_parameter_strategy=expected_improvement,
         regression_model=GaussianProcessRegressor,
     )
     bb_obj.history = fake_history
     # Test the append method
     bb_obj._append_parameters([1, 3])
     np.testing.assert_array_equal(
         bb_obj.history["parameters"],
         np.array([[1, 2], [2, 3], [1, 3], [4, 3], [2, 1], [1, 5], [1, 3]]),
     )
Exemple #7
0
 def test_fitness_aggregation_std(self):
     """Tests that the fitness aggregation is performed properly when using the std
     as the estimator for the fitness aggregation.
     """
     np.random.seed(5)
     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,
         resampling_policy="simple_resampling",
         nbr_resamples=2,
         fitness_aggregation="simple_fitness_aggregation",
         estimator=np.std,
     )
     bb_obj.history = {
         "fitness": np.array([10, 5, 4, 2, 15, 20]),
         "parameters": np.array(
             [[1, 2, 3], [2, 3, 3], [1, 3, 3], [1, 5, 3], [1, 1, 3], [1, 5, 3]]
         ),
         "truncated": np.array([True, True, True, True, True, True]),
         "resampled": np.array([True, True, True, True, True, True]),
         "initialization": np.array([True, True, True, True, True, True]),
     }
     aggregated_history = bb_obj.fitness_aggregation.transform(
         bb_obj.history)
     np.testing.assert_array_equal(
         aggregated_history["fitness"], np.array([0.0, 0.0, 0.0, 9.0, 0.0])
     )
     np.testing.assert_array_equal(
         aggregated_history["parameters"],
         np.array([[1, 2, 3], [2, 3, 3], [1, 3, 3], [1, 5, 3], [1, 1, 3]]),
     )