我是python的新用户。目前,我正在编写一个python脚本,旨在通过SciPy对数据协调和参数估计执行目标函数的最小化。代码显示在这里:
import numpy as np
import sys
import pandas as pd
from scipy.optimize import minimize
import math
# read from specified excel file
file = 'TP Data.xlsx'
x1 = pd.ExcelFile(file)
df = pd.read_excel(file,sheet_name = 'Sheet1');
# number of trial points
df_nr = len(df.index)
ndata = 1
if ndata > df_nr:
sys.exit('Error! Input trials exceeds data trials.')
df_lim = df.iloc[0:ndata]
# data initialization
FA6 = df_lim['F(A6)'].values
TA2 = df_lim['T(A2)'].values
TA4 = df_lim['T(A4)'].values
TA5 = df_lim['T(A5)'].values
TA7 = df_lim['T(A7)'].values
TA8 = df_lim['T(A8)'].values
TD1 = df_lim['T(D1)'].values
TC1 = df_lim['T(C1)'].values
TB1 = df_lim['T(B1)'].values
TB2 = df_lim['T(B2)'].values
FA1 = df_lim['F(A1)'].values
FA3 = df_lim['F(A3)'].values
FC1 = df_lim['F(C1)'].values
FD1 = df_lim['F(D1)'].values
FB1 = df_lim['F(B1)'].values
TA1 = df_lim['T(A1)'].values
TB3 = df_lim['T(B3)'].values
TC2 = df_lim['T(C2)'].values
TD2 = df_lim['T(D2)'].values
# unmeasured variables TA3 and TA6, initialized as TA2
TA3 = TA2.copy()
TA6 = TA2.copy()
data_arr = np.concatenate((FA6, TA2, TA4, TA5, TA7, TA8, TD1, TC1, TB1, TB2, FA1, FA3, FC1, FD1, FB1, TA1, TB3, TC2, TD2, TA3, TA6))
data_mat = np.reshape(data_arr,(21,-1))
U = np.array([5.0,4.0,7.0,5.0])
data_arr_full = np.concatenate((data_arr, U))
sflow = 0.5*np.ones((1,ndata))
stemp = 0.01*np.ones((1,ndata))
stdDevAllrow = np.concatenate((sflow, stemp, stemp, stemp, stemp, stemp, stemp, stemp, stemp, stemp, sflow, sflow, sflow, sflow, sflow, stemp, stemp, stemp, stemp, stemp, stemp), axis = 0)
#fudge = len(U) + 2*len(TA3);
def objective(x):
sumz = 0
for i in range(0,19):
sumz = sumz + ((x[i] - data_arr[i])/stdDevAllrow[i])**2
return sumz
#xlol = np.zeros(len(np.arange(0,19)))
#qlol = objective(xlol)
def constraint_residuals():
xdata = data_arr_full[0:-4]
UAint = data_arr_full[-4::]
FA6=xdata[0]
TA2=xdata[1]
TA4=xdata[2]
TA5=xdata[3]
TA7=xdata[4]
TA8=xdata[5]
TD1=xdata[6]
TC1=xdata[7]
TB1=xdata[8]
TB2=xdata[9]
FA1=xdata[10]
FA3=xdata[11]
FC1=xdata[12]
FD1=xdata[13]
FB1=xdata[14]
TA1=xdata[15]
TB3=xdata[16]
TC2=xdata[17]
TD2=xdata[18]
TA3=xdata[19]
TA6=xdata[20]
dt1_1 = TB2 - TA2
dt1_2 = TB3 - TA1
dt2_1 = TB1 - TA4
dt2_2 = TB2 - TA3
dt3_1 = TC1 - TA5
dt3_2 = TC2 - TA4
dt4_1 = TD1 - TA7
dt4_2 = TD2 - TA6
lmtd1 = (dt1_1 - dt1_2)/math.log(dt1_1/dt1_2)
lmtd2 = (dt2_1 - dt2_2)/math.log(dt2_1/dt2_2)
lmtd3 = (dt3_1 - dt3_2)/math.log(dt3_1/dt3_2)
lmtd4 = (dt4_1 - dt4_2)/math.log(dt4_1/dt4_2)
y = np.zeros(14)
y[0] = FA1 - FA3 - FA6
y[1] = FA1*(TA2 - TA1) - FB1*(TB2 - TB3)
y[2] = FA3*(TA4 - TA3) - FB1*(TB1 - TB2)
y[3] = FA3*(TA5 - TA4) - FC1*(TC1 - TC2)
y[4] = FA6*(TA7 - TA6) - FD1*(TD1 - TD2)
y[5] = FA1*TA8 - FA3*TA5 - FA6*TA7
y[6] = UAint[0]*lmtd1 - FB1*(TB2 - TB3)
y[7] = UAint[1]*lmtd2 - FB1*(TB1 - TB2)
y[8] = UAint[2]*lmtd3 - FC1*(TC1 - TC2)
y[9] = UAint[3]*lmtd4 - FD1*(TD1 - TD2)
y[10] = TB2 - TA4
y[11] = TB1 - TA4
y[12] = TC1 - TA5
y[13] = TD1 - TA7
return y
res_con = constraint_residuals()
complete_con = np.append(res_con,data_arr_full)
#print(complete_con)
consts = {'type':'ineq', 'fun': complete_con}
def constraint_bounds():
con1_low_lim = np.zeros(len(res_con)) #exactness
con1_up_lim = np.zeros(len(res_con)) #exactness
con1_low_lim = np.append(con1_low_lim, [1, 1, 1, 1])
con1_up_lim = np.append(con1_up_lim, [10, 10, 10, 10])
con2_low_lim = data_mat - 5*stdDevAllrow
con2_up_lim = data_mat + 5*stdDevAllrow
con_low_lim = np.append(con1_low_lim,con2_low_lim)
con_up_lim = np.append(con1_up_lim,con2_up_lim)
bound_stack = np.vstack((con_low_lim, con_up_lim))
bound_stack = np.transpose(bound_stack)
bounds = tuple(map(tuple, bound_stack))
return bounds
bnds = constraint_bounds()
xguess = data_arr_full*1.001
xguess = xguess.tolist()
sol = minimize(objective, xguess, method ='SLSQP', bounds = bnds,
constraints = consts)
使用 minimize 时,出现标题中提到的错误。
完整回溯错误:
runfile('C:/Users/*/Desktop/Python Files/TPNonlinOpt.py', wdir='C:/Users/*/Desktop/Python Files')
Traceback (most recent call last):
File "<ipython-input-296-e3621176fc71>", line 1, in <module>
runfile('C:/Users/*/Desktop/Python Files/TPNonlinOpt.py', wdir='C:/Users/*/Desktop/Python Files')
File "C:\Users\*\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "C:\Users\*\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/*/Desktop/Python Files/TPNonlinOpt.py", line 145, in <module>
sol = minimize(objective, xguess, method ='SLSQP', bounds = bnds, constraints = consts)
File "C:\Users\*\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 611, in minimize
constraints, callback=callback, **options)
File "C:\Users\*\Anaconda3\lib\site-packages\scipy\optimize\slsqp.py", line 315, in _minimize_slsqp
for c in cons['ineq']]))
File "C:\Users\*\Anaconda3\lib\site-packages\scipy\optimize\slsqp.py", line 315, in <listcomp>
for c in cons['ineq']]))
TypeError: 'numpy.ndarray' object is not callable
我不确定该如何解决。任何帮助表示赞赏。
谢谢!