Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bounds_transformer could bypass global_bounds due to the test logic within _trim function in domain_reduction.py #441

Merged
merged 15 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 16 additions & 8 deletions bayes_opt/domain_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,23 @@ def _update(self, target_space: TargetSpace) -> None:
self.r = self.contraction_rate * self.r

def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array:
for i, variable in enumerate(new_bounds):
if variable[0] < global_bounds[i, 0]:
variable[0] = global_bounds[i, 0]
if variable[1] > global_bounds[i, 1]:
variable[1] = global_bounds[i, 1]
""" Tests new_bounds to enforce global_bounds and minimum_window."""
# sort the order of the bounds for each parameter
for i, pbounds in enumerate(new_bounds):
if pbounds[0] > pbounds[1]:
new_bounds[i, 0] = pbounds[1]
new_bounds[i, 1] = pbounds[0]
perezed00 marked this conversation as resolved.
Show resolved Hide resolved

# check if the new bounds exceed the global bounds
for i, pbounds in enumerate(new_bounds):
# check if lower bound exceeds range
if (pbounds[0] < global_bounds[i, 0] or pbounds[0] > global_bounds[i, 1]):
pbounds[0] = global_bounds[i, 0]
# check if upper bound exceeds range
if (pbounds[1] > global_bounds[i, 1] or pbounds[1] < global_bounds[i, 0]):
pbounds[1] = global_bounds[i, 1]
till-m marked this conversation as resolved.
Show resolved Hide resolved

for i, entry in enumerate(new_bounds):
if entry[0] > entry[1]:
new_bounds[i, 0] = entry[1]
new_bounds[i, 1] = entry[0]
window_width = abs(entry[0] - entry[1])
if window_width < self.minimum_window[i]:
dw = (self.minimum_window[i] - window_width) / 2.0
Expand Down
34 changes: 33 additions & 1 deletion tests/test_seq_domain_red.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,36 @@ def test_exceeded_bounds():
verbose=0,
random_state=1,
bounds_transformer=bounds_transformer
)
)

def test_trim_both_new_bounds_beyond_gloabal_bounds():
"""Test if the global bounds are respected when both new bounds for a given parameter
are beyond the global bounds."""

# initialize a bounds transformer
bounds_transformer = SequentialDomainReductionTransformer(minimum_window=10)
pbounds = {'x': (-10, 10),'y': (-10, 10)}
target_sp = TargetSpace(target_func=black_box_function, pbounds=pbounds)
bounds_transformer.initialize(target_sp)
global_bounds = np.asarray(list(pbounds.values()))

def verify_bounds_in_range(new_bounds, global_bounds):
"""Check if the new bounds are within the global bounds."""
test = True
for i, pbounds in enumerate(new_bounds):
if (pbounds[0] < global_bounds[i, 0] or pbounds[0] > global_bounds[i, 1]):
test = False
if (pbounds[1] > global_bounds[i, 1] or pbounds[1] < global_bounds[i, 0]):
test = False
return test

# test if both bounds for one parameter are beyond the global bounds
new_bounds = np.array( [[-50, -20], [-10, 10]] )
trimmed_bounds = bounds_transformer._trim(new_bounds, global_bounds)
assert verify_bounds_in_range(trimmed_bounds, global_bounds)

# test if both bounds for one parameter are beyond the global bounds
# while they are out of order
new_bounds = np.array( [[-20, -50], [-10, 10]] )
trimmed_bounds = bounds_transformer._trim(new_bounds, global_bounds)
assert verify_bounds_in_range(trimmed_bounds, global_bounds)