To resolve a quadratic minimum problem I used scipy.Optimize.Minimize
.
First I initialize my function and some points :
def func(x):
return 1/4*(x[0]**2+x[1]**2) + x[2]
P1=[]
for i in range(3):
P1.append((rd.uniform(-1,1), rd.uniform(-1,1)))
P2=[]
for i in range(3):
P2.append((rd.uniform(-1,1), rd.uniform(-1,1)))
But then I have a problem with the contraints. When I do it there is no error, it works :
cons = [{'type': 'ineq', 'fun': lambda x: x[2] - x[0] * P1[0][0] - x[1] * P1[0][1] - (P1[0][0] ** 2 + P1[0][1] ** 2)},
{'type': 'ineq', 'fun': lambda x: x[2] - x[0] * P1[1][0] - x[1] * P1[1][1] - (P1[1][0] ** 2 + P1[1][1] ** 2)},
{'type': 'ineq', 'fun': lambda x: x[2] - x[0] * P1[2][0] - x[1] * P1[2][1] - (P1[2][0] ** 2 + P1[2][1] ** 2)}]
for i in range(len(P2)):
cons.append({'type': 'ineq', 'fun': lambda x: -x[2] + x[0]*P2[i][0] + x[1]*P2[i][1] + (P2[i][0]**2+P2[i][1]**2)})
But when I do it, it return RuntimeWarning
: invalid value encountered in double_scalars
and doesn't work :
cons=[]
for i in range(0, len(P1)):
cons.append({'type': 'ineq', 'fun': lambda x: x[2] - x[0] * P1[i][0] - x[1] * P1[i][1] - (P1[i][0] ** 2 + P1[i][1] ** 2)})
for i in range(len(P2)):
cons.append({'type': 'ineq', 'fun': lambda x: -x[2] + x[0]*P2[i][0] + x[1]*P2[i][1] + (P2[i][0]**2+P2[i][1]**2)})
PS : Here is the end :
x0 = np.array([0, 0, 0])
res = minimize(func, x0, constraints=cons)
I don't know where is my mistake in the case that doesn't work. Thank you in advance :)