微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 skopt 中为函数 f(x1,x2) 设置 x 的每个维度的界限

如何解决在 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,random_state=0)

我收到错误消息: ValueError: 无效维度 [(1.0,4.0),(1.1,3.9)]。阅读支持类型的文档。

通过改变边界:

def func(x1,x2):
    return x1[0][0]*x2[0][1]

bounds = [(1.0,9.9)]

gp_res = gp_minimize(func=func,random_state=0)

我收到以下错误消息: 类型错误: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',high=100.0)
bounds = [dim1,dim2]

@use_named_args(dimensions=bounds)
def func(x1,x2):
 return x1*x2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。