9
9
from numpy .random import uniform
10
10
from scipy .optimize import minimize
11
11
from scipy .stats import qmc
12
- import warnings
13
12
from ..surrogate_modeling .gp import GaussianProcess
14
13
from .acquisition import LCBacquisition , EIacquisition
15
14
from ..problems .problem import Problem
16
15
from .optproblem import IpoptProb
17
- from ..utils .util import Evaluator
16
+ from ..utils .util import Evaluator , Logger
18
17
19
18
# A base class defining a general framework for Bayesian Optimization
20
19
class BOAlgorithmBase :
@@ -35,6 +34,7 @@ def __init__(self):
35
34
self .x_opt = None # Best observed point
36
35
self .y_opt = None # Best observed value
37
36
self .idx_opt = None # Index of the best observed value in the history
37
+ self .logger = Logger () # logger
38
38
39
39
# Sets the acquisition function type and batch size
40
40
def setAcquisitionType (self , acquisition_type , batch_size = 1 ):
@@ -100,6 +100,8 @@ def __init__(self, prob:Problem, gpsurrogate:GaussianProcess, xtrain, ytrain,
100
100
self .opt_evaluator = options .get ('opt_evaluator' , self .opt_evaluator )
101
101
assert isinstance (self .opt_evaluator , Evaluator )
102
102
103
+ self .logger .setlevel (options .get ('log_level' , "INFO" ))
104
+
103
105
if options and 'opt_solver' in options :
104
106
opt_solver = options ['opt_solver' ]
105
107
assert opt_solver in ["SLSQP" , "trust-constr" , "IPOPT" ], f"Invalid opt_solver: { opt_solver } "
@@ -219,17 +221,17 @@ def optimize(self):
219
221
self .x_hist .append (x_train [- j ].flatten ())
220
222
self .y_hist .append (y_train [- j ].flatten ())
221
223
if self .batch_size == 1 :
222
- print (f"Sample point X:" )
224
+ self . logger . info (f"Sample point X:" )
223
225
else :
224
- print (f"Sample points X:" )
226
+ self . logger . info (f"Sample points X:" )
225
227
for j in range (self .batch_size ):
226
- print (f"{ x_train [- j - 1 ]} " )
228
+ self . logger . info (f"{ x_train [- j - 1 ]} " )
227
229
if self .batch_size == 1 :
228
- print (f"\n Observation Y:" )
230
+ self . logger . info (f"Observation Y:" )
229
231
else :
230
- print (f"\n Observations Y:" )
232
+ self . logger . info (f"Observations Y:" )
231
233
for j in range (self .batch_size ):
232
- print (f"{ y_new [- j - 1 ]} " )
234
+ self . logger . info (f"{ y_new [- j - 1 ]} " )
233
235
234
236
# Save the optimal results and all the training data
235
237
self .idx_opt = np .argmin (self .y_hist )
@@ -238,9 +240,7 @@ def optimize(self):
238
240
self .setTrainingData (x_train , y_train )
239
241
240
242
print (f"\n \n Optimal at BO iteration: { self .idx_opt + 1 } " )
241
- print (f"Optimal point: { self .x_opt .flatten ()} , Optimal value: { self .y_opt } " )
242
- print ()
243
-
243
+ print (f"Optimal point: { self .x_opt .flatten ()} , Optimal value: { self .y_opt } \n \n " )
244
244
245
245
class minimizer_wrapper :
246
246
def __init__ (self , fun , method , bounds , constraints , solver_options ):
@@ -260,15 +260,15 @@ def minimizer_callback(self, x0s):
260
260
y = minimize (self .fun ['obj' ], x0 , method = self .method , bounds = self .bounds , constraints = self .constraints , options = self .solver_options )
261
261
success = y .success
262
262
if not success :
263
- print (y .message )
263
+ self . logger . warning (y .message )
264
264
xopt = y .x
265
265
yopt = y .fun
266
266
elif self .method == "trust-constr" :
267
267
nonlinear_constraint = NonlinearConstraint (self .constraints ['cons' ], self .constraints ['cl' ], self .constraints ['cu' ], jac = self .constraints ['jac' ])
268
268
y = minimize (self .fun ['obj' ], x0 , method = self .method , bounds = self .bounds , constraints = [nonlinear_constraint ], options = self .solver_options )
269
269
success = y .success
270
270
if not success :
271
- print (y .message )
271
+ self . logger . warning (y .message )
272
272
xopt = y .x
273
273
yopt = y .fun
274
274
else :
@@ -281,7 +281,7 @@ def minimizer_callback(self, x0s):
281
281
# ipopt returns 0 as success
282
282
success = True
283
283
else :
284
- warnings . warn (f"Ipopt failed to solve the problem. Status msg: { msg } " )
284
+ self . logger . warning (f"Ipopt failed to solve the problem. Status msg: { msg } " )
285
285
success = False
286
286
287
287
yopt = info ['obj_val' ]
0 commit comments