Skip to content

Commit 46176a6

Browse files
committed
print number of spreader. add data.csv as file to collect info. Add randomSeed() for repeatability
1 parent 1cd8308 commit 46176a6

File tree

2 files changed

+92
-47
lines changed

2 files changed

+92
-47
lines changed

People.pde

+72-34
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1+
/** Pasvorn Boonmark Copyright May 2, 2020
2+
* People class to mimic random people behavior moving withing a 2D plane
3+
* The first portion of a People's properties are constant definding boundary
4+
* values that governs one's behaviors. The are defined are follow:
5+
* MAXVEL = maximum velocity that one can move per frame - unit in pixel
6+
* Blimit = boundary limit - use for detecting if a People is within contact
7+
* range of another People
8+
* get_well_day = when a People is sick, how many "days he will get better
9+
* the value is in number of frame since one got sick
10+
* MAXBOUND = govern a boundary that one travel. This is artificial number.
11+
* according to US Transport survey, we travel 40 miles per day on an
12+
* average. So this should reflect that - see www.bts.gov - 2017-05-31
13+
* The following are color of a People based on one's status:
14+
* - well_color when one is well
15+
* - sick_color when one is sick
16+
* - immune_color when one is immune
17+
* The state diagram of a Person is as follow:
18+
* Current State | next states | Trigger
19+
* well sick | contact a sick People with 40% chance
20+
* sick | immune | after sick for "get_well_day"
121
22+
*/
223
class People {
324

4-
final float MAXVEL = 10;
5-
final int blimit = 5;
25+
final float MAXVEL = 3;
26+
final int Blimit = 5;
627
final int get_well_day = 140;
728
final int MAXBOUND = 300;
829
final int well_color = 0x4FFFFFFF;
930
final int sick_color = 0x4FFF0000;
1031
final int immune_color = 0x8F00ff00;
32+
final float sick_v_factor = 0.1;
1133

1234
int myindex;
35+
boolean debug = false;
1336
float xlocation, ylocation;
1437
float xbound[] = new float[2];
1538
float ybound[] = new float[2];
@@ -22,55 +45,69 @@ class People {
2245
boolean immune = false;
2346
int mycolor;
2447
float vfactor = 1.0;
25-
26-
People( int index) {
48+
49+
//
50+
People( int index) {
51+
// initialize a People's properties
2752
xlocation = random(0, width);
2853
ylocation = random(9, height);
54+
// how fast one traveled
2955
xvel = random(-MAXVEL, MAXVEL);
3056
yvel = random(-MAXVEL, MAXVEL);
31-
float xspan = random(10, MAXBOUND);
32-
float yspan = random(10, MAXBOUND);
57+
58+
// Boundary a Person can be - essentially allocated 2xrandom number
59+
float xspan = random(50, MAXBOUND);
60+
float yspan = random(50, MAXBOUND);
3361
xbound[0] = xlocation - xspan;
3462
xbound[1] = xlocation + xspan;
3563
ybound[0] = ylocation - yspan;
3664
ybound[1] = ylocation + yspan;
3765
mycolor = well_color;
3866
myindex = index;
3967
gotsick = 0;
40-
}
41-
42-
void move(int framenumber) {
68+
}
69+
70+
// when a People moves - check to make sure one is within one's bound
71+
void move(int framenumber) {
4372
getbetter(framenumber);
4473
xlocation += xvel*vfactor;
4574
ylocation += yvel*vfactor;
46-
if (xlocation > xbound[1] | xlocation < xbound[0] ) xvel *= -1.0;
47-
48-
if (ylocation > ybound[1] | ylocation < ybound[0]) yvel *= -1.0;
49-
50-
}
51-
52-
boolean meet(People OtherPeople) {
75+
if (xlocation > xbound[1] | xlocation < xbound[0] )
76+
xvel *= -1.0;
77+
if (ylocation > ybound[1] | ylocation < ybound[0])
78+
yvel *= -1.0;
79+
}
80+
81+
// meet - when one People meets another People
82+
boolean meet(People OtherPeople) {
5383
if (myindex == OtherPeople.myindex) return false;
54-
boolean result = ( abs(xlocation - OtherPeople.xlocation) < 5 &
55-
abs(ylocation - OtherPeople.ylocation) < 5 );
56-
//if (result) println(" meet: ", myindex, OtherPeople.myindex);
84+
boolean result = ( abs(xlocation - OtherPeople.xlocation) < Blimit &
85+
abs(ylocation - OtherPeople.ylocation) < Blimit );
86+
if (debug & result)
87+
println(" meet: ", myindex, OtherPeople.myindex);
5788
return result;
58-
}
59-
60-
void contact(People OtherPeople) {
61-
if( sicked() ) {
89+
}
90+
91+
// giving a chance, will a People gets sick
92+
boolean get_sick_chance (float chance) {
93+
return random(0,1.0) < chance;
94+
}
95+
96+
// contact - this is when contact is made. People can get sick here
97+
void contact(People OtherPeople) {
98+
if( sicked() ) {
6299
if( !OtherPeople.sicked() ) {
63-
if( random(0,1.0) < 0.4) {
64-
OtherPeople.getsick(framenumber);
65-
spreader++;
66-
}
100+
if( get_sick_chance(0.4) ) {
101+
OtherPeople.getsick(framenumber);
102+
spreader++;
103+
}
67104
}
68-
} else if ( OtherPeople.sicked()) {
69-
if ( random(0,1.0) < 0.4) {
70-
getsick(framenumber);
71-
OtherPeople.spreader++;
72-
}
73-
}
105+
} else if ( OtherPeople.sicked()) {
106+
if ( get_sick_chance(0.4) ) {
107+
getsick(framenumber);
108+
OtherPeople.spreader++;
109+
}
110+
}
74111
}
75112

76113
void getsick(int framenumber) {
@@ -80,7 +117,6 @@ class People {
80117
sickedat = framenumber;
81118
getwellat = framenumber + random(120,200);
82119
//println(myindex, " got sicked at ", framenumber);
83-
vfactor = 0.4;
84120
}
85121
}
86122

@@ -104,6 +140,8 @@ class People {
104140

105141
void draw() {
106142
fill(mycolor);
143+
textSize(16);
144+
text(spreader,xlocation+5, ylocation);
107145
circle(xlocation,ylocation,10);
108146
}
109147

contagion001.pde

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
final int N = 2000;
1+
// number of People
2+
final int N = 800;
23

34
int framenumber = 0;
45
People[] p1 = new People[N];
5-
6+
// PrinterWrite is for saving output for analysis
7+
PrintWriter output;
8+
/**
9+
We use randomSeed() call so we can repeat experiment with different
10+
parameters.
11+
*/
612
void setup() {
7-
size(2000,1000);
13+
size(1000,640);
14+
randomSeed(1);
815
frameRate(20);
916
for(int i=0; i < N; i++) {
1017
p1[i] = new People(i);
1118
}
1219
p1[0].getsick(1);
20+
String filename = "data" + ".csv";
21+
output = createWriter(filename);
22+
output.println("Frame,Well,Sick,Recovered");
1323
}
1424

1525
void draw() {
@@ -27,21 +37,18 @@ void draw() {
2737
People person2 = p1[j];
2838
if( person1.meet(person2) ) {
2939
person1.contact(person2);
30-
/*
31-
if( person1.sicked() ) {
32-
if( !person2.sicked() ) {
33-
if( random(0,1.0) < 0.4) person2.getsick(framenumber);
34-
}
35-
} else if ( person2.sicked()) {
36-
if ( random(0,1.0) < 0.4) person1.getsick(framenumber);
37-
} */
3840
}
3941
}
4042
p1[i].move(framenumber);
4143
p1[i].draw();
4244
}
4345

4446
framenumber += 1;
45-
println(framenumber,well_count, sick_count, immuned_count);
46-
if( sick_count <= 0) exit();
47+
output.println(framenumber + "," + well_count + "," +
48+
sick_count + "," +immuned_count);
49+
if( sick_count <= 0) {
50+
output.flush();
51+
output.close();
52+
exit();
53+
}
4754
}

0 commit comments

Comments
 (0)