How do I add limiting conditions when using GpyOpt

2019-04-16 10:38发布

Currently I try to minimize the function and get optimized parameters using 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) 

I want to add limiting conditions to this function. Here is an example.

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

How should I modify this code?

2条回答
欢心
2楼-- · 2019-04-16 11:14

GPyOpt only supports constrains in a form of c(x0, x1, ..., xn) <= 0, so the best you can do is to pick a small enough value and "sandwich" the constrain expression that you have with it. Let's say 0.1 is sufficiently small, then you could do this:

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

and then

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

The API would look like that:

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)
查看更多
唯我独甜
3楼-- · 2019-04-16 11:26

I found a faster way.

If you need X0+X1.....Xn ==100000000 , you only give X0+X1....Xn-1 to GpyOpt.

After GpyOpt give you (X0+X1.....Xn-1), you can get Xn = 100000000 - sum(X0+X1.....Xn-1)

查看更多
登录 后发表回答