我正在实施一个非常简单的易感染感染恢复模型,该模型具有稳定的空闲侧项目 - 通常是一项非常简单的任务。但是我使用PysCeS或SciPy遇到了求解器错误,两者都使用lsoda作为它的底层求解器。这只发生在参数的特定值上,我很难理解为什么。我正在使用的代码如下:
import numpy as np
from pylab import *
import scipy.integrate as spi
#Parameter Values
S0 = 99.
I0 = 1.
R0 = 0.
PopIn= (S0, I0, R0)
beta= 0.50
gamma=1/10.
mu = 1/25550.
t_end = 15000.
t_start = 1.
t_step = 1.
t_interval = np.arange(t_start, t_end, t_step)
#Solving the differential equation. Solves over t for initial conditions PopIn
def eq_system(PopIn,t):
'''Defining SIR System of Equations'''
#Creating an array of equations
Eqs= np.zeros((3))
Eqs[0]= -beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - mu*PopIn[0] + mu*(PopIn[0]+PopIn[1]+PopIn[2])
Eqs[1]= (beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - gamma*PopIn[1] - mu*PopIn[1])
Eqs[2]= gamma*PopIn[1] - mu*PopIn[2]
return Eqs
SIR = spi.odeint(eq_system, PopIn, t_interval)
这会产生以下错误:
lsoda-- at current t (=r1), mxstep (=i1) steps
taken on this call before reaching tout
In above message, I1 = 500
In above message, R1 = 0.7818108252072E+04
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.
通常当我遇到这样的问题时,我设置的方程系统出现了一些完全错误,但我都看不出它有什么问题。奇怪的是,如果你将mu改为像1/15550
这样的东西,它也会起作用。如果系统出现问题,我还在R中实现了模型,如下所示:
require(deSolve)
sir.model <- function (t, x, params) {
S <- x[1]
I <- x[2]
R <- x[3]
with (
as.list(params),
{
dS <- -beta*S*I/(S+I+R) - mu*S + mu*(S+I+R)
dI <- beta*S*I/(S+I+R) - gamma*I - mu*I
dR <- gamma*I - mu*R
res <- c(dS,dI,dR)
list(res)
}
)
}
times <- seq(0,15000,by=1)
params <- c(
beta <- 0.50,
gamma <- 1/10,
mu <- 1/25550
)
xstart <- c(S = 99, I = 1, R= 0)
out <- as.data.frame(lsoda(xstart,times,sir.model,params))
这个也使用lsoda,但似乎没有任何障碍。任何人都可以看到Python代码中出现了什么问题吗?
答案 0 :(得分:21)
我认为对于您选择的参数,您遇到stiffness的问题 - 由于数值不稳定,求解器的步长在溶液曲线的斜率区域变得非常小其实很浅薄。由lsoda
包裹的Fortran解算器scipy.integrate.odeint
尝试在适合“僵硬”和“非僵硬”系统的方法之间自适应地切换,但在这种情况下似乎无法切换到僵硬的方法。
非常粗略地你可以大规模地增加允许的最大步数,解算器最终会到达那里:
SIR = spi.odeint(eq_system, PopIn, t_interval,mxstep=5000000)
更好的选择是使用面向对象的ODE求解器scipy.integrate.ode
,它允许您明确选择是使用刚性还是非刚性方法:
import numpy as np
from pylab import *
import scipy.integrate as spi
def run():
#Parameter Values
S0 = 99.
I0 = 1.
R0 = 0.
PopIn= (S0, I0, R0)
beta= 0.50
gamma=1/10.
mu = 1/25550.
t_end = 15000.
t_start = 1.
t_step = 1.
t_interval = np.arange(t_start, t_end, t_step)
#Solving the differential equation. Solves over t for initial conditions PopIn
def eq_system(t,PopIn):
'''Defining SIR System of Equations'''
#Creating an array of equations
Eqs= np.zeros((3))
Eqs[0]= -beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - mu*PopIn[0] + mu*(PopIn[0]+PopIn[1]+PopIn[2])
Eqs[1]= (beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - gamma*PopIn[1] - mu*PopIn[1])
Eqs[2]= gamma*PopIn[1] - mu*PopIn[2]
return Eqs
ode = spi.ode(eq_system)
# BDF method suited to stiff systems of ODEs
ode.set_integrator('vode',nsteps=500,method='bdf')
ode.set_initial_value(PopIn,t_start)
ts = []
ys = []
while ode.successful() and ode.t < t_end:
ode.integrate(ode.t + t_step)
ts.append(ode.t)
ys.append(ode.y)
t = np.vstack(ts)
s,i,r = np.vstack(ys).T
fig,ax = subplots(1,1)
ax.hold(True)
ax.plot(t,s,label='Susceptible')
ax.plot(t,i,label='Infected')
ax.plot(t,r,label='Recovered')
ax.set_xlim(t_start,t_end)
ax.set_ylim(0,100)
ax.set_xlabel('Time')
ax.set_ylabel('Percent')
ax.legend(loc=0,fancybox=True)
return t,s,i,r,fig,ax
输出:
答案 1 :(得分:1)
受感染的人口PopIn[1]
衰减为零。显然,(正常)数值不精确导致PopIn[1]
在t = 322.9附近变为负值(约-3.549e-12)。然后最终解决方案在t = 7818.093附近爆炸,PopIn[0]
朝向无穷大,PopIn[1]
朝向-infinity。
编辑:我删除了我之前的“快速修复”建议。这是一个可疑的黑客。