11import numpy as np
2+ import pytest
23
34from bayes_opt import SequentialDomainReductionTransformer
45from bayes_opt import BayesianOptimization
5-
6+ from bayes_opt . target_space import TargetSpace
67
78def black_box_function (x , y ):
89 """Function with unknown internals we wish to maximize.
@@ -104,3 +105,42 @@ def test_minimum_window_array_is_kept():
104105 )
105106 window_widths = np .diff (bounds_transformer .bounds )
106107 assert np .all (np .isclose (np .squeeze (np .min (window_widths , axis = 0 )), window_ranges ))
108+
109+ def test_trimming_bounds ():
110+ """Test if the bounds are trimmed correctly within the bounds"""
111+ def dummy_function (x1 , x2 , x3 , x4 , x5 ):
112+ return 0.0
113+
114+ min_window = 1.0
115+ bounds_transformer = SequentialDomainReductionTransformer (minimum_window = min_window )
116+ pbounds = {
117+ 'x1' : (- 1 , 0.6 ),
118+ 'x2' : (- 1 , 0.5 ),
119+ 'x3' : (- 0.4 , 0.6 ),
120+ 'x4' : (0.3 , 1.3 ),
121+ 'x5' : (- 1 , 0.8 ),
122+ }
123+ target_sp = TargetSpace (target_func = dummy_function , pbounds = pbounds )
124+ bounds_transformer .initialize (target_sp )
125+ new_bounds = np .concatenate ((np .ones ((5 , 1 )) * 0.1 , np .ones ((5 , 1 ))), axis = 1 )
126+ global_bounds = np .asarray (list (pbounds .values ()))
127+
128+ trimmed_bounds = bounds_transformer ._trim (new_bounds , global_bounds )
129+ # check that the bounds are trimmed to the minimum window
130+ # raises ValueError if the bounds are not trimmed correctly
131+ bounds_transformer ._window_bounds_compatiblity (trimmed_bounds )
132+
133+
134+ def test_exceeded_bounds ():
135+ """Raises Value Error if the bounds are exceeded."""
136+ window_ranges = [1.01 , 0.72 ]
137+ bounds_transformer = SequentialDomainReductionTransformer (minimum_window = window_ranges )
138+ pbounds = {'x' : (- 0.5 , 0.5 ), 'y' : (- 0.7 , 0.0 )}
139+ with pytest .raises (ValueError ):
140+ _ = BayesianOptimization (
141+ f = black_box_function ,
142+ pbounds = pbounds ,
143+ verbose = 0 ,
144+ random_state = 1 ,
145+ bounds_transformer = bounds_transformer
146+ )
0 commit comments