I am using scipy.optimize.minimize to solve a problem, but the result given by the package violates the constraint.
The case is mad simple where only one objective function and only one constraint are given. Here is the code:
import math
import numpy as np
import scipy
from scipy.optimize import minimize
def objective(x):
return np.sum(np.dot(x,x))
n = 5
X_bound=[(0,4) for i in range(n)]
X_guess=[1 for i in range(n)]
_tmp = []
func_list = []
def temp_func(X):
total = 0
for i in range(n):
total = total + np.maximum(X[i] * 5 - 6, 0)
return total/n - 1
func_list.append(temp_func)
for ii in range(len(func_list)):
_tmp.append({'type': 'ineq', 'fun': func_list[ii]})
X_constraint=_tmp
sol=scipy.optimize.minimize(objective,X_guess,method='SLSQP',bounds=X_bound,
constraints=X_constraint)
result = sol.x
result
The result given by the above code is
array([5.61582806e-12, 3.56925226e-12, 3.57912934e-12, 3.57912933e-12, 3.57872619e-12])
which obviously violates the one (and only) constraint.
Any ideas what i do wrong? Thanks