传染病模型-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");}
}
传染病模型-java代码相关推荐
- SIR传染模型Matlab代码,sir传染病模型 MATLAB代码运行不了,
问题描述: sir传染病模型 MATLAB代码运行不了, function y=ill(t,x) a=1;b=0.3; y=[a*x(1)*x(2)-b*x(1),-a*x(1)*x(2)]'; ts ...
- 灰色模型 java代码_灰色模型的简单Java实现
前几天在以前的遗留代码中发现一个问题,就是我生成的一个数据的走势曲线的预测值(用于灰色时间序列预测)总是和老代码里的不一致,具体来说就是:遗留代码里面的预测值的斜率总是为零,相比之下我生成的就比较合理 ...
- java weka命令行_使用自己的Java代码和模型获取WEKA中的预测百分比
概观 我知道可以通过GUI和命令行选项在训练有素的WEKA模型中获得每个预测的百分比,如文档文章"Making predictions"中所方便解释和演示的那样. 预测 我知道有三 ...
- java源文件编译成jar_从源文件和JAR文件构建Java代码模型
java源文件编译成jar 最近,我花了一些时间来研究有效java ,该方法正在GitHub上达到300星(可以免费帮助实现目标:D). Effectivejava是在您的Java代码上运行查询的工具 ...
- 从源文件和JAR文件构建Java代码模型
最近,我花了一些时间来研究有效java ,该方法正在GitHub上达到300星(随时帮助实现目标:D). Effectivejava是在您的Java代码上运行查询的工具. 它基于我参与的另一个项目ja ...
- 避免在JSP中写java代码
作者:蜗牛学院CTO李懿老师 自从十年前的taglibs(如JSTL)和EL(表达语言,这些事情)诞生以来,在JSP中使用scriptlet(<% %>这些东西)的确是非常不鼓励的. 小 ...
- 【Android 内存优化】Java 内存模型 ( Java 虚拟机内存模型 | 线程私有区 | 共享数据区 | 内存回收算法 | 引用计数 | 可达性分析 )
文章目录 一. Java 虚拟机内存模型 二. 程序计数器 ( 线程私有区 ) 三. 虚拟机栈 ( 线程私有区 ) 四. 本地方法栈 ( 线程私有区 ) 五. 方法区 ( 共享数据区 ) 1. 方法区 ...
- java代码连接数据库
jdbc的知识点: JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API, 可以为多种关系数据库提供统一访问,它由一组用Jav ...
- ajax代码 java,AJAX - java代码库 - 云代码
[java]代码库1.什么是AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). 2.ajax的特点:局部刷新 3.Aj ...
- 作业调度问题java代码_Tabu Search求解作业车间调度问题(Job Shop Scheduling)-附Java代码...
本文来源于公众号[程序猿声],作者舟寒丶 作业车间调度问题 问题模型 举个栗子 有关禁忌搜索算法的内容,公众号内有详细教程: 大家可以点击超链接回顾相关知识,这里就不再细说了. 一般而言,用禁忌搜索算 ...
最新文章
- 三、单链表增删改查原理和代码实现
- shell脚本编写知识2
- Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)...
- 单片机汇编编程300例_pic单片机编程串烧,pic单片机汇编语言讲解下篇
- 万户OA应变大考验之新员工学习篇
- JS数组与对象的遍历方法大全
- 机器学习 训练验证测试_测试前验证| 机器学习
- linux qt5.9交叉编译,QT5.9移植
- Canvas绘图基础(一)
- Leetcode 448. Find All Numbers Disappeared in an Array
- “赢在幻灯片”有奖征文启事
- SaveRasterFile failed: IDLnaMetadata Error:naGetMetadata-GetMetadataJob failed
- 哈佛幸福课 24人格力量测试
- efficientnet
- 如何快速理解最大流和最小割
- 大饼震荡不变,新平台搭建?
- SpringBoot【整合JSP】
- vmware linux访问windowsxp下的文件
- .bat文件实现一个简单的http请求工具(支持get和post请求)
- 高考状元杀死同学 自称跟马加爵的经历很像