Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Constants.java
#	src/MyPanel.java
#	src/Person.java
合并分支。
  • Loading branch information
GinRyan committed Feb 6, 2020
1 parent 7773456 commit 43388df
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
5 changes: 0 additions & 5 deletions src/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,4 @@ public static double stdGaussian(double sigma, double u) {
return sigma * X + u;
}

public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(stdGaussian(25, 140 /2 ));
}
}
}
23 changes: 13 additions & 10 deletions src/MyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,29 @@ public void paint(Graphics g) {
if (pIndex >= people.size()) {
pIndex = 0;
}
int captionStartOffsetX = 16;
int captionStartOffsetY = 40;
int captionSize = 24;

//显示数据信息
g.setColor(Color.WHITE);
g.drawString("城市总人数:" + Constants.CITY_PERSON_SIZE, 16, 40);
g.drawString("城市总人数:" + Constants.CITY_PERSON_SIZE, captionStartOffsetX, captionStartOffsetY );
g.setColor(new Color(0xdddddd));
g.drawString("健康者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.NORMAL), 16, 64);
g.drawString("健康者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.NORMAL), captionStartOffsetX, captionStartOffsetY + captionSize);
g.setColor(new Color(0xffee00));
g.drawString("潜伏者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.SHADOW), 16, 88);
g.drawString("潜伏者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.SHADOW), captionStartOffsetX, captionStartOffsetY + 2 * captionSize);
g.setColor(new Color(0xff0000));
g.drawString("发病者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.CONFIRMED), 16, 112);
g.drawString("发病者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.CONFIRMED), captionStartOffsetX, captionStartOffsetY + 3 * captionSize);
g.setColor(new Color(0x48FFFC));
g.drawString("已隔离人数:" + PersonPool.getInstance().getPeopleSize(Person.State.FREEZE), 16, 136);
g.drawString("已隔离人数:" + PersonPool.getInstance().getPeopleSize(Person.State.FREEZE), captionStartOffsetX, captionStartOffsetY + 4 * captionSize);
g.setColor(new Color(0x00ff00));
g.drawString("空余病床:" + (Constants.BED_COUNT - PersonPool.getInstance().getPeopleSize(Person.State.FREEZE)), 16, 160);
g.drawString("空余病床:" + (Constants.BED_COUNT - PersonPool.getInstance().getPeopleSize(Person.State.FREEZE)), captionStartOffsetX, captionStartOffsetY + 5 * captionSize);
g.setColor(new Color(0xE39476));
g.drawString("急需病床:" + Hospital.getInstance().needBeds(), 16, 184);
g.drawString("急需病床:" + Hospital.getInstance().needBeds(), captionStartOffsetX, captionStartOffsetY + 6 * captionSize);
g.setColor(new Color(0xccbbcc));
g.drawString("病死人数:" + PersonPool.getInstance().getPeopleSize(Person.State.DEATH), captionStartOffsetX, captionStartOffsetY + 7 * captionSize);
g.setColor(new Color(0xffffff));
g.drawString("病死人数:" + PersonPool.getInstance().getPeopleSize(Person.State.DEATH), 16, 184);
g.setColor(new Color(0xffffff));
g.drawString("世界时间(天):" + (int)(worldTime/10.0), 16, 208);
g.drawString("世界时间(天):" + (int)(worldTime/10.0), captionStartOffsetX, captionStartOffsetY + 8 * captionSize);
; }


Expand Down
66 changes: 32 additions & 34 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class Person extends Point {
int sig = 1;

/**
* 正态分布N(mu,sigma)随机位移目标位置
*/

Expand All @@ -42,21 +41,21 @@ public interface State {

//已治愈出院的人转为NORMAL即可,否则会与作者通过数值大小判断状态的代码冲突
int DEATH = FREEZE + 1;//病死者

}

public Person(City city, int x, int y) {
super(x, y);
this.city = city;
this.x = x;
this.y = y;
//对市民的初始位置进行N(x,100)的正态分布随机
targetXU = 100 * new Random().nextGaussian() + x;
targetYU = 100 * new Random().nextGaussian() + y;
targetXU = MathUtil.stdGaussian(100, x);
targetYU = MathUtil.stdGaussian(100, y);

}

/**
* 流动意愿标准化
*
* 根据标准正态分布生成随机人口流动意愿
* <p>
* 流动意愿标准化后判断是在0的左边还是右边从而决定是否流动。
Expand Down Expand Up @@ -115,7 +114,7 @@ private void freezy() {
state = State.FREEZE;
}


/**
* 不同状态下的单个人实例运动行为
*/
private void action() {
Expand All @@ -128,10 +127,11 @@ private void action() {
}
//存在流动意愿的,将进行流动,流动位移仍然遵循标准正态分布
if (moveTarget == null || moveTarget.isArrived()) {
//在想要移动并且没有目标时,将自身移动目标设置为随机生成的符合正态分布的目标点
//在想要移动并且没有目标时,将自身移动目标设置为随机生成的符合正态分布的目标点
//产生N(a,b)的数:Math.sqrt(b)*random.nextGaussian()+a
double targetX = MathUtil.stdGaussian(targetSig, targetXU);
double targetY = MathUtil.stdGaussian(targetSig, targetYU); moveTarget = new MoveTarget((int) targetX, (int) targetY);
double targetY = MathUtil.stdGaussian(targetSig, targetYU);
moveTarget = new MoveTarget((int) targetX, (int) targetY);

}

Expand All @@ -142,7 +142,7 @@ private void action() {
double length = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));//与目标点的距离

if (length < 1) {
//判断是否到达目标点
//判断是否到达目标点
moveTarget.setArrived(true);
return;
}
Expand All @@ -169,8 +169,8 @@ private void action() {


if (getX() > 700) {
//这个700也许是x方向边界的意思,因为画布大小1000x800
//TODO:如果是边界那么似乎边界判断还差一个y方向
//这个700也许是x方向边界的意思,因为画布大小1000x800
//TODO:如果是边界那么似乎边界判断还差一个y方向
moveTarget = null;
if (udX > 0) {
udX = -udX;
Expand All @@ -183,35 +183,35 @@ private void action() {

private float SAFE_DIST = 2f;//安全距离

/**
/**
* 对各种状态的人进行不同的处理,更新发布市民健康状态
*/ public void update() {
*/
public void update() {
//@TODO找时间改为状态机

if (state == State.FREEZE || state == State.DEATH) {
return;//如果已经隔离或者死亡了,就不需要处理了
}

//处理已经确诊的感染者(即患者)
//
if (state == State.CONFIRMED && dieMoment == 0) {
int destiny = new Random().nextInt(10000)+1;//命运数字,[1,10000]随机数
if (1 <= destiny && destiny <= (int)(Constants.FATALITY_RATE * 10000)) {
//如果命运数字落在死亡区间
int dieTime = (int) (Constants.DIE_VARIANCE * new Random().nextGaussian()+Constants.DIE_TIME);
dieMoment = confirmedTime + dieTime;//发病后确定死亡时刻
//System.out.printf("%d,%f,%d\n",destiny,Constants.FATALITY_RATE * 10000,dieTime);
}
else {
dieMoment = -1;//逃过了死神的魔爪
}


int destiny = new Random().nextInt(10000) + 1;//幸运数字,[1,10000]随机数
if (1 <= destiny && destiny <= (int) (Constants.FATALITY_RATE * 10000)) {

//如果幸运数字落在死亡区间
int dieTime = (int) (Constants.DIE_VARIANCE * new Random().nextGaussian() + Constants.DIE_TIME);
dieMoment = confirmedTime + dieTime;//发病后确定死亡时刻
} else {
dieMoment = -1;//逃过了死神的魔爪
}
}
//*/



if (state == State.CONFIRMED
&& MyPanel.worldTime - confirmedTime >= Constants.HOSPITAL_RECEIVE_TIME) {
//如果患者已经确诊,且(世界时刻-确诊时刻)大于医院响应时间,即医院准备好病床了,可以抬走了
//如果患者已经确诊,且(世界时刻-确诊时刻)大于医院响应时间,即医院准备好病床了,可以抬走了
Bed bed = Hospital.getInstance().pickBed();//查找空床位
if (bed == null) {

Expand All @@ -226,19 +226,17 @@ private void action() {
bed.setEmpty(false);
}
}
if (MyPanel.worldTime - infectedTime > Constants.SHADOW_TIME && state == State.SHADOW) {


//处理病死者
if((state == State.CONFIRMED || state == State.FREEZE ) && MyPanel.worldTime >= dieMoment && dieMoment > 0) {
state = State.DEATH;//患者死亡
if ((state == State.CONFIRMED || state == State.FREEZE) && MyPanel.worldTime >= dieMoment && dieMoment > 0) {
state = State.DEATH;//患者死亡
}

//增加一个正态分布用于潜伏期内随机发病时间
double stdRnShadowtime = MathUtil.stdGaussian(25, Constants.SHADOW_TIME / 2);

//处理发病的潜伏期感染者
if (MyPanel.worldTime - infectedTime > stdRnShadowtime && state == State.SHADOW) { state = State.CONFIRMED;//潜伏者发病
if (MyPanel.worldTime - infectedTime > stdRnShadowtime && state == State.SHADOW) {
state = State.CONFIRMED;//潜伏者发病
confirmedTime = MyPanel.worldTime;//刷新时间
}
//处理未隔离者的移动问题
Expand Down

0 comments on commit 43388df

Please sign in to comment.