奇怪的SciPy ODE集成错误

10

我正在实现一个非常简单的易感-感染-恢复模型,该模型针对一个稳定人口进行建模,这是一个相当琐碎的任务。但是,我在使用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.

通常当我遇到这样的问题时,我设置的方程系统中会有一些终端错误,但我却看不出任何问题。奇怪的是,如果将μ更改为类似于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 代码出了什么问题吗?

2个回答

23
我认为你选择的参数会遇到刚性方程问题 - 由于数值不稳定性,求解器的步长在曲线斜率非常缓和的区域被迫变得非常小。Fortran求解器lsodascipy.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

输出:

这里输入图片描述


这里的所有内容都被包装成run(),是为了更容易调用,还是出于机械原因? - Fomite
@Fomite 编写 Python 文件作为模块而不是脚本通常是一个好主意,因为它允许代码重用并鼓励更好的代码结构。 - DanielSank

1
被感染的人口PopIn[1]会下降到零。显然,(正常的)数值不精确导致PopIn[1]在t=322.9附近变为负数(约为-3.549e-12)。然后最终解在t=7818.093附近崩溃,其中PopIn[0]趋向于+无穷大,而PopIn[1]则趋向于-无穷大。

编辑:我删除了早期的“快速修复”建议。那是一个可疑的黑客行为。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接