math647032
math647032

Reputation: 33

Scipy minimize does not take constraints into account

I am new to programmation and I tried to solve a maths optimization problem using Python.

The idea of the problem is to minimize the value of a function while some constraints are respected. This might seem very straightforward, however for some reason the result I get clearly does not satisfy the constraints.

The function in question is the following (I do not have enough reputation points to post images of LaTeX equations):

f(a,b,c,d) = 2*a + 3*b + c + 2*d

With the following constraints:

I used scipy.optimize.minimize in Python to try to solve it, here is my code:

import numpy as np
import scipy
from scipy.optimize import minimize as min

def f(x):
    return 2*x[0] + 3*x[1] + x[2] + 2*x[3]

cons = ({'type' : 'eq','fun': lambda x: np.array([x[0]+x[1]-2])},
{'type' : 'eq','fun': lambda x: np.array([x[2]+x[3]-8])}, 
{'type':'eq' , 'fun': lambda x: np.array([x[0]+x[2]-3])}, 
{'type':'eq' , 'fun': lambda x: np.array([x[1]+x[3]-7])},
{'type':'ineq' , 'fun': lambda x: np.array([x[0]])},
{'type':'ineq' , 'fun': lambda x: np.array([x[1]])},
{'type':'ineq' , 'fun': lambda x: np.array([x[2]])},
{'type':'ineq' , 'fun': lambda x: np.array([x[3]])},
{'type':'ineq' , 'fun':f})

u = min(f,[1.5,0.5,1.5,6.5],constraints=cons,method='Nelder-Mead',options={'Disp':True,'maxiter':2})

print(u)
print(f(u.x))

It gives me some values for a,b,c,d that do not satisfy the equalities. I did try to change the optimization method but it did not help.

Any advice would be really appreciated, thank you for reading!

Upvotes: 3

Views: 1992

Answers (2)

Warren Weckesser
Warren Weckesser

Reputation: 114791

Your objective function is linear, and your equality constraints are linear, so the problem is simple enough to do by hand. If you do that, you'll observe a couple interesting facts:

  • One of your constraints is redundant. For example, you can drop a + c = 3, because that equation is implied by the other three.

  • You are left with three linear equations and four unknowns. The solution to such a system is a straight line in the four-dimensional space. The interesting thing about that line is that your objective function is constant on it. It has the value 19 at every point on the line. So (taking into account the inequality constraints), every point on the line with nonnegative coordinates is a solution. In particular, your starting point [1.5,0.5,1.5,6.5] is on the line, so it is a solution. So is [1.9, 0.1, 1.1, 6.9], [1.65, 0.35, 1.35, 6.65], or in fact any point of the form [0, 2, 3, 5] + a*[1, -1, -1, 1] for which the components are not negative.

Upvotes: 1

Matteo Peluso
Matteo Peluso

Reputation: 452

Hi from the documentation seems like the only method available for a constraint minimization are COBYLA and SLSQP, and in particular, when using a 'eq' constraint, just the SLSQP one. This might work:

u = min(f,[1.5,0.5,1.5,6.5],constraints=cons,method='SLSQP')

Upvotes: 1

Related Questions