1919class BOAlgorithmBase :
2020 def __init__ (self ):
2121 self .acquisition_type = "LCB" # Type of acquisition function (default = "LCB")
22+ self .batch_type = "KB" # strategy for qEI
2223 self .xtrain = None # Training data
2324 self .ytrain = None # Training data
2425 self .prob = None # Problem structure
2526 self .bo_maxiter = 20 # Maximum number of Bayesian optimization steps
2627 self .n_start = 10 # estimating acquisition global optima by determining local optima n_start times and then determining the discrete max of that set
27- self .q = 1 # batch size
28+ self .batch_size = 1 # batch size
2829 # save some internal member train
2930 self .y_hist = None # History of evaluations
3031 self .x_hist = None # History of evaluations
@@ -33,9 +34,9 @@ def __init__(self):
3334 self .idx_opt = None # Index of the best observed value in the history
3435
3536 # Sets the acquisition function type and batch size
36- def setAcquisitionType (self , acquisition_type , q = 1 ):
37+ def setAcquisitionType (self , acquisition_type , batch_size = 1 ):
3738 self .acquisition_type = acquisition_type
38- self .q = q
39+ self .batch_size = batch_size
3940
4041 # Sets the training data
4142 def setTrainingData (self , xtrain , ytrain ):
@@ -66,7 +67,7 @@ def getOptimalObjective(self):
6667class BOAlgorithm (BOAlgorithmBase ):
6768 def __init__ (self , prob :Problem , gpsurrogate :GaussianProcess , xtrain , ytrain ,
6869 user_grad = None ,
69- options = None ):
70+ options = {} ):
7071 super ().__init__ ()
7172
7273 assert isinstance (gpsurrogate , GaussianProcess )
@@ -77,21 +78,20 @@ def __init__(self, prob:Problem, gpsurrogate:GaussianProcess, xtrain, ytrain,
7778 self .bounds = self .gpsurrogate .get_bounds ()
7879 self .fun_grad = None
7980
80- if options and 'bo_maxiter' in options :
81- self .bo_maxiter = options ['bo_maxiter' ]
82- assert self .bo_maxiter > 0 , f"Invalid bo_maxiter: { self .bo_maxiter } "
83-
84- if options and 'solver_options' in options :
85- self .solver_options = options ['solver_options' ]
86- else :
87- self .solver_options = {"maxiter" : 200 }
88-
89- if options and 'acquisition_type' in options :
90- acquisition_type = options ['acquisition_type' ]
91- assert acquisition_type in ["LCB" , "EI" ], f"Invalid acquisition_type: { acquisition_type } "
92- else :
93- acquisition_type = "LCB"
94- self .setAcquisitionType (acquisition_type )
81+ self .bo_maxiter = options .get ('bo_maxiter' , self .bo_maxiter )
82+ assert self .bo_maxiter > 0 , f"Invalid bo_maxiter: { self .bo_maxiter } "
83+
84+ self .solver_options = {"maxiter" : 200 }
85+ self .solver_options = options .get ('solver_options' , self .solver_options )
86+
87+ acquisition_type = options .get ('acquisition_type' , "LCB" )
88+ assert acquisition_type in ["LCB" , "EI" ], f"Invalid acquisition_type: { acquisition_type } "
89+ batch_size = options .get ('batch_size' , 1 )
90+ assert isinstance (batch_size , int ), f"batch_size { batch_size } not an integer"
91+ assert batch_size > 0 , f"batch_size { batch_size } is not strictly positive"
92+ assert ((batch_size == 1 and acquisition_type == "LCB" ) or (acquisition_type == "EI" )), \
93+ f"batched BO only supported for expected-improvement"
94+ self .setAcquisitionType (acquisition_type , batch_size )
9595
9696 if options and 'opt_solver' in options :
9797 opt_solver = options ['opt_solver' ]
@@ -100,6 +100,7 @@ def __init__(self, prob:Problem, gpsurrogate:GaussianProcess, xtrain, ytrain,
100100 opt_solver = "SLSQP"
101101 self .set_method (opt_solver )
102102
103+
103104 if user_grad :
104105 self .fun_grad = user_grad
105106
0 commit comments