@@ -406,154 +406,52 @@ def verify_optimizers_match(optimizer1, optimizer2):
406406 assert suggestion1 == suggestion2 , f"\n Suggestion 1: { suggestion1 } \n Suggestion 2: { suggestion2 } "
407407
408408
409- def test_integration_upper_confidence_bound (target_func_x_and_y , pbounds , tmp_path ):
410- """Test save/load integration with UpperConfidenceBound acquisition."""
411- acquisition_function = UpperConfidenceBound (kappa = 2.576 )
412-
413- # Create and run first optimizer
414- optimizer = BayesianOptimization (
415- f = target_func_x_and_y ,
416- pbounds = pbounds ,
417- acquisition_function = acquisition_function ,
418- random_state = 1 ,
419- verbose = 0 ,
420- )
421- optimizer .maximize (init_points = 2 , n_iter = 3 )
422-
423- # Save state
424- state_path = tmp_path / "ucb_state.json"
425- optimizer .save_state (state_path )
426-
427- # Create new optimizer and load state
428- new_optimizer = BayesianOptimization (
429- f = target_func_x_and_y ,
430- pbounds = pbounds ,
431- acquisition_function = UpperConfidenceBound (kappa = 2.576 ),
432- random_state = 1 ,
433- verbose = 0 ,
434- )
435- new_optimizer .load_state (state_path )
436-
437- verify_optimizers_match (optimizer , new_optimizer )
438-
439-
440- def test_integration_probability_improvement (target_func_x_and_y , pbounds , tmp_path ):
441- """Test save/load integration with ProbabilityOfImprovement acquisition."""
442- acquisition_function = ProbabilityOfImprovement (xi = 0.01 )
443-
444- optimizer = BayesianOptimization (
445- f = target_func_x_and_y ,
446- pbounds = pbounds ,
447- acquisition_function = acquisition_function ,
448- random_state = 1 ,
449- verbose = 0 ,
450- )
451- optimizer .maximize (init_points = 2 , n_iter = 3 )
452-
453- state_path = tmp_path / "pi_state.json"
454- optimizer .save_state (state_path )
455-
456- new_optimizer = BayesianOptimization (
457- f = target_func_x_and_y ,
458- pbounds = pbounds ,
459- acquisition_function = ProbabilityOfImprovement (xi = 0.01 ),
460- random_state = 1 ,
461- verbose = 0 ,
462- )
463- new_optimizer .load_state (state_path )
464-
465- verify_optimizers_match (optimizer , new_optimizer )
466-
467-
468- def test_integration_expected_improvement (target_func_x_and_y , pbounds , tmp_path ):
469- """Test save/load integration with ExpectedImprovement acquisition."""
470- acquisition_function = ExpectedImprovement (xi = 0.01 )
471-
472- optimizer = BayesianOptimization (
473- f = target_func_x_and_y ,
474- pbounds = pbounds ,
475- acquisition_function = acquisition_function ,
476- random_state = 1 ,
477- verbose = 0 ,
478- )
479- optimizer .maximize (init_points = 2 , n_iter = 3 )
480-
481- state_path = tmp_path / "ei_state.json"
482- optimizer .save_state (state_path )
483-
484- new_optimizer = BayesianOptimization (
485- f = target_func_x_and_y ,
486- pbounds = pbounds ,
487- acquisition_function = ExpectedImprovement (xi = 0.01 ),
488- random_state = 1 ,
489- verbose = 0 ,
490- )
491- new_optimizer .load_state (state_path )
492-
493- verify_optimizers_match (optimizer , new_optimizer )
494-
495-
496- def test_integration_constant_liar (target_func_x_and_y , pbounds , tmp_path ):
497- """Test save/load integration with ConstantLiar acquisition."""
498- base_acq = UpperConfidenceBound (kappa = 2.576 )
499- acquisition_function = ConstantLiar (base_acquisition = base_acq )
500-
501- optimizer = BayesianOptimization (
502- f = target_func_x_and_y ,
503- pbounds = pbounds ,
504- acquisition_function = acquisition_function ,
505- random_state = 1 ,
506- verbose = 0 ,
507- )
508- optimizer .maximize (init_points = 2 , n_iter = 3 )
509-
510- state_path = tmp_path / "cl_state.json"
511- optimizer .save_state (state_path )
512-
513- new_optimizer = BayesianOptimization (
514- f = target_func_x_and_y ,
515- pbounds = pbounds ,
516- acquisition_function = ConstantLiar (base_acquisition = UpperConfidenceBound (kappa = 2.576 )),
517- random_state = 1 ,
518- verbose = 0 ,
519- )
520- new_optimizer .load_state (state_path )
521-
522- verify_optimizers_match (optimizer , new_optimizer )
523-
524-
525- def test_integration_gp_hedge (target_func_x_and_y , pbounds , tmp_path ):
526- """Test save/load integration with GPHedge acquisition."""
527- base_acquisitions = [
528- UpperConfidenceBound (kappa = 2.576 ),
529- ProbabilityOfImprovement (xi = 0.01 ),
530- ExpectedImprovement (xi = 0.01 ),
531- ]
532- acquisition_function = GPHedge (base_acquisitions = base_acquisitions )
409+ @pytest .mark .parametrize (
410+ ("acquisition_fn_factory" , "state_filename" , "extra_kwargs" ),
411+ [
412+ (lambda : UpperConfidenceBound (kappa = 2.576 ), "ucb_state.json" , {}),
413+ (lambda : ProbabilityOfImprovement (xi = 0.01 ), "pi_state.json" , {}),
414+ (lambda : ExpectedImprovement (xi = 0.01 ), "ei_state.json" , {}),
415+ (lambda : ConstantLiar (base_acquisition = UpperConfidenceBound (kappa = 2.576 )), "cl_state.json" , {}),
416+ (
417+ lambda : GPHedge (
418+ base_acquisitions = [
419+ UpperConfidenceBound (kappa = 2.576 ),
420+ ProbabilityOfImprovement (xi = 0.01 ),
421+ ExpectedImprovement (xi = 0.01 ),
422+ ]
423+ ),
424+ "gphedge_state.json" ,
425+ {},
426+ ),
427+ ],
428+ )
429+ def test_integration_acquisition_functions (
430+ acquisition_fn_factory , state_filename , extra_kwargs , target_func_x_and_y , pbounds , tmp_path
431+ ):
432+ """Parametrized integration test for acquisition functions."""
433+ acquisition_function = acquisition_fn_factory ()
533434
534435 optimizer = BayesianOptimization (
535436 f = target_func_x_and_y ,
536437 pbounds = pbounds ,
537438 acquisition_function = acquisition_function ,
538439 random_state = 1 ,
539440 verbose = 0 ,
441+ ** extra_kwargs ,
540442 )
541443 optimizer .maximize (init_points = 2 , n_iter = 3 )
542444
543- state_path = tmp_path / "gphedge_state.json"
445+ state_path = tmp_path / state_filename
544446 optimizer .save_state (state_path )
545447
546- new_base_acquisitions = [
547- UpperConfidenceBound (kappa = 2.576 ),
548- ProbabilityOfImprovement (xi = 0.01 ),
549- ExpectedImprovement (xi = 0.01 ),
550- ]
551448 new_optimizer = BayesianOptimization (
552449 f = target_func_x_and_y ,
553450 pbounds = pbounds ,
554- acquisition_function = GPHedge ( base_acquisitions = new_base_acquisitions ),
451+ acquisition_function = acquisition_fn_factory ( ),
555452 random_state = 1 ,
556453 verbose = 0 ,
454+ ** extra_kwargs ,
557455 )
558456 new_optimizer .load_state (state_path )
559457
0 commit comments