gpt4 book ai didi

python - GEKKO 和 Scipy.optimize 导致非线性参数估计的结果不同

转载 作者:行者123 更新时间:2023-12-04 09:11:10 24 4
gpt4 key购买 nike

我正在学习如何使用 GEKKO 解决参数估计问题以及作为第一步,我正在开发我有的示例问题以前使用 Scipy 最小化例程实现。这些有已按照 APMonitor.com 和内提供的类(class)。目前的问题是间歇式 react 器甲醇制烃过程的模拟从以下获得: http://www.daetools.com/docs/tutorials-all.html#tutorial-che-opt-5

模型描述可以在进一步描述的代码中遵循下面,但考虑的基本步骤是:

   A --> B   
A + B --> C
C + B --> P
A --> C
A --> P
A + B --> P

可获得 A、C 和 P 浓度的实验数据作为时间的函数。该模型的目标是估计速率六个基本 react (k1-k6) 的常数。难度我我现在遇到的是我的 GEKKO 模型和基于 Scipy.optimize 的模型导致不同的参数估计,尽管对参数使用相同的实验数据和初始猜测。我还将此模型与使用 gPROMS 和 Athena 开发的模型进行了比较Visual Studio,scipy 模型与参数一致使用这些封闭源程序获得的估计。估计的每个程序的参数如下所示:

  • Scipy 模型(L-BFGS-B 优化器):[k1 k2 k3 k4 k5 k6] = [2.779, 0., 0.197, 3.042, 2.148, 0.541]

  • GEKKO 模型(IPOPT 优化器):[k1 k2 k3 k4 k5 k6] = [3.7766387559, 1.1826920269e-07, 0.21242442412, 4.130394645, 2.4232122905, 3.3140978171]

有趣的是,这两个模型导致相同的目标函数值优化结束时为 0.0123,图中看起来相似物种浓度与时间的关系。我试过改变 GEKKO 的优化器并将公​​差收紧到 1E-8 无济于事。我的猜测是我的 GEKKO 模型设置不正确,但我找不到问题所在用它。如果能指出我可能的任何帮助,我们将不胜感激可能导致模型差异的问题。我附上下面两个脚本:

Scipy 模型

import numpy as np
from scipy.integrate import solve_ivp
from scipy.optimize import minimize
import matplotlib.pyplot as plt

#Experimental data
times = np.array([0.0, 0.071875, 0.143750, 0.215625, 0.287500, 0.359375, 0.431250,
0.503125, 0.575000, 0.646875, 0.718750, 0.790625, 0.862500,
0.934375, 1.006250, 1.078125, 1.150000])
A_obs = np.array([1.0, 0.552208, 0.300598, 0.196879, 0.101175, 0.065684, 0.045096,
0.028880, 0.018433, 0.011509, 0.006215, 0.004278, 0.002698,
0.001944, 0.001116, 0.000732, 0.000426])
C_obs = np.array([0.0, 0.187768, 0.262406, 0.350412, 0.325110, 0.367181, 0.348264,
0.325085, 0.355673, 0.361805, 0.363117, 0.327266, 0.330211,
0.385798, 0.358132, 0.380497, 0.383051])
P_obs = np.array([0.0, 0.117684, 0.175074, 0.236679, 0.234442, 0.270303, 0.272637,
0.274075, 0.278981, 0.297151, 0.297797, 0.298722, 0.326645,
0.303198, 0.277822, 0.284194, 0.301471])

def rxn(x, k): #rate equations in power law form r = k [A][B]
A = x[0]
B = x[1]
C = x[2]
P = x[3]

k1 = k[0]
k2 = k[1]
k3 = k[2]
k4 = k[3]
k5 = k[4]
k6 = k[5]

r1 = k1 * A
r2 = k2 * A * B
r3 = k3 * C * B
r4 = k4 * A
r5 = k5 * A
r6 = k6 * A * B

return [r1, r2, r3, r4, r5, r6] #returns reaction rate of each equation

#mass balance diff eqs, function calls rxn function

def mass_balances(t, x, *args):
k = args
r = rxn(x, k)
dAdt = - r[0] - r[1] - r[3] - r[4] - r[5]
dBdt = + r[0] - r[1] - r[2] - r[5]
dCdt = + r[1] - r[2] + r[3]
dPdt = + r[2] + r[4] + r[5]

return [dAdt, dBdt, dCdt, dPdt]

IC = [1.0, 0, 0, 0] #Initial conditions of species A, B, C, P
ki= [1, 1, 1, 1, 1, 1]

#Objective function definition

def obj_fun(k):
#solve initial value problem over time span of data
sol = solve_ivp(mass_balances,[min(times),max(times)],IC, args = (k), t_eval=(times))
y_model = np.vstack((sol.y[0],sol.y[2],sol.y[3])).T
obs = np.vstack((A_obs, C_obs, P_obs)).T
err = np.sum((y_model-obs)**2)

return err

bnds = ((0, None), (0, None),(0, None),(0, None),(0, None),(0, None))
model = minimize(obj_fun,ki, bounds=bnds, method = 'L-BFGS-B')
k_opt = model.x

print(k_opt.round(decimals = 3))

y_calc = solve_ivp(mass_balances,[min(times),max(times)],IC, args = (model.x), t_eval=(times))

plt.plot(y_calc.t, y_calc.y.T)
plt.plot(times,A_obs,'bo')
plt.plot(times,C_obs,'gx')
plt.plot(times,P_obs,'rs')

GEKKO 模型

import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO

#Experimental data
times = np.array([0.0, 0.071875, 0.143750, 0.215625, 0.287500, 0.359375, 0.431250,
0.503125, 0.575000, 0.646875, 0.718750, 0.790625, 0.862500,
0.934375, 1.006250, 1.078125, 1.150000])
A_obs = np.array([1.0, 0.552208, 0.300598, 0.196879, 0.101175, 0.065684, 0.045096,
0.028880, 0.018433, 0.011509, 0.006215, 0.004278, 0.002698,
0.001944, 0.001116, 0.000732, 0.000426])
C_obs = np.array([0.0, 0.187768, 0.262406, 0.350412, 0.325110, 0.367181, 0.348264,
0.325085, 0.355673, 0.361805, 0.363117, 0.327266, 0.330211,
0.385798, 0.358132, 0.380497, 0.383051])
P_obs = np.array([0.0, 0.117684, 0.175074, 0.236679, 0.234442, 0.270303, 0.272637,
0.274075, 0.278981, 0.297151, 0.297797, 0.298722, 0.326645,
0.303198, 0.277822, 0.284194, 0.301471])


m = GEKKO(remote = False)

t = m.time = times


Am = m.CV(value=A_obs, lb = 0)
Cm = m.CV(value=C_obs, lb = 0)
Pm = m.CV(value=P_obs, lb = 0)

A = m.Var(1, lb = 0)
B = m.Var(0, lb = 0)
C = m.Var(0, lb = 0)
P = m.Var(0, lb = 0)

Am.FSTATUS = 1
Cm.FSTATUS = 1
Pm.FSTATUS = 1

k1 = m.FV(1, lb = 0)
k2 = m.FV(1, lb = 0)
k3 = m.FV(1, lb = 0)
k4 = m.FV(1, lb = 0)
k5 = m.FV(1, lb = 0)
k6 = m.FV(1, lb = 0)

k1.STATUS = 1
k2.STATUS = 1
k3.STATUS = 1
k4.STATUS = 1
k5.STATUS = 1
k6.STATUS = 1

r1 = m.Var(0, lb = 0)
r2 = m.Var(0, lb = 0)
r3 = m.Var(0, lb = 0)
r4 = m.Var(0, lb = 0)
r5 = m.Var(0, lb = 0)
r6 = m.Var(0, lb = 0)

m.Equation(r1 == k1 * A)
m.Equation(r2 == k2 * A * B)
m.Equation(r3 == k3 * C * B)
m.Equation(r4 == k4 * A)
m.Equation(r5 == k5 * A)
m.Equation(r6 == k6 * A * B)


#mass balance diff eqs, function calls rxn function
m.Equation(A.dt() == - r1 - r2 - r4 - r5 - r6)
m.Equation(B.dt() == r1 - r2 - r3 - r6)
m.Equation(C.dt() == r2 - r3 + r4)
m.Equation(P.dt() == r3 + r5 + r6)

m.Obj((A-Am)**2+(P-Pm)**2+(C-Cm)**2)


m.options.IMODE = 5
m.options.SOLVER = 3 #IPOPT optimizer
m.options.RTOL = 1E-8
m.options.OTOL = 1E-8
m.solve()

k_opt = [k1.value[0],k2.value[0], k3.value[0], k4.value[0], k5.value[0], k6.value[0]]
print(k_opt)
plt.plot(t,A)
plt.plot(t,C)
plt.plot(t,P)
plt.plot(t,B)
plt.plot(times,A_obs,'bo')
plt.plot(times,C_obs,'gx')
plt.plot(times,P_obs,'rs')

最佳答案

这里有一些建议:

  • 设置 m.options.NODES=3 或更高(最多 6)以获得更好的集成精度。
  • AmCmPm设置为参数而不是变量。它们是固定输入。
  • 尝试不同的初始条件。可能有多个局部最小值。
  • 目标函数可能是平坦的,因此不同的参数值给出相同的目标函数值。你可以test the parameter confidence intervals查看数据是否给出了窄或宽的联合置信区域。

修改后的结果如下:

parameter estimation

import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO

#Experimental data
times = np.array([0.0, 0.071875, 0.143750, 0.215625, 0.287500, 0.359375, 0.431250,
0.503125, 0.575000, 0.646875, 0.718750, 0.790625, 0.862500,
0.934375, 1.006250, 1.078125, 1.150000])
A_obs = np.array([1.0, 0.552208, 0.300598, 0.196879, 0.101175, 0.065684, 0.045096,
0.028880, 0.018433, 0.011509, 0.006215, 0.004278, 0.002698,
0.001944, 0.001116, 0.000732, 0.000426])
C_obs = np.array([0.0, 0.187768, 0.262406, 0.350412, 0.325110, 0.367181, 0.348264,
0.325085, 0.355673, 0.361805, 0.363117, 0.327266, 0.330211,
0.385798, 0.358132, 0.380497, 0.383051])
P_obs = np.array([0.0, 0.117684, 0.175074, 0.236679, 0.234442, 0.270303, 0.272637,
0.274075, 0.278981, 0.297151, 0.297797, 0.298722, 0.326645,
0.303198, 0.277822, 0.284194, 0.301471])

m = GEKKO(remote=False)

t = m.time = times

Am = m.Param(value=A_obs, lb = 0)
Cm = m.Param(value=C_obs, lb = 0)
Pm = m.Param(value=P_obs, lb = 0)

A = m.Var(1, lb = 0)
B = m.Var(0, lb = 0)
C = m.Var(0, lb = 0)
P = m.Var(0, lb = 0)

k = m.Array(m.FV,6,value=1,lb=0)
for ki in k:
ki.STATUS = 1
k1,k2,k3,k4,k5,k6 = k

r1 = m.Var(0, lb = 0)
r2 = m.Var(0, lb = 0)
r3 = m.Var(0, lb = 0)
r4 = m.Var(0, lb = 0)
r5 = m.Var(0, lb = 0)
r6 = m.Var(0, lb = 0)

m.Equation(r1 == k1 * A)
m.Equation(r2 == k2 * A * B)
m.Equation(r3 == k3 * C * B)
m.Equation(r4 == k4 * A)
m.Equation(r5 == k5 * A)
m.Equation(r6 == k6 * A * B)

#mass balance diff eqs, function calls rxn function
m.Equation(A.dt() == - r1 - r2 - r4 - r5 - r6)
m.Equation(B.dt() == r1 - r2 - r3 - r6)
m.Equation(C.dt() == r2 - r3 + r4)
m.Equation(P.dt() == r3 + r5 + r6)

m.Minimize((A-Am)**2)
m.Minimize((P-Pm)**2)
m.Minimize((C-Cm)**2)

m.options.IMODE = 5
m.options.SOLVER = 3 #IPOPT optimizer
m.options.RTOL = 1E-8
m.options.OTOL = 1E-8
m.options.NODES = 5
m.solve()

k_opt = []
for ki in k:
k_opt.append(ki.value[0])
print(k_opt)

plt.plot(t,A)
plt.plot(t,C)
plt.plot(t,P)
plt.plot(t,B)
plt.plot(times,A_obs,'bo')
plt.plot(times,C_obs,'gx')
plt.plot(times,P_obs,'rs')
plt.show()

关于python - GEKKO 和 Scipy.optimize 导致非线性参数估计的结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63350026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com