Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions bayes_opt/domain_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,11 +108,27 @@ 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
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 _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)}

Expand Down
42 changes: 41 additions & 1 deletion tests/test_seq_domain_red.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
)