I have the following simple python code:
from scipy.optimize import minimize
def cons_function(x):
return x[0] - 1000
def objective(x):
a = x[0]
return a ** 5
x0 = [0]
cons_dict = [{'type':'ineq', 'fun': cons_function}]
res = minimize(objective, x0, constraints=cons_dict)
print('x: ', res.x)
print('fun: ',res.fun)
And the output is as follows:
x: [-2.64828354e+12]
fun: -1.3026339977067573e+62
But the value of x
is supposed to be greater than 1000
. And the minimum value of the objective function would be 10e+15
.
Your initial guess x0 is not feasible and contradicts your constraint as you can see by checking the return optimization result res
:
fun: -1.3026339977067573e+62
jac: array([2.45939306e+50])
message: 'Inequality constraints incompatible'
nfev: 50
nit: 16
njev: 15
status: 4
success: False
x: array([-2.64828354e+12])
You always have to use a feasible initial guess that satisfies all your constraints. Note also that your constraint is a simple bound on the variable, so it's recommended to pass it as a bound instead of the more general constraints:
from scipy.optimize import minimize
def objective(x):
a = x[0]
return a ** 5
# 1000 <= x[0] <= None is equivalent to 1000 <= x[0] <= Inf
bounds = [(1000, None)]
x0 = [1002]
res = minimize(objective, x0, bounds=bounds)
print('x: ', res.x)
print('fun: ',res.fun)