From 84380fe79c3a0b62ba52f95d4cbdde41b939c8db Mon Sep 17 00:00:00 2001 From: LemurPwned Date: Sun, 26 Feb 2023 12:29:47 +0100 Subject: [PATCH 1/3] suggested fix to #394 --- bayes_opt/domain_reduction.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bayes_opt/domain_reduction.py b/bayes_opt/domain_reduction.py index 214ff5c22..16cb9b850 100644 --- a/bayes_opt/domain_reduction.py +++ b/bayes_opt/domain_reduction.py @@ -105,9 +105,19 @@ def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array: new_bounds[i, 1] = entry[0] window_width = abs(entry[0] - entry[1]) if window_width < self.minimum_window[i]: - new_bounds[i, 0] -= (self.minimum_window[i] - window_width) / 2.0 - new_bounds[i, 1] += (self.minimum_window[i] - window_width) / 2.0 - + dw = (self.minimum_window[i] - window_width) / 2.0 + new_bounds[i, 0] -= dw + new_bounds[i, 1] += dw + # left_expansion_space = abs(global_bounds[i, 0] - entry[0]) # should be non-positive + # right_expansion_space = abs(global_bounds[i, 1] - entry[1]) # should be non-negative + # # conservative + # dw_l = min(dw, left_expansion_space) + # dw_r = min(dw, right_expansion_space) + # # this crawls towards the edge + # ddw_r = dw_r + max(dw - dw_l, 0) + # ddw_l = dw_l + max(dw - dw_r, 0) + # new_bounds[i, 0] -= ddw_l + # new_bounds[i, 1] += ddw_r return new_bounds def _create_bounds(self, parameters: dict, bounds: np.array) -> dict: From 174e788c57e9150dd98ffebbaafb90193272e80d Mon Sep 17 00:00:00 2001 From: LemurPwned Date: Sun, 26 Feb 2023 12:43:00 +0100 Subject: [PATCH 2/3] I sneezed, sorry -- uncommented --- bayes_opt/domain_reduction.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/bayes_opt/domain_reduction.py b/bayes_opt/domain_reduction.py index 16cb9b850..c41cd66e2 100644 --- a/bayes_opt/domain_reduction.py +++ b/bayes_opt/domain_reduction.py @@ -106,18 +106,16 @@ def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array: window_width = abs(entry[0] - entry[1]) if window_width < self.minimum_window[i]: dw = (self.minimum_window[i] - window_width) / 2.0 - new_bounds[i, 0] -= dw - new_bounds[i, 1] += dw - # left_expansion_space = abs(global_bounds[i, 0] - entry[0]) # should be non-positive - # right_expansion_space = abs(global_bounds[i, 1] - entry[1]) # should be non-negative - # # conservative - # dw_l = min(dw, left_expansion_space) - # dw_r = min(dw, right_expansion_space) - # # this crawls towards the edge - # ddw_r = dw_r + max(dw - dw_l, 0) - # ddw_l = dw_l + max(dw - dw_r, 0) - # new_bounds[i, 0] -= ddw_l - # new_bounds[i, 1] += ddw_r + left_expansion_space = abs(global_bounds[i, 0] - entry[0]) # should be non-positive + right_expansion_space = abs(global_bounds[i, 1] - entry[1]) # should be non-negative + # conservative + dw_l = min(dw, left_expansion_space) + dw_r = min(dw, right_expansion_space) + # this crawls towards the edge + ddw_r = dw_r + max(dw - dw_l, 0) + ddw_l = dw_l + max(dw - dw_r, 0) + new_bounds[i, 0] -= ddw_l + new_bounds[i, 1] += ddw_r return new_bounds def _create_bounds(self, parameters: dict, bounds: np.array) -> dict: From ea1a95edd5369a66b8cf64321f3569dc156bf451 Mon Sep 17 00:00:00 2001 From: LemurPwned Date: Wed, 1 Mar 2023 10:07:03 +0100 Subject: [PATCH 3/3] adding tests & checking global bounds compatiblity with window sizes --- bayes_opt/domain_reduction.py | 11 +++++++++ tests/test_seq_domain_red.py | 42 ++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/bayes_opt/domain_reduction.py b/bayes_opt/domain_reduction.py index c41cd66e2..efc3fc932 100644 --- a/bayes_opt/domain_reduction.py +++ b/bayes_opt/domain_reduction.py @@ -68,6 +68,9 @@ def initialize(self, target_space: TargetSpace) -> None: self.r = self.contraction_rate * self.r + # check if the minimum window fits in the orignal bounds + self._window_bounds_compatiblity(self.original_bounds) + def _update(self, target_space: TargetSpace) -> None: # setting the previous @@ -118,6 +121,14 @@ def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array: new_bounds[i, 1] += ddw_r return new_bounds + def _window_bounds_compatiblity(self, global_bounds: np.array) -> bool: + """Checks if global bounds are compatible with the minimum window sizes.""" + for i, entry in enumerate(global_bounds): + global_window_width = abs(entry[1] - entry[0]) + if global_window_width < self.minimum_window[i]: + raise ValueError( + "Global bounds are not compatible with the minimum window size.") + def _create_bounds(self, parameters: dict, bounds: np.array) -> dict: return {param: bounds[i, :] for i, param in enumerate(parameters)} diff --git a/tests/test_seq_domain_red.py b/tests/test_seq_domain_red.py index ae71a9669..ffe72e14f 100644 --- a/tests/test_seq_domain_red.py +++ b/tests/test_seq_domain_red.py @@ -1,8 +1,9 @@ import numpy as np +import pytest from bayes_opt import SequentialDomainReductionTransformer from bayes_opt import BayesianOptimization - +from bayes_opt.target_space import TargetSpace def black_box_function(x, y): """Function with unknown internals we wish to maximize. @@ -104,3 +105,42 @@ def test_minimum_window_array_is_kept(): ) window_widths = np.diff(bounds_transformer.bounds) assert np.all(np.isclose(np.squeeze(np.min(window_widths, axis=0)), window_ranges)) + +def test_trimming_bounds(): + """Test if the bounds are trimmed correctly within the bounds""" + def dummy_function(x1, x2, x3, x4, x5): + return 0.0 + + min_window = 1.0 + bounds_transformer = SequentialDomainReductionTransformer(minimum_window=min_window) + pbounds = { + 'x1': (-1, 0.6), + 'x2': (-1, 0.5), + 'x3': (-0.4, 0.6), + 'x4': (0.3, 1.3), + 'x5': (-1, 0.8), + } + target_sp = TargetSpace(target_func=dummy_function, pbounds=pbounds) + bounds_transformer.initialize(target_sp) + new_bounds = np.concatenate((np.ones((5, 1)) * 0.1, np.ones((5, 1))), axis=1) + global_bounds = np.asarray(list(pbounds.values())) + + trimmed_bounds = bounds_transformer._trim(new_bounds, global_bounds) + # check that the bounds are trimmed to the minimum window + # raises ValueError if the bounds are not trimmed correctly + bounds_transformer._window_bounds_compatiblity(trimmed_bounds) + + +def test_exceeded_bounds(): + """Raises Value Error if the bounds are exceeded.""" + window_ranges = [1.01, 0.72] + bounds_transformer = SequentialDomainReductionTransformer(minimum_window=window_ranges) + pbounds = {'x': (-0.5, 0.5), 'y': (-0.7, 0.0)} + with pytest.raises(ValueError): + _ = BayesianOptimization( + f=black_box_function, + pbounds=pbounds, + verbose=0, + random_state=1, + bounds_transformer=bounds_transformer + ) \ No newline at end of file