3535 BayesianOptimizer ,
3636 OutputEvaluator ,
3737 BayesianOptimizerOutputEvaluator ,
38+ BayesianOptimizerController ,
3839)
3940
4041from climada .util .calibrate .test .test_base import hazard , exposure
@@ -173,14 +174,18 @@ def setUp(self) -> None:
173174 def test_single (self ):
174175 """Test with single parameter"""
175176 self .input .bounds = {"slope" : (- 1 , 3 )}
177+ controller = BayesianOptimizerController (
178+ init_points = 10 , n_iter = 20 , max_iterations = 1
179+ )
176180 optimizer = BayesianOptimizer (self .input , random_state = 1 )
177- output = optimizer .run (init_points = 10 , n_iter = 20 )
181+ output = optimizer .run (controller )
178182
179183 # Check result (low accuracy)
180184 self .assertAlmostEqual (output .params ["slope" ], 1.0 , places = 2 )
181185 self .assertAlmostEqual (output .target , 0.0 , places = 3 )
182186 self .assertEqual (output .p_space .dim , 1 )
183187 self .assertTupleEqual (output .p_space_to_dataframe ().shape , (30 , 2 ))
188+ self .assertEqual (controller .iterations , 1 )
184189
185190 def test_multiple_constrained (self ):
186191 """Test with multiple constrained parameters"""
@@ -204,13 +209,17 @@ def test_multiple_constrained(self):
204209 self .input .bounds = {"intensity_1" : (- 1 , 4 ), "intensity_2" : (- 1 , 4 )}
205210 # Run optimizer
206211 optimizer = BayesianOptimizer (self .input , random_state = 1 )
207- output = optimizer .run (n_iter = 200 )
212+ controller = BayesianOptimizerController .from_input (
213+ self .input , sampling_base = 5 , max_iterations = 3
214+ )
215+ output = optimizer .run (controller )
208216
209217 # Check results (low accuracy)
210218 self .assertEqual (output .p_space .dim , 2 )
211219 self .assertAlmostEqual (output .params ["intensity_1" ], 1.0 , places = 2 )
212220 self .assertAlmostEqual (output .params ["intensity_2" ], 3.0 , places = 1 )
213221 self .assertAlmostEqual (output .target , 0.0 , places = 3 )
222+ self .assertGreater (controller .iterations , 1 )
214223
215224 # Check constraints in parameter space
216225 p_space = output .p_space_to_dataframe ()
@@ -224,7 +233,8 @@ def test_multiple_constrained(self):
224233 ("Calibration" , "Allowed" ),
225234 },
226235 )
227- self .assertTupleEqual (p_space .shape , (300 , 5 ))
236+ self .assertGreater (p_space .shape [0 ], 50 ) # Two times random iterations
237+ self .assertEqual (p_space .shape [1 ], 5 )
228238 p_allowed = p_space .loc [p_space ["Calibration" , "Allowed" ], "Parameters" ]
229239 npt .assert_array_equal (
230240 (p_allowed ["intensity_1" ] < p_allowed ["intensity_2" ]).to_numpy (),
@@ -235,7 +245,10 @@ def test_plots(self):
235245 """Check if executing the default plots works"""
236246 self .input .bounds = {"slope" : (- 1 , 3 )}
237247 optimizer = BayesianOptimizer (self .input , random_state = 1 )
238- output = optimizer .run (init_points = 10 , n_iter = 20 )
248+ controller = BayesianOptimizerController .from_input (
249+ self .input , max_iterations = 1
250+ )
251+ output = optimizer .run (controller )
239252
240253 output_eval = OutputEvaluator (self .input , output )
241254 output_eval .impf_set .plot ()
0 commit comments