【问题标题】:Scipy.optimize terminates successfully for infeasible NLPScipy.optimize 成功终止不可行的 NLP
【发布时间】:2017-07-22 22:20:44
【问题描述】:

尝试使用 scipy.optimize SLSQP 解决 NLP。这个问题显然是不可行的,但 scipy.optimize 中的最小化功能似乎不同意。

minimize X^2 + Y^2 
subject to 
X + Y = 11
X, Y >= 6

代码:

from scipy.optimize import minimize

def obj(varx):
    return varx[1]**2 + varx[0]**2

def constr1(varx):
    constr1 = -varx[0]-varx[1]+11
    return constr1


bnds = [(6,float('Inf')),(6,float('Inf'))]
ops = ({'maxiter':100000, 'disp':'bool'})
cons = ({'type':'eq', 'fun':constr1})       
res = minimize(obj, x0=[7,7], method='SLSQP', constraints = cons, bounds = bnds, options = ops)

print res.x
print res.success

输出:

Optimization terminated successfully.    (Exit mode 0)
            Current function value: 72.0
            Iterations: 6
            Function evaluations: 8
            Gradient evaluations: 2
[ 6.  6.]
True

我错过了什么吗?

【问题讨论】:

  • 我以前见过这个错误。不知道如何解决这个问题(除了使用不同的求解器)。
  • 知道其他可靠的非线性求解器吗?
  • 请参阅here 了解有关此问题的讨论。我主要做大规模建模,我主要的通用 NLP 求解器是 CONOPT 和 IPOPT(以及其他)。

标签: python optimization scipy nonlinear-optimization


【解决方案1】:

你可以试试mystic。对于约束的不可行解决方案,它无法解决问题。虽然不是“显而易见的”(也许),但它返回一个inf 用于不可行的解决方案......我想可以改进行为(我是作者),以更明显地发现只有不可行的解决方案。

>>> def objective(x):
...   return x[0]**2 + x[1]**2
... 
>>> equations = """
... x0 + x1 = 11
... """
>>> bounds = [(6,None),(6,None)]
>>> 
>>> from mystic.solvers import fmin_powell, diffev2
>>> from mystic.symbolic import generate_constraint, generate_solvers, simplify
>>>
>>> cf = generate_constraint(generate_solvers(simplify(equations)))
>>>
>>> result = fmin_powell(objective, x0=[10,10], bounds=bounds, constraints=cf, gtol=50, disp=True, full_output=True)
Warning: Maximum number of iterations has been exceeded
>>> result[1]
array(inf)
>>>
>>> result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=40, gtol=50, disp=True, full_output=True)
Warning: Maximum number of iterations has been exceeded
>>> result[1]
inf 

【讨论】:

    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多