code: def test_bayes_optimizaion(self):
      print("Start bayesian optimization")
  
      # 1. Initialize parameters
      acquisition_function_kappa = 5
      init_point_number = 3
      iteration_number = 3
      iteration_index = 0
      train_features = []
      train_labels = []
  
      gp = GaussianProcessRegressor(
          kernel=Matern(nu=2.5),
          n_restarts_optimizer=25,
      )
  
      bound_dict = {"x": (-4, 4), "y": (-3, 3)}
      # Example: [[-4,  4], [-3,  3]]
      bounds = []
      for key in bound_dict.keys():
          bounds.append(bound_dict[key])
      # Example: ndarray([[-4,  4], [-3,  3]])
      bounds = np.asarray(bounds)
  
      # 2. Get init random samples
      # Example: array([-3.66909025, -1.93270006, 1.36095631])
      init_xs = np.random.uniform(-4, 4, size=init_point_number)
      # Example: array([-0.84486644, -0.95367483, 0.61358525])
      init_ys = np.random.uniform(-3, 3, size=init_point_number)
  
      # Example: [[-3.66909025, -0.84486644], [-1.93270006, -0.95367483], [1.36095631, 0.61358525]]
      init_points = []
      for i in range(init_point_number):
          init_points.append([init_xs[i], init_ys[i]])
  
      # Example: [-4.4555402320291684, -7.9016857176523114]
      init_labels = []
      for point in init_points:
          init_labels.append(self.test_function(point[0], point[1]))
  
      # 3. GP compute the prior
      train_features = np.asarray(init_points)
      train_labels = np.asarray(init_labels)
      current_max_label = train_labels.max()
  
      gp.fit(train_features, train_labels)
  
      # 4. Acquision function computes the max value
      # Example: [[-3.66909025, -0.84486644], [-1.93270006, -0.95367483], [1.36095631, 0.61358525], ...], shape is [100000, 2]
      x_tries = np.random.uniform(
          bounds[:, 0], bounds[:, 1], size=(100000, bounds.shape[0])
      )
  
      mean, std = gp.predict(x_tries, return_std=True)
      # Confidence bound criteria
      acquisition_fucntion_values = (
          mean + acquisition_function_kappa * std
      )
      x_max = x_tries[acquisition_fucntion_values.argmax()]
      max_acquision_fucntion_value = acquisition_fucntion_values.max()
  
      x_max = np.clip(x_max, bounds[:, 0], bounds[:, 1])
      print("Current max acquision function choose: {}".format(x_max))
  
      for i in range(iteration_number):
          iteration_index += 1
  
          # 5. Choose the best and compute to add in train dataset
          train_features = np.vstack(
              (train_features, x_max.reshape((1, -1)))
          )
          train_labels = np.append(
              train_labels, self.test_function(x_max[0], x_max[1])
          )
  
          # 6. Re-compute gaussian process and acquistion function
          gp.fit(train_features, train_labels)
  
          # Update maximum value
          if train_labels[-1] > current_max_label:
              current_max_label = train_labels[-1]
              print("Get the better parameters!")
  
          x_tries = np.random.uniform(
              bounds[:, 0], bounds[:, 1], size=(100000, bounds.shape[0])
          )
  
          mean, std = gp.predict(x_tries, return_std=True)
          acquisition_fucntion_values = (
              mean + acquisition_function_kappa * std
          )
          x_max = x_tries[acquisition_fucntion_values.argmax()]
          max_acquision_fucntion_value = (
              acquisition_fucntion_values.max()
          )
  
          x_max = np.clip(x_max, bounds[:, 0], bounds[:, 1])
          print(
              "Max label: {}, current label: {}, acquision function choose: {}".format(
                  current_max_label, train_labels[-1], x_max
              )
          )
  
file_path: advisor/advisor_server/suggestion/algorithm/bayesian_optimization.py
imports: from __future__ import print_function
  from __future__ import division
  import json
  import numpy
  from sklearn.gaussian_process import GaussianProcessRegressor
  from sklearn.gaussian_process.kernels import Matern
  from suggestion.models import Study
  from suggestion.models import Trial
  from suggestion.algorithm.abstract_algorithm import AbstractSuggestionAlgorithm
  from suggestion.algorithm.random_search import RandomSearchAlgorithm
label_desc: Use image node_id 3 for calling the BayesianOptimizationDemo obj's underlying member method code with example usage: obj.test_bayes_optimizaion() without return types