Skip to content

Commit 55ffaf5

Browse files
committed
Merge branch 'master' into docstring-overhaul
2 parents 90fa99a + 129caac commit 55ffaf5

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

bayes_opt/domain_reduction.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
reduction transformer as based on Stander and Craig's "On the robustness of a
55
simple domain reduction scheme for simulation-based optimization"
66
"""
7-
from typing import Optional, Union, List
7+
from typing import Optional, Union, List, Dict
88

99
import numpy as np
1010
from .target_space import TargetSpace
@@ -39,12 +39,16 @@ def __init__(
3939
gamma_osc: float = 0.7,
4040
gamma_pan: float = 1.0,
4141
eta: float = 0.9,
42-
minimum_window: Optional[Union[List[float], float]] = 0.0
42+
minimum_window: Optional[Union[List[float], float, Dict[str, float]]] = 0.0
4343
) -> None:
4444
self.gamma_osc = gamma_osc
4545
self.gamma_pan = gamma_pan
4646
self.eta = eta
47-
self.minimum_window_value = minimum_window
47+
if isinstance(minimum_window, dict):
48+
self.minimum_window_value = [item[1] for item in sorted(minimum_window.items(), key=lambda x: x[0])]
49+
else:
50+
self.minimum_window_value = minimum_window
51+
4852

4953
def initialize(self, target_space: TargetSpace) -> None:
5054
"""Initialize all of the parameters.

bayes_opt/target_space.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ def mask(self):
205205

206206
return mask
207207

208+
@property
209+
def mask(self):
210+
'''Returns a boolean array of the points that satisfy the constraint and boundary conditions'''
211+
mask = np.ones_like(self.target, dtype=bool)
212+
213+
# mask points that don't satisfy the constraint
214+
if self._constraint is not None:
215+
mask &= self._constraint.allowed(self._constraint_values)
216+
217+
# mask points that are outside the bounds
218+
if self._bounds is not None:
219+
within_bounds = np.all((self._bounds[:, 0] <= self._params) &
220+
(self._params <= self._bounds[:, 1]), axis=1)
221+
mask &= within_bounds
222+
223+
return mask
224+
208225
def params_to_array(self, params):
209226
"""Convert a dict representation of parameters into an array version.
210227
@@ -314,6 +331,11 @@ def register(self, params, target, constraint_value=None):
314331
if not np.all((self._bounds[:, 0] <= x) & (x <= self._bounds[:, 1])):
315332
warn(f'\nData point {x} is outside the bounds of the parameter space. ', stacklevel=2)
316333

334+
# if x is not within the bounds of the parameter space, warn the user
335+
if self._bounds is not None:
336+
if not np.all((self._bounds[:, 0] <= x) & (x <= self._bounds[:, 1])):
337+
warn(f'\nData point {x} is outside the bounds of the parameter space. ', stacklevel=2)
338+
317339
self._params = np.concatenate([self._params, x.reshape(1, -1)])
318340
self._target = np.concatenate([self._target, [target]])
319341

@@ -463,6 +485,7 @@ def res(self):
463485
Does not report if points are within the bounds of the parameter space.
464486
465487
"""
488+
466489
if self._constraint is None:
467490
params = [dict(zip(self.keys, p)) for p in self.params]
468491

tests/test_seq_domain_red.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,24 @@ def verify_bounds_in_range(new_bounds, global_bounds):
187187
trimmed_bounds = bounds_transformer._trim(new_bounds, global_bounds)
188188
assert verify_bounds_in_range(trimmed_bounds, global_bounds)
189189

190+
191+
def test_minimum_window_dict_ordering():
192+
"""Tests if dictionary input for minimum_window is reordered the same as pbounds"""
193+
window_ranges = {'y': 1, 'x': 3,'w': 1e5}
194+
bounds_transformer = SequentialDomainReductionTransformer(minimum_window=window_ranges)
195+
pbounds = {'y': (-1, 1),'w':(-1e6,1e6), 'x': (-10, 10)}
196+
197+
_ = BayesianOptimization(
198+
f=None,
199+
pbounds=pbounds,
200+
verbose=0,
201+
random_state=1,
202+
bounds_transformer=bounds_transformer
203+
)
204+
190205
if __name__ == '__main__':
191206
r"""
192207
CommandLine:
193208
python tests/test_seq_domain_red.py
194209
"""
195-
pytest.main([__file__])
210+
pytest.main([__file__])

0 commit comments

Comments
 (0)