对于不可行的NLP,Scipy.optimize成功终止 [英] Scipy.optimize terminates successfully for infeasible NLP

查看:174
本文介绍了对于不可行的NLP,Scipy.optimize成功终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用scipy.optimize SLSQP解决NLP.这个问题显然是不可行的,但是scipy.optimize中的minimum函数似乎不同意.

Tried solving an NLP using the scipy.optimize SLSQP. The problem is clearly infeasible but the minimize function in scipy.optimize seems to disagree.

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

我想念什么吗?

推荐答案

您可以尝试mystic.它无法解决无法解决约束问题的问题.尽管不是显而易见的"(也许),但它为不可行的解决方案返回了inf ...我想可以改善行为(我是作者),以便更明显地发现只有不可行的解决方案.

You can try mystic. It fails to solve the problem for infeasible solutions to the constraints. While not "obvious" (maybe), it returns an inf for infeasible solutions... I guess the behavior could be improved upon (I'm the author) to make it more obvious that only infeasible solutions are found.

>>> 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 

这篇关于对于不可行的NLP,Scipy.optimize成功终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆