Python Powell 最小化求解器:类型错误 - 减去 'list' 和 'list'
Python Powell minimize solver: type error - subtracting 'list' and 'list'
我尝试 solve/minimise 一组多项式(Winterstein-Jensen LTE 计算的一部分)Python。
我正在使用 SciPy 中的最小化求解器 fmin_Powell。见以下代码:
import numpy as np
import scipy as sp
import scipy.optimize as spo
#start parameters and initial estimates:
average = 128.96530977973006
Stdevv = 355.72971228985256
Skewv = 12913.846528570562
Kurtv = 3.0009847038644031
Exckurtv = 0.00098470386440308033
c = np.zeros (4)
h30 = Skewv / (4.0 + 2.0 * (np.power(abs(1.0 + 1.5 * Exckurtv), 0.5)))
h40 = (np.sqrt((abs(1.0 + 1.5 * Exckurtv))) - 1.0) / 18.0
K0 = np.power((1.0 + 2.0 * (np.power(h30,2.0)) + 6.0 * np.power(h40,2.0)), -0.5 )
c[1] = Stdevv * K0 * (1.0 - 3.0 * h40)
c[2] = Stdevv * K0 * h30
c[3] = Stdevv * K0 * h40
#multi polynomials
def wj_f(p):
f1 = (np.power(p[0],2.0) + 6.0 * p[0] * p[2] + 2.0 * np.power(p[1],2.0) +
15.0 * np.power(p[2],2.0) - np.power(Stdevv,2.0))
f2 = (p[1] * (6.0 * np.power(p[0],2.0) + 8.0 * np.power(p[1],2.0) + 72.0 * p[0] * p[2] + 270.0 * np.power(p[2],2.0))
- np.power(Stdevv,3.0) * Skewv)
f3 = (60.0 * np.power(p[1],4.0) + 3.0 * np.power(p[0],4.0) + 10395.0 * np.power(p[2],4.0)
+ 60.0 * np.power(p[0],2.0) * np.power(p[1],2.0) + 4500.0 * np.power(p[1],2.0) * np.power(p[2],2.0)
+ 630.0 * np.power(p[0],2.0) * np.power(p[2],2.0) + 936.0 * p[0] * np.power(p[1],2.0) * p[2]
+ 3780.0 * p[0] * np.power(p[2],3.0) + 60.0 * np.power(p[0],3.0) * p[2] - np.power(Stdevv,4.0) * Kurtv)
wj_f = [f1,f2,f3]
return(wj_f)
print('Start solving')
#solver
xopt = spo.fmin_powell(wj_f,c[1:],args=(),xtol=1e-06,ftol=1e- 06,maxiter=None,maxfun=None,full_output=1,disp=1,retall=1,callback=None,direc=None)
print('Solved')
但是,我收到以下错误:
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
execfile('wj_in_python.py')
File "wj_in_python.py", line 85, in <module>
xopt = spo.fmin_powell(wj_f,c[1:],args=(),xtol=1e-06,ftol=1e-06,maxiter=None,maxfun=None,full_output=1,disp=1,retall=1,callback=None,direc=None)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 2227, in fmin_powell
res = _minimize_powell(func, x0, args, callback=callback, **opts)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 2299, in _minimize_powell
tol=xtol * 100)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 2120, in _linesearch_powell
alpha_min, fret, iter, num = brent(myfunc, full_output=1, tol=tol)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 1885, in brent
res = _minimize_scalar_brent(func, brack, args, **options)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 1901, in _minimize_scalar_brent
brent.optimize()
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 1757, in optimize
tmp1 = (x - w) * (fx - fv)
TypeError: unsupported operand type(s) for -: 'list' and 'list'
错误本身很明显:列表不能彼此相减。
问题是:为什么 Python/the SciPy-script 创建两个或更多列表而不是数字或整数?
应该将三个函数的结果合并为一个大和 (wj_f = f1 + f2 + f3
)。
求解器无法使用 list/matrix/vector。
我尝试 solve/minimise 一组多项式(Winterstein-Jensen LTE 计算的一部分)Python。
我正在使用 SciPy 中的最小化求解器 fmin_Powell。见以下代码:
import numpy as np
import scipy as sp
import scipy.optimize as spo
#start parameters and initial estimates:
average = 128.96530977973006
Stdevv = 355.72971228985256
Skewv = 12913.846528570562
Kurtv = 3.0009847038644031
Exckurtv = 0.00098470386440308033
c = np.zeros (4)
h30 = Skewv / (4.0 + 2.0 * (np.power(abs(1.0 + 1.5 * Exckurtv), 0.5)))
h40 = (np.sqrt((abs(1.0 + 1.5 * Exckurtv))) - 1.0) / 18.0
K0 = np.power((1.0 + 2.0 * (np.power(h30,2.0)) + 6.0 * np.power(h40,2.0)), -0.5 )
c[1] = Stdevv * K0 * (1.0 - 3.0 * h40)
c[2] = Stdevv * K0 * h30
c[3] = Stdevv * K0 * h40
#multi polynomials
def wj_f(p):
f1 = (np.power(p[0],2.0) + 6.0 * p[0] * p[2] + 2.0 * np.power(p[1],2.0) +
15.0 * np.power(p[2],2.0) - np.power(Stdevv,2.0))
f2 = (p[1] * (6.0 * np.power(p[0],2.0) + 8.0 * np.power(p[1],2.0) + 72.0 * p[0] * p[2] + 270.0 * np.power(p[2],2.0))
- np.power(Stdevv,3.0) * Skewv)
f3 = (60.0 * np.power(p[1],4.0) + 3.0 * np.power(p[0],4.0) + 10395.0 * np.power(p[2],4.0)
+ 60.0 * np.power(p[0],2.0) * np.power(p[1],2.0) + 4500.0 * np.power(p[1],2.0) * np.power(p[2],2.0)
+ 630.0 * np.power(p[0],2.0) * np.power(p[2],2.0) + 936.0 * p[0] * np.power(p[1],2.0) * p[2]
+ 3780.0 * p[0] * np.power(p[2],3.0) + 60.0 * np.power(p[0],3.0) * p[2] - np.power(Stdevv,4.0) * Kurtv)
wj_f = [f1,f2,f3]
return(wj_f)
print('Start solving')
#solver
xopt = spo.fmin_powell(wj_f,c[1:],args=(),xtol=1e-06,ftol=1e- 06,maxiter=None,maxfun=None,full_output=1,disp=1,retall=1,callback=None,direc=None)
print('Solved')
但是,我收到以下错误:
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
execfile('wj_in_python.py')
File "wj_in_python.py", line 85, in <module>
xopt = spo.fmin_powell(wj_f,c[1:],args=(),xtol=1e-06,ftol=1e-06,maxiter=None,maxfun=None,full_output=1,disp=1,retall=1,callback=None,direc=None)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 2227, in fmin_powell
res = _minimize_powell(func, x0, args, callback=callback, **opts)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 2299, in _minimize_powell
tol=xtol * 100)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 2120, in _linesearch_powell
alpha_min, fret, iter, num = brent(myfunc, full_output=1, tol=tol)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 1885, in brent
res = _minimize_scalar_brent(func, brack, args, **options)
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 1901, in _minimize_scalar_brent
brent.optimize()
File "C:\Program Files (x86)\Python.7.3\lib\site-packages\scipy\optimize\optimize.py", line 1757, in optimize
tmp1 = (x - w) * (fx - fv)
TypeError: unsupported operand type(s) for -: 'list' and 'list'
错误本身很明显:列表不能彼此相减。
问题是:为什么 Python/the SciPy-script 创建两个或更多列表而不是数字或整数?
应该将三个函数的结果合并为一个大和 (wj_f = f1 + f2 + f3
)。
求解器无法使用 list/matrix/vector。