# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import warnings
warnings.filterwarnings('ignore')
from bayes_opt import BayesianOptimization
# use sklearn's default parameters for theta and random_start
gp_params = {"alpha": 1e-5, "n_restarts_optimizer": 2}

# ## 1. 定义需要最大化的函数f，以及参数边界bo.bounds

bo = BayesianOptimization(lambda x, y: -x ** 2 - (y - 1) ** 2 + 1,
                          {'x': (-4, 4), 'y': (-3, 3)}) # 边界bo.bounds

# ## 2. 先验知识: 添加已知点（可不加）

bo.initialize(
    {
        'target': [-1, -1],
        'x': [1, 1],
        'y': [0, 2]
    }
)

# ## 3. 添加算法探索点

# bo.init_points
bo.explore({'x': [-1, 3], 'y': [-2, 2]})

# ## 4. 初始化后，maximize使得算法最优
# - ucb: kappa
# - ei: xi
# - poi: xi

# init_points=5, n_iter=25, acq='ucb'|'ei'|'poi', kappa=2.576, xi=0.0
bo.maximize(n_iter=5, kappa=1)

# ## 5. 若不能满足需求，添加更多探索点（从3.继续）

bo.explore({'x': [0.6], 'y': [-0.23]})
bo.maximize(n_iter=5, acq='ei', **gp_params)

# Finally, we take a look at the final results.
print(bo.res['max'])
# print(bo.res['all'])

StarcoderdataJupyterScriptsDedupFiltered