传染病模型-java代码

在这里插入图片描述

model层

hospital类

public class Hospital {
    private int contain;//总床位
    private int free;//空余床位
    private int working;//正在使用的床位

    public Hospital() {

    }

    public Hospital(int contain) {
        this.contain = contain;
        this.free = contain;
        this.working = 0;
    }

    public int getContain() {
        return contain;
    }

    public void setContain(int contain) {
        this.contain = contain;
    }

    public int getFree() {
        return free;
    }

    public void setFree(int free) {
        this.free = free;
    }

    public int getWorking() {
        return working;
    }

    public void setWorking(int working) {
        this.working = working;
    }
    public boolean isFull(){
        if(this.free<=0){
            return true;
        }
            return false;
    }
}

Person类

public class Person {
    private boolean isInfected;
    private boolean inHospital;
    private int infectedDay;//第几天被感染的
    private boolean isSure;
    private int cureDate;//接受治疗开始时间
    private  boolean isCured;//是否治疗完成
//    当isSure和isInfected同时为true时,表示确诊
//    当isSure为false,isInfected为true时表示,未确诊患者或无症状患者

    public Person() {
        this.isInfected=false;
        this.inHospital=false;
        this.isSure=false;
        this.isCured=false;
    }

    public Person(boolean isInfected, boolean inHospital, int infectedDay, boolean isSure, int cureDate, boolean isCured) {
        this.isInfected = isInfected;
        this.inHospital = inHospital;
        this.infectedDay = infectedDay;
        this.isSure = isSure;
        this.cureDate = cureDate;
        this.isCured = isCured;
    }

    public boolean isCured() {
        return isCured;
    }

    public void setCured(boolean cured) {
        isCured = cured;
    }

    public int getInfectedDay() {
        return infectedDay;
    }

    public void setInfectedDay(int infectedDay) {
        this.infectedDay = infectedDay;
    }

    public int getCureDate() {
        return cureDate;
    }

    public void setCureDate(int cureDate) {
        this.cureDate = cureDate;
    }

    public boolean isInfected() {
        return isInfected;
    }

    public void setInfected(boolean infected) {
        isInfected = infected;
    }

    public boolean isInHospital() {
        return inHospital;
    }

    public void setInHospital(boolean inHospital) {
        this.inHospital = inHospital;
    }



    public boolean isSure() {
        return isSure;
    }

    public void setSure(boolean sure) {
        isSure = sure;
    }
}
service层

SIR

public class SIR {
    public void doSIR(Map<String,Object> paramMap,int i,int n){
        Hospital hospital=(Hospital)paramMap.get("hospital") ;
        List<Person> susceptible=(List<Person>) paramMap.get("susceptible");//未确诊人群
        List<Person> infected=(List<Person>) paramMap.get("infected");//感染者
        List<Person> sured=(List<Person>)paramMap.get("sured"); //确诊人群
        List<Person> removed=(List<Person>) paramMap.get("removed");//痊愈人群
        int latentPeriod=(int)paramMap.get("latentPeriod");//潜伏期长度
        double infectionRate=(double)paramMap.get("infectionRate");//感染率
        double cureRate=(double)paramMap.get("cureRate");//治愈率
        int cureTime=(int)paramMap.get("cureTime");//治愈周期
        //人群中存在感染者,病毒就会有机会感染人群
        if(!infected.isEmpty()){
            for (Person person : susceptible) {
                if(!person.isInfected()){
                    double a=0;
                    for (int i1=0;i1<n;i1++){
                        a+=Math.random();
                    }
                    a=a/n;
                    if(a<=infectionRate){
                        person.setInfected(true);
                        person.setInfectedDay(i);
                        infected.add(person);
                    }
                    //模拟感染
                }
            }
        }
        //感染人群出现症状,就变为确证患者,从感染者中隔离移除,不在感染正常患者
        for (Person person : infected) {
            int infectedDate=person.getInfectedDay();
            if((i-infectedDate)>=latentPeriod){
                person.setSure(true);
                sured.add(person);
                if(!hospital.isFull()){
                    sendToHospital(person,hospital,i);
                }
            }
        }
        //确诊患者在医院有空余床位的时候,送去医院进行治疗
        for (Person person : sured) {
            if(person.isInHospital()){
                cure(person,hospital,i,cureTime,removed);
            }else{
                if(!hospital.isFull()){
                    sendToHospital(person,hospital,i);
                }
            }
        }
        updateSusceptible(susceptible);
        updateInfected(infected);
        updateSured(sured);

    }
    public boolean hasInfected(List<Person> persons){
        for (Person person : persons) {
            if(person.isInfected()){
                return true;
            }
        }
        return false;
    }
    public void sendToHospital(Person person, Hospital hospital,int day){
        person.setInHospital(true);
        person.setCureDate(day);
        hospital.setWorking(hospital.getWorking()+1);
        hospital.setFree(hospital.getFree()-1);
    }
    public void updateSusceptible(List<Person> susceptible){
        List<Integer> indexs=new ArrayList<Integer>();
        for(int i=0;i<susceptible.size();i++){
            Person person=susceptible.get(i);
            if(person.isInfected()){
                int index=i;
                indexs.add(index);
            }
        }
        for (int i=indexs.size()-1;i>=0;i--){
            int index=indexs.get(i);
            susceptible.remove(index);
        }
    }
    public void updateInfected(List<Person> infected){
        List<Integer> indexs=new ArrayList<Integer>();
        for(int i=0;i<infected.size();i++){
            Person person=infected.get(i);
            if(person.isSure()){
                int index=i;
                indexs.add(index);
            }
        }
        for (int i=indexs.size()-1;i>=0;i--){
            int index=indexs.get(i);
            infected.remove(index);
        }
    }
    public void updateSured(List<Person> sured){
        List<Integer> indexs=new ArrayList<Integer>();
        for(int i=0;i<sured.size();i++){
            Person person=sured.get(i);
            if(person.isCured()){
                int index=i;
                indexs.add(index);
            }
        }
        for (int i=indexs.size()-1;i>=0;i--){
            int index=indexs.get(i);
            sured.remove(index);
        }
    }
    public void cure(Person person,Hospital hospital,int now,int cureTime,List<Person> removed){
        int cureDate=person.getCureDate();
        if((now-cureDate)>=cureTime){
            person.setCured(true);
            removed.add(person);
            hospital.setFree(hospital.getFree()+1);
            hospital.setWorking(hospital.getWorking()-1);
        }
    }
}

Main方法

public class Main {
    //count个人中 有num个人感染病毒
    private static int count;
    private  static int num;
    private static  int days;//模拟时长
    private static int latentPeriod;//潜伏期长度
    private static double infectionRate;//感染率
    private static double cureRate;//治愈率
    private static int cureTime;//治愈周期
    private static int n;
    private static int contain;//医院床位
    public static StandardChartTheme getChineseTheme() {
        //解决jfreechart中文乱码
        StandardChartTheme chineeTheme = new StandardChartTheme("CN");
        chineeTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
        chineeTheme.setRegularFont(new Font("隶书", Font.PLAIN, 15));
        chineeTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
        return chineeTheme;
    }

    public static void main(String[] args) throws IOException {
        //设置不同的参数进行模拟
        count=10000;
        num=50;
        days=100;
        cureRate=0.8;
        n=100;
        latentPeriod=14;
        infectionRate=0.5;
        contain=1000;
        for(cureTime=1;cureTime<=20;cureTime++){
            doMain(count,num,days,latentPeriod,infectionRate,cureRate,cureTime,n,contain);
        }
        System.out.println("done!");
    }
    public  static void doMain(int count,int num,int days,int latentPeriod,double infectionRate,double cureRate,int cureTime,int n,int contain) throws  IOException{
        List<Person> susceptible=new ArrayList<Person>();
        List<Person> infected=new ArrayList<Person>();
        List<Person> removed=new ArrayList<Person>();
        List<Person> sured=new ArrayList<Person>();
        SIR sir=new SIR();
        DefaultCategoryDataset dataset=new DefaultCategoryDataset();
        Hospital hospital=new Hospital(contain);
        for(int i=1;i<=count;i++){
            Person person=new Person();
            if(i<=num){
                person.setInfected(true);
                person.setInfectedDay(1);
                infected.add(person);
            }else{
                susceptible.add(person);
            }

        }
        Map<String,Object> paramMap=new HashMap<String,Object>();
        paramMap.put("hospital",hospital);
        paramMap.put("susceptible",susceptible);
        paramMap.put("infected",infected);
        paramMap.put("removed",removed);
        paramMap.put("sured",sured);
        paramMap.put("latentPeriod",latentPeriod);
        paramMap.put("infectionRate",infectionRate);
        paramMap.put("cureRate",cureRate);
        paramMap.put("cureTime",cureTime);
        int susceptibleCount=0;
        int infectedCount=0;
        int removedCount=0;
        int suredCount=0;
        for(int i=1;i<=days;i++){

            sir.doSIR(paramMap,i,n);
            susceptibleCount=((List<Person>)paramMap.get("susceptible")).size();
            infectedCount=((List<Person>)paramMap.get("infected")).size();
            removedCount=((List<Person>)paramMap.get("removed")).size();
            suredCount=((List<Person>)paramMap.get("sured")).size();
            dataset.addValue(susceptibleCount,"易感人群",""+i);
            dataset.addValue(infectedCount,"感染者",""+i);
            dataset.addValue(suredCount,"确诊患者",""+i);
            dataset.addValue(removedCount,"移除人群",""+i);
//            System.out.println("susceptibleCount:"+susceptibleCount+"\t"+"infectedCount:"+infectedCount+"\t"+"suredCount:"+suredCount+
//                    "\tremovedCount:"+removedCount +"\t总数"+(susceptibleCount+infectedCount+removedCount+suredCount)+"\t"+i);
        }
        ChartFactory.setChartTheme(getChineseTheme());
        String fileName="D:/数学建模/COVID-19模拟(潜伏期"+latentPeriod+"天,感染率"+infectionRate+"治疗周期"+cureTime+"天,床位"+contain+").png ";
        JFreeChart chart = ChartFactory.createLineChart("COVID-19模拟", "天数", "人数", dataset,PlotOrientation.VERTICAL,true,true,false);
        ChartUtils.saveChartAsPNG(new File(fileName), chart, 1920, 1080);
        System.out.println("s");
    }
}
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及传染病模型的数值仿真,常见的编程语言如Python和MATLAB都提供了丰富的科学计算库和工具,可以用来实现数值仿真代码。下面是一个使用Python编写的简单示例代码,演示了如何使用SIR模型进行传染病的数值仿真。 ```python import numpy as np import matplotlib.pyplot as plt # 模型参数 beta = 0.2 # 感染率 gamma = 0.1 # 恢复率 # 初始条件 N = 1000 # 总人数 I0 = 1 # 初始感染人数 R0 = 0 # 初始康复人数 S0 = N - I0 - R0 # 初始易感人数 # 时间步长和仿真时间 dt = 0.1 # 时间步长 T = 100 # 仿真时间 # 数值计算 t = np.linspace(0, T, int(T/dt)+1) # 时间网格 S = np.zeros_like(t) # 易感人数 I = np.zeros_like(t) # 感染人数 R = np.zeros_like(t) # 康复人数 S[0] = S0 I[0] = I0 R[0] = R0 for i in range(1, len(t)): dSdt = -beta * S[i-1] * I[i-1] / N dIdt = beta * S[i-1] * I[i-1] / N - gamma * I[i-1] dRdt = gamma * I[i-1] S[i] = S[i-1] + dt * dSdt I[i] = I[i-1] + dt * dIdt R[i] = R[i-1] + dt * dRdt # 绘图 plt.plot(t, S, label='Susceptible') plt.plot(t, I, label='Infected') plt.plot(t, R, label='Recovered') plt.xlabel('Time') plt.ylabel('Population') plt.legend() plt.show() ``` 这段代码使用SIR模型传染病进行数值仿真,并绘制了易感人数、感染人数和康复人数随时间变化的曲线图。你可以根据实际需求修改模型参数、初始条件和仿真时间等参数。 需要注意的是,这只是一个简单的示例代码,实际应用中可能需要考虑更多的因素和复杂性。在进行具体的传染病模型数值仿真时,建议参考相关的科学论文和文献,以确保模型的准确性和合理性。 希望这个示例代码对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值