0

I want to use the scipy.optimize.minimize function without specifying my constraints as lambda functions. Is this possible?

i.e. for the standard example:

from scipy.optimize import minimize

def fun(x):
    return (x[0] - 1) ** 2 + (x[1] - 2.5)**2.

x   = (2, 0)

def c_0(x):
    return x[0] - 2. * x[1] + 2.

def c_1(x):
    return -x[0] - 2. * x[1] + 6.

def c_2(x):
    return -x[0] + 2. * x[1] + 2.

cons = ({'type': 'ineq', 'fun': c_0(x)},
    {'type': 'ineq', 'fun': c_1(x)},
    {'type': 'ineq', 'fun': c_2(x)})

bnds = ((0, None), (0, None))

res = minimize(fun(x), x, method='SLSQP', bounds=bnds, constraints=cons)

The reason I want to avoid using lambda functions is that my constraint number grows quite quickly for my problem (2*number of degrees of freedom), so unless there's a way of creating a "lambda" factory for my constraints, explicitly writing them will become tedious very quickly.

The above code snippet returns:

TypeError: 'float' object is not callable
2
  • Note the difference: fun is a function; fun(x) is its value at x. Commented Oct 2, 2017 at 17:06
  • Thanks for the clarification! Commented Oct 2, 2017 at 17:11

1 Answer 1

1

Call the functions without arguments. This works for me:

from scipy.optimize import minimize

def fun(x):
    return (x[0] - 1) ** 2 + (x[1] - 2.5)**2.

x   = (2, 0)

def c_0(x):
    return x[0] - 2. * x[1] + 2.

def c_1(x):
    return -x[0] - 2. * x[1] + 6.

def c_2(x):
    return -x[0] + 2. * x[1] + 2.

cons = ({'type': 'ineq', 'fun': c_0},
    {'type': 'ineq', 'fun': c_1},
    {'type': 'ineq', 'fun': c_2})

bnds = ((0, None), (0, None))

res = minimize(fun, x, method='SLSQP', bounds=bnds, constraints=cons)
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, but that's not calling the function, it's passing it as a parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.