【问题标题】:Scipy optimize.minimize exits successfully when constraints aren't satisfied当约束不满足时,Scipy optimize.minimize 成功退出
【发布时间】:2018-06-30 09:00:49
【问题描述】:

我一直在使用 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.])

这个问题没有满足约束的解,但是,minimize() 使用初始条件作为最优解成功返回。

这种行为是有意的吗?如果是这样,如果最优解不满足约束,有没有办法强制失败?

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    这似乎是一个错误。我在issue on 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多