设置skopt中函数f(x1,x2)在x各维上的边界



我有一个函数f(x1,x2),并且想使用skopt为x的两个维度设置gp_minimization的边界。

只有一个变量(x1),效果很好:

def func(x1):
return x1[0]
bounds = [(1.0, 10.0)]
gp_res = gp_minimize(func, dimensions=bounds, acq_func="EI", n_calls=100, random_state=0)

但是使用一个有多个变量的函数,比如f(x1,x2),我需要加上第二个变量的边界。我试着这样做:

def func(x1, x2):
return x1[0][0]*x2[0][1]
bounds = [[(1.0, 10.0),(10.1, 9.9)]]
bounds[0][0] #(1.0,10.0) To set the bounds for x1
bounds[0][1] #(10.1,9.9) To set the bounds for x2
gp_res = gp_minimize(func=func, dimensions=bounds, acq_func="EI", n_calls=100, random_state=0)

我得到错误信息:ValueError: Invalid dimension[(1.0, 4.0),(1.1, 3.9)]。阅读文档了解支持的类型。

将边界更改为:

def func(x1, x2):
return x1[0][0]*x2[0][1]
bounds = [(1.0, 10.0),(10.1, 9.9)]
gp_res = gp_minimize(func=func, dimensions=bounds, acq_func="EI", n_calls=100, random_state=0)

我收到以下错误信息:TypeError: func()缺少1个必需的位置参数:'x2'

你能帮我解决这个问题吗?如何设置两个变量的边界?参照这个例子(Tim Head, 2016年7月)。Holger Nahrstaedt重新格式化2020):https://scikit-optimize.github.io/stable/auto_examples/strategy-comparison.html sphx-glr-download-auto-examples-strategy-comparison-py

解决了

解决方案:

dim1 = Real(name='x1', low=1.0, high=100.0)
dim2 = Real(name='x2', low=1.0, high=100.0)
bounds = [dim1, dim2]
@use_named_args(dimensions=bounds)
def func(x1, x2):
return x1*x2

最新更新