gpt4 book ai didi

python - 使用 GpyOpt 时如何添加限制条件?

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:50 26 4
gpt4 key购买 nike

目前我尝试使用 GPyOpt 最小化函数并优化参数。

import GPy
import GPyOpt
from math import log
def f(x):
x0,x1,x2,x3,x4,x5 = x[:,0],x[:,1],x[:,2],x[:,3],x[:,4],x[:,5],
f0 = 0.2 * log(x0)
f1 = 0.3 * log(x1)
f2 = 0.4 * log(x2)
f3 = 0.2 * log(x3)
f4 = 0.5 * log(x4)
f5 = 0.2 * log(x5)
return -(f0 + f1 + f2 + f3 + f4 + f5)

bounds = [
{'name': 'x0', 'type': 'discrete', 'domain': (1,1000000)},
{'name': 'x1', 'type': 'discrete', 'domain': (1,1000000)},
{'name': 'x2', 'type': 'discrete', 'domain': (1,1000000)},
{'name': 'x3', 'type': 'discrete', 'domain': (1,1000000)},
{'name': 'x4', 'type': 'discrete', 'domain': (1,1000000)},
{'name': 'x5', 'type': 'discrete', 'domain': (1,1000000)}
]

myBopt = GPyOpt.methods.BayesianOptimization(f=f, domain=bounds)
myBopt.run_optimization(max_iter=100)
print(myBopt.x_opt)
print(myBopt.fx_opt)

我想给这个函数加上限制条件。这是一个例子。

x0 + x1 + x2 + x3 + x4 + x5 == 100000000

我该如何修改这段代码?

最佳答案

GPyOpt 只支持 c(x0, x1, ..., xn) <= 0 形式的约束,所以你能做的最好的事情就是选择一个足够小的值并将你拥有的约束表达式“夹在中间”。假设 0.1 足够小,那么您可以这样做:

(x0 + x1 + x2 + x3 + x4 + x5) - 100000000 <= 0.1
(x0 + x1 + x2 + x3 + x4 + x5) - 100000000 >= -0.1

然后

(x0 + x1 + x2 + x3 + x4 + x5) - 100000000 - 0.1 <= 0
100000000 - (x0 + x1 + x2 + x3 + x4 + x5) - 0.1 <= 0

API 看起来像这样:

constraints = [
{
'name': 'constr_1',
'constraint': '(x[:,0] + x[:,1] + x[:,2] + x[:,3] + x[:,4] + x[:,5]) - 100000000 - 0.1'
},
{
'name': 'constr_2',
'constraint': '100000000 - (x[:,0] + x[:,1] + x[:,2] + x[:,3] + x[:,4] + x[:,5]) - 0.1'
}
]

myBopt = GPyOpt.methods.BayesianOptimization(f=f, domain=bounds, constraints = constraints)

关于python - 使用 GpyOpt 时如何添加限制条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com