-:'NoneType'和'NoneType'的不受支持的操作数类型

时间:2018-12-15 14:20:28

标签: python python-3.x scipy

我正在用SLSQP解决具有相等约束的非线性编程,但是看来代码无法运行,我也不知道为什么。任何帮助将不胜感激!

有关scipy模块的信息可以在以下位置找到: https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 15 22:10:11 2018

@author: chloe
"""
import numpy as np
from scipy.optimize import minimize

def objective(x):
    x1=x[0]
    x2=x[1]
    return x2*(x2-x1)-10

def constraint1(x):
    return x[0]
def constraint2(x):
    return x[1]
def constraint3(x):
    x[0]=x[1]


x0=[1,2]

print(objective(x0))

con1={'type': 'ineq', 'fun' :constraint1}
con2={'type': 'ineq', 'fun' :constraint2}
con3={'type': 'ineq', 'fun' :constraint3}

cons=[con1,con2,con3]

bnds=((0,None),(0,None))
sol = minimize(objective,x0,method='SLSQP',bounds=bnds,constraints=cons)

print(sol)

,结果如下:

runfile('/Users/chloe/.spyder-py3/untitled1.py', wdir='/Users/chloe/.spyder-py3')
-8
Traceback (most recent call last):

  File "<ipython-input-51-1ea03e3e84bf>", line 1, in <module>
    runfile('/Users/chloe/.spyder-py3/untitled1.py', wdir='/Users/chloe/.spyder-py3')

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/chloe/.spyder-py3/untitled1.py", line 35, in <module>
    sol = minimize(objective,x0,method='SLSQP',bounds=bnds,constraints=cons)

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py", line 611, in minimize
    constraints, callback=callback, **options)

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/scipy/optimize/slsqp.py", line 414, in _minimize_slsqp
    for con in cons['ineq']])

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/scipy/optimize/slsqp.py", line 414, in <listcomp>
    for con in cons['ineq']])

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/scipy/optimize/slsqp.py", line 277, in cjac
    return approx_jacobian(x, fun, epsilon, *args)

  File "/Users/chloe/anaconda3/lib/python3.7/site-packages/scipy/optimize/slsqp.py", line 63, in approx_jacobian
    jac[i] = (func(*((x0+dx,)+args)) - f0)/epsilon

TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

0 个答案:

没有答案