当约束条件不满足时,Scipy optimize.minimize成功退出

9

我一直在使用scipy.optimize.minimize(文档),并注意到当我定义一个无法满足的约束问题时会出现一些奇怪的行为。这里有个例子:

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.])

这个问题没有解决方案能够满足约束条件,但是使用初始条件作为最优解, minimize() 函数可以成功返回。

这种行为是否是有意的?如果是的话,是否有一种方法可以强制失败,如果最优解不满足约束条件?

1个回答

4

这似乎是一个错误。我在github上的问题中添加了一个类似于您示例的评论。

如果您使用不同的方法,例如COBYLA,该函数会正确地失败未找到解决方案:

In [10]: optimize.minimize(f, x0=3, constraints=constraints, method='COBYLA')
Out[10]: 
     fun: -3.75
   maxcv: 2.5
 message: 'Did not converge to a solution satisfying the constraints. See `maxcv` for magnitude of violation.'
    nfev: 7
  status: 4
 success: False
       x: array(2.5)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接