Python 使用scipy.optimize.minimize而不使用lambda函数

Python 使用scipy.optimize.minimize而不使用lambda函数,python,numpy,optimization,lambda,scipy,Python,Numpy,Optimization,Lambda,Scipy,我想使用scipy.optimize.minimize函数,而不将约束指定为lambda函数。这可能吗 i、 e.对于标准示例: 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]

我想使用scipy.optimize.minimize函数,而不将约束指定为lambda函数。这可能吗

i、 e.对于标准示例:

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)
我不想使用lambda函数的原因是,对于我的问题,我的约束数增长得相当快(2*自由度),因此,除非有办法为我的约束创建“lambda”工厂,否则显式编写它们将很快变得单调乏味

上述代码段返回:

TypeError: 'float' object is not callable

调用没有参数的函数。这对我很有用:

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)

注意区别:乐趣是一种功能;乐趣(x)是它在x的价值。感谢您的澄清!正确,但这不是调用函数,而是将其作为参数传递。