0

I am trying to minimize a function with two constraints: x[0] > 1, x[1] > 1

If I pass them as a tuple (commented out in the code below) I am getting the right answer (which is (1,1)).

However, if I pass them in a for loop, it seems that the 1st constraint (x[0] > 1) is being ignored. I am getting the answer as (0,1).

What am I missing?

from scipy.optimize import minimize

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

cons = []
n = 2
for i in range(n):
    cons.append({'type': 'ineq', 'fun': lambda x: x[i] - 1})

# cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 1},
#         {'type': 'ineq', 'fun': lambda x: x[1] - 1})

x0 = [0,0]
sol = minimize(costFunction, x0, constraints = cons)
print(sol['x'])

Thanks.

Suvo
  • 57
  • 7

0 Answers0