Python 使用GpyOpt时如何添加限制条件?
目前,我尝试使用GPyOpt最小化函数并获得优化的参数Python 使用GpyOpt时如何添加限制条件?,python,data-science,bayesian,gpyopt,Python,Data Science,Bayesian,Gpyopt,目前,我尝试使用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
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)形式的约束我找到了一种更快的方法
如果您需要X0+X1…..Xn==100000000,则只需将X0+X1….Xn-1赋给GpyOpt
在GpyOpt给你(X0+X1…..Xn-1)之后,你可以得到
Xn=100000000-和(X0+X1…..Xn-1)
我对边界的解释是,每个变量的值可以是1或1000000。这是你的意图,还是你想让所有的价值观都介于两者之间?
(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
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)