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");
}
}