def run(): # Defining the bounds and dimensions of the input space X_lower = np.array([0]) X_upper = np.array([6]) dims = 1 # Set the method that we will use to optimize the acquisition function maximizer = stochastic_local_search # Defining the method to model the objective function kernel = GPy.kern.Matern52(input_dim=dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) # The acquisition function that we optimize in order to pick a new x acquisition_func = EI( model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1 ) # par is the minimum improvement that a point has to obtain # Draw one random point and evaluate it to initialize BO X = np.array([np.random.uniform(X_lower, X_upper, dims)]) Y = objective_function(X) # Fit the model on the data we observed so far model.train(X, Y) # Update the acquisition function model with the retrained model acquisition_func.update(model) # Optimize the acquisition function to obtain a new point new_x = maximizer(acquisition_func, X_lower, X_upper) # Evaluate the point and add the new observation to our set of previous seen points new_y = objective_function(np.array(new_x)) X = np.append(X, new_x, axis=0) Y = np.append(Y, new_y, axis=0) # Visualize the objective function, model and the acquisition function fig = plt.figure() # Sub plot for the model and the objective function ax1 = fig.add_subplot(2, 1, 1) # Sub plot for the acquisition function ax2 = fig.add_subplot(2, 1, 2) resolution = 0.1 # Call plot_model function ax1 = plotting.plot_model(model, X_lower, X_upper, ax1, resolution, "b", "blue", "Prosterior Mean", 3, True) # Call plot_objective_function ax1 = plotting.plot_objective_function( objective_function, X_lower, X_upper, X, Y, ax1, resolution, "black", "ObjectiveFunction", True ) ax1.set_title("Model + Objective Function") # Call plot_acquisition_function ax2 = plotting.plot_acquisition_function( acquisition_func, X_lower, X_upper, X, ax2, resolution, "AcquisitionFunction", True ) plt.savefig("test2.png") os.system("eog test2.png&")
acquisition_func = EI(model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1) maximizer = DIRECT bo = BayesianOptimization(acquisition_fkt=acquisition_func, model=model, maximize_fkt=maximizer, X_lower=X_lower, X_upper=X_upper, dims=dims, objective_fkt=objective_function) bo.run(num_iterations=5) X, Y = bo.get_observations() X = X[:-1] Y = Y[:-1] model = bo.get_model() f, (ax1, ax2) = plt.subplots(2, sharex=True) ax1 = plot_model(model, X_lower, X_upper, ax1) ax1 = plot_objective_function(objective_function, X_lower, X_upper, X, Y, ax1) ax1.legend() acquisition_func = EI(model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1) ax2 = plot_acquisition_function(acquisition_func, X_lower, X_upper, ax2) plt.legend() plt.savefig("bo.png")
# Set the method that we will use to optimize the acquisition function maximizer = GridSearch(acquisition_func, task.X_lower, task.X_upper) # Draw one random point and evaluate it to initialize BO X = np.array([np.random.uniform(task.X_lower, task.X_upper, task.n_dims)]) Y = task.objective_function(X) # This is the main Bayesian optimization loop for i in range(10): # Fit the model on the data we observed so far model.train(X, Y) # Update the acquisition function model with the retrained model acquisition_func.update(model) # Optimize the acquisition function to obtain a new point new_x = maximizer.maximize() # Evaluate the point and add the new observation to our set of previous seen points new_y = task.objective_function(np.array(new_x)) X = np.append(X, new_x, axis=0) Y = np.append(Y, new_y, axis=0) # Visualize the objective function, model and the acquisition function f, (ax1, ax2) = plt.subplots(2, sharex=True) ax1 = plot_objective_function(task.objective_function, task.X_lower, task.X_upper, X, Y, ax1) ax1 = plot_model(model, task.X_lower, task.X_upper, ax1) ax2 = plot_acquisition_function(acquisition_func, task.X_lower, task.X_upper, ax2) plt.show(block=True)