Skip to content

Commit

Permalink
Fix SIR Model error
Browse files Browse the repository at this point in the history
  • Loading branch information
vingkan committed Jun 30, 2017
1 parent 6e1d040 commit e1125ee
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 47 deletions.
96 changes: 57 additions & 39 deletions server/SIRModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,48 @@ class SIRModel {
private static double BETA = 0.9; // % of S -> I
private static double GAMMA = 0.2; // % of I -> R

private static int S0 = 950; // Initially Susceptible
private static int I0 = 50; // Initially Infected
private static int R0 = 0; // Initially Resistant
private static int N = S0 + I0 + R0; // Total Population
private static double S0 = 950; // Initially Susceptible
private static double I0 = 50; // Initially Infected
private static double R0 = 0; // Initially Resistant
private static double N = S0 + I0 + R0; // Total Population

private static double[] RANGE = {0.0, 10.0}; // Simulation Range in Days
//private static double DT = 0.1; // Step Size in Days
private static double DT = 0.1; // Step Size in Days

// Data Structure for storing records
public static List<Integer> S_HIST = new ArrayList<Integer>();
public static List<Integer> I_HIST = new ArrayList<Integer>();
public static List<Integer> R_HIST = new ArrayList<Integer>();
public static List<Double> S_HIST = new ArrayList<Double>();
public static List<Double> I_HIST = new ArrayList<Double>();
public static List<Double> R_HIST = new ArrayList<Double>();

private static String OUTFILE = "sir-output.txt"; // Output File

public SIRModel(double beta, double gamma, int s0, int i0, int r0) {
public SIRModel(double beta, double gamma, int s0, int i0, int r0, double days) {
S_HIST.clear();
I_HIST.clear();
R_HIST.clear();
this.BETA = beta;
this.GAMMA = gamma;
this.S0 = s0;
this.I0 = i0;
this.R0 = r0;
this.S0 = (double)s0;
this.I0 = (double)i0;
this.R0 = (double)r0;
this.RANGE[1] = days;
S_HIST.add(S0);
I_HIST.add(I0);
R_HIST.add(R0);
}

public SIRModel(double beta, double gamma, int s0, int i0, int r0, double dt, double days) {
S_HIST.clear();
I_HIST.clear();
R_HIST.clear();
this.BETA = beta;
this.GAMMA = gamma;
this.S0 = (double)s0;
this.I0 = (double)i0;
this.R0 = (double)r0;
this.DT = dt;
this.RANGE[1] = days;
S_HIST.add(S0);
I_HIST.add(I0);
R_HIST.add(R0);
Expand All @@ -58,9 +76,9 @@ public static void main(String[] args) {
R_HIST.add(R0);

// Run Simulation
int Sf = S(RANGE[1]);
int If = I(RANGE[1]);
int Rf = R(RANGE[1]);
double Sf = S(RANGE[1]);
double If = I(RANGE[1]);
double Rf = R(RANGE[1]);

// TODO: Check that records have been correctly populated

Expand All @@ -71,9 +89,9 @@ public static void main(String[] args) {
BufferedWriter bw = new BufferedWriter(fw);

for (double ti = RANGE[0]; ti <= RANGE[1]; ti += DT) {
int Si = getRecord(S_HIST, ti);
int Ii = getRecord(I_HIST, ti);
int Ri = getRecord(R_HIST, ti);
double Si = getRecord(S_HIST, ti);
double Ii = getRecord(I_HIST, ti);
double Ri = getRecord(R_HIST, ti);
String row = ti + "\t" + Si+ "\t" + Ii + "\t" + Ri + "\n";
bw.write(row);
}
Expand All @@ -89,23 +107,23 @@ public static void main(String[] args) {
}

public static void simulate() {
int Sf = S(RANGE[1]);
int If = I(RANGE[1]);
int Rf = R(RANGE[1]);
double Sf = S(RANGE[1]);
double If = I(RANGE[1]);
double Rf = R(RANGE[1]);
}

/*
* @param t - simulation time in days
* @return - number of people susceptible at time t
*/
public static int S(double t) {
int count = getRecord(S_HIST, t);
if (count != -1) {
public static double S(double t) {
double count = getRecord(S_HIST, t);
if (count >= 0) {
return count;
}
else {
double lt = t - DT;
int newCount = (int) ( S(lt) + dS(t) );
double newCount = ( (S(lt)) + dS(t) );
S_HIST.add(newCount);
return newCount;
}
Expand All @@ -115,14 +133,14 @@ public static int S(double t) {
* @param t - simulation time in days
* @return - number of people infected at time t
*/
public static int I(double t) {
int count = getRecord(I_HIST, t);
if (count != -1) {
public static double I(double t) {
double count = getRecord(I_HIST, t);
if (count >= 0) {
return count;
}
else {
double lt = t - DT;
int newCount = (int) ( I(lt) + dI(t) );
double newCount = ( (I(lt)) + dI(t) );
I_HIST.add(newCount);
return newCount;
}
Expand All @@ -132,14 +150,14 @@ public static int I(double t) {
* @param t - simulation time in days
* @return - number of people resistant at time t
*/
public static int R(double t) {
int count = getRecord(R_HIST, t);
if (count != -1) {
public static double R(double t) {
double count = getRecord(R_HIST, t);
if (count >= 0) {
return count;
}
else {
double lt = t - DT;
int newCount = (int) ( R(lt) + dR(t) );
double newCount = ( (R(lt)) + dR(t) );
R_HIST.add(newCount);
return newCount;
}
Expand All @@ -151,7 +169,7 @@ public static int R(double t) {
*/
public static double dS(double t) {
double lt = t - DT;
double change = (double) ( -1 * BETA * ( ( S(lt) * I(lt) ) / N ) );
double change = ( -1.0 * BETA * ( ( (S(lt)) * (I(lt)) ) / (N) ) );
return change * DT;
}

Expand All @@ -161,8 +179,8 @@ public static double dS(double t) {
*/
public static double dI(double t) {
double lt = t - DT;
double change = (double) ( BETA * ( ( S(lt) * I(lt) ) / N ) );
double removed = (double) ( -1 * GAMMA * I(lt) );
double change = ( BETA * ( ( (S(lt)) * (I(lt)) ) / (N) ) );
double removed = ( -1.0 * GAMMA * (I(lt)) );
return (change + removed) * DT;
}

Expand All @@ -172,7 +190,7 @@ public static double dI(double t) {
*/
public static double dR(double t) {
double lt = t - DT;
double change = (double) ( GAMMA * I(lt) );
double change = ( GAMMA * (I(lt)) );
return change * DT;
}

Expand All @@ -195,14 +213,14 @@ private static int getTimeIndex(double t) {
* @param t - simulation time in days
* @return - number of people recorded in data structure at time t
*/
private static int getRecord(List<Integer> records, double t) {
private static double getRecord(List<Double> records, double t) {
int index = getTimeIndex(t);
try {
int count = records.get(index);
double count = records.get(index);
return count;
}
catch(Exception e){
return -1;
return -1.0;
}
}

Expand Down
3 changes: 2 additions & 1 deletion server/SIRServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public static void main(String[] args){
int s0 = Integer.parseInt(req.queryParams("s"));
int i0 = Integer.parseInt(req.queryParams("i"));
int r0 = Integer.parseInt(req.queryParams("r"));
SIRModel model = new SIRModel(beta, gamma, s0, i0, r0);
double days = Double.parseDouble(req.queryParams("d"));
SIRModel model = new SIRModel(beta, gamma, s0, i0, r0, days);
model.simulate();
JSONObject obj = new JSONObject();
obj.put("t", model.getTimeRecords());
Expand Down
101 changes: 101 additions & 0 deletions server/sir-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
0.0 950 50 0
0.1 945 53 1
0.2 941 56 2
0.30000000000000004 936 60 3
0.4 931 64 4
0.5 925 68 5
0.6 925 68 5
0.7 920 72 7
0.7999999999999999 914 77 8
0.8999999999999999 907 82 10
0.9999999999999999 901 87 11
1.0999999999999999 894 92 13
1.2 886 98 15
1.3 870 110 19
1.4000000000000001 861 116 21
1.5000000000000002 852 123 23
1.6000000000000003 843 130 26
1.7000000000000004 833 137 28
1.8000000000000005 823 145 31
1.9000000000000006 812 152 34
2.0000000000000004 801 161 37
2.1000000000000005 789 169 40
2.2000000000000006 777 178 44
2.3000000000000007 765 187 47
2.400000000000001 752 196 51
2.500000000000001 738 205 55
2.600000000000001 725 215 59
2.700000000000001 711 224 63
2.800000000000001 696 234 68
2.9000000000000012 682 244 73
3.0000000000000013 667 254 77
3.1000000000000014 651 265 83
3.2000000000000015 636 275 88
3.3000000000000016 620 285 93
3.4000000000000017 604 295 99
3.5000000000000018 588 305 105
3.600000000000002 572 316 111
3.700000000000002 555 326 117
3.800000000000002 539 335 124
3.900000000000002 523 345 131
4.000000000000002 507 354 138
4.100000000000001 490 363 145
4.200000000000001 474 372 152
4.300000000000001 458 381 159
4.4 443 389 167
4.5 427 397 175
4.6 427 397 175
4.699999999999999 412 404 183
4.799999999999999 397 411 191
4.899999999999999 382 417 199
4.999999999999998 368 423 207
5.099999999999998 354 429 216
5.1999999999999975 340 434 225
5.299999999999997 327 439 233
5.399999999999997 314 443 242
5.4999999999999964 301 446 251
5.599999999999996 289 450 260
5.699999999999996 277 452 269
5.799999999999995 266 455 278
5.899999999999995 255 456 287
5.999999999999995 245 458 296
6.099999999999994 235 459 305
6.199999999999994 225 459 314
6.299999999999994 215 459 324
6.399999999999993 207 459 333
6.499999999999993 198 459 342
6.5999999999999925 190 458 351
6.699999999999992 182 456 360
6.799999999999992 174 455 370
6.8999999999999915 167 453 379
6.999999999999991 160 450 388
7.099999999999991 154 448 397
7.19999999999999 148 445 406
7.29999999999999 142 442 415
7.39999999999999 136 439 423
7.499999999999989 131 436 432
7.599999999999989 126 432 441
7.699999999999989 121 428 450
7.799999999999988 116 424 458
7.899999999999988 111 420 467
7.999999999999988 107 416 475
8.099999999999987 103 412 483
8.199999999999987 99 408 492
8.299999999999986 96 403 500
8.399999999999986 92 398 508
8.499999999999986 89 394 516
8.599999999999985 86 389 524
8.699999999999985 83 384 532
8.799999999999985 80 379 539
8.899999999999984 77 375 547
8.999999999999984 74 370 554
9.099999999999984 72 365 562
9.199999999999983 70 360 569
9.299999999999983 67 355 576
9.399999999999983 65 350 583
9.499999999999982 63 345 590
9.599999999999982 61 340 597
9.699999999999982 59 335 604
9.799999999999981 57 330 611
9.89999999999998 56 325 617
9.99999999999998 54 321 624
24 changes: 17 additions & 7 deletions sir/sir.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@
/*
* Using Multiple Ports on Cloud9
* https://docs.c9.io/docs/multiple-ports
* $ python -m SimpleHTTPServer 8081
* Open: http://esidelta-vingkan.c9users.io:8081/sir.html
*/

const ENDPOINT = 'http://esigamma-vingkan.c9users.io/sir';
const ENDPOINT = 'http://esidelta-vingkan.c9users.io/sir';
const YELLOW = 'rgb(255,153,0)';
const BLUE = 'rgb(51,102,204)';
const RED = 'rgb(220,57,18)';
Expand All @@ -85,13 +87,21 @@
g: gammaInput.value,
s: sInput.value,
i: iInput.value,
r: rInput.value
r: rInput.value,
d: 100.0
};
//console.log(params)
$.get(ENDPOINT, params, (data) => {
//console.log(data);
console.log(data);
plotData(data);
});
}).fail((err) => {
console.log('Error in server call.');
console.log(err);
});
}

function roundListValues(list){
return list.map(d => Math.round(d));
}

function plotData(data) {
Expand All @@ -108,7 +118,7 @@
let plots = [
{
x: data.t,
y: data.s,
y: roundListValues(data.s),
name: 'Susceptible',
mode: 'markers',
marker: {
Expand All @@ -117,7 +127,7 @@
},
{
x: data.t,
y: data.i,
y: roundListValues(data.i),
name: 'Infected',
mode: 'markers',
marker: {
Expand All @@ -126,7 +136,7 @@
},
{
x: data.t,
y: data.r,
y: roundListValues(data.r),
name: 'Resistant',
mode: 'markers',
marker: {
Expand Down

0 comments on commit e1125ee

Please sign in to comment.