Consider the following problem:
import numpy
import scipy.optimize
def f(x):
return (x[0] == 1)*(x[1] + 2)**2 - (x[0] == 0)*(x[1] + 1)**2
kwargs = {
'method': 'trust-constr',
'jac': False,
'bounds': [(0, 1), (0, 1)],
}
m = scipy.optimize.minimize(f, numpy.array([1, 0]), **kwargs).x
print(m)
# [0.91136811 0.19026955] <- wrong result
I would like to optimize this function on the space
x[0] \in {0,1}
x[1] \in [0,1]
Is there any way to specify that x[0] should not be a real value (i,e. a value on the line [0,1]
), and instead only either 0 or 1?
My current take would be to perform N optimizations, one per option of x[0]. The problem is that this quickly explodes if there are multiple categorical variables.