尝试使用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

我遗漏了什么吗?

最佳答案

您可以尝试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

关于python - 对于不可行的NLP,Scipy.optimize成功终止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42568164/

10-12 14:52