不满足约束条件时,成功进行Scipy Optimize.minimize退出

我一直在使用scipy.optimize.minimize (docs)


当我定义一个无法满足约束的问题时,发现一些奇怪的行为。这是一个例子:


from scipy import optimize


# minimize f(x) = x^2 - 4x

def f(x):

    return x**2 - 4*x


def x_constraint(x, sign, value):

    return sign*(x - value)


# subject to x >= 5 and x<=0 (not possible)

constraints = []

constraints.append({'type': 'ineq', 'fun': x_constraint, 'args': [1, 5]})

constraints.append({'type': 'ineq', 'fun': x_constraint, 'args': [-1, 0]})


optimize.minimize(f, x0=3, constraints=constraints)

结果输出:


fun: -3.0

     jac: array([ 2.])

 message: 'Optimization terminated successfully.'

    nfev: 3

     nit: 5

    njev: 1

  status: 0

 success: True

       x: array([ 3.])

没有满足约束条件的解决方案,但是,使用初始条件作为最佳解决方案,minimum()成功返回。


这种行为是故意的吗?如果是这样,如果最佳解决方案不满足约束条件,是否有办法强制失败?


蛊毒传说
浏览 830回答 1
1回答

千巷猫影

这似乎是一个错误。我在github上的问题上添加了带有您的示例变体的评论。如果您使用其他方法(例如COBYLA),则该函数将无法正确找到解决方案:In [10]: optimize.minimize(f, x0=3, constraints=constraints, method='COBYLA')Out[10]:&nbsp;&nbsp; &nbsp; &nbsp;fun: -3.75&nbsp; &nbsp;maxcv: 2.5&nbsp;message: 'Did not converge to a solution satisfying the constraints. See `maxcv` for magnitude of violation.'&nbsp; &nbsp; nfev: 7&nbsp; status: 4&nbsp;success: False&nbsp; &nbsp; &nbsp; &nbsp;x: array(2.5)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python