Skip to content

Commit 129caac

Browse files
authored
Update domain_reduction.py (#454)
* Update domain_reduction.py Adds ability to pass a dictionary as the minimum_window argument in SequentialDomainReductionTransformer. Deals with issue where TargetSpace sorts the pBounds dictionary, causing the order of the list used for minimum_window to possibly not match the order of the stored boundaries. * Update test_seq_domain_red.py Added test_minimum_window_dict_ordering to test whether dictionary input for SequentialDomainReductionTransformer is ordered to match pBounds.
1 parent 844927b commit 129caac

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

bayes_opt/domain_reduction.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Union, List
1+
from typing import Optional, Union, List, Dict
22

33
import numpy as np
44
from .target_space import TargetSpace
@@ -29,12 +29,16 @@ def __init__(
2929
gamma_osc: float = 0.7,
3030
gamma_pan: float = 1.0,
3131
eta: float = 0.9,
32-
minimum_window: Optional[Union[List[float], float]] = 0.0
32+
minimum_window: Optional[Union[List[float], float, Dict[str, float]]] = 0.0
3333
) -> None:
3434
self.gamma_osc = gamma_osc
3535
self.gamma_pan = gamma_pan
3636
self.eta = eta
37-
self.minimum_window_value = minimum_window
37+
if isinstance(minimum_window, dict):
38+
self.minimum_window_value = [item[1] for item in sorted(minimum_window.items(), key=lambda x: x[0])]
39+
else:
40+
self.minimum_window_value = minimum_window
41+
3842

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

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)