-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulation.java
More file actions
566 lines (517 loc) · 19.7 KB
/
population.java
File metadata and controls
566 lines (517 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by ben on 2017-09-26.
*/
public class population
{
/* //case 0
final int [] X = {0,-1,-1,-1,0,1,1,1} ;
final int [] Y = {1,1,0,-1,-1,-1,0,1};*/
//Delimiter used in CSV file
private static final String COMMA_DELIMITER = ",";
private static final String NEW_LINE_SEPARATOR = "\n";
//****************** data input ************************
//size of the population
int N;
//probability that a sick individ infects its neighbours
double ps;
//the lifetime of the disease for each individ
int min;
int max;
//probability that an individ X die iff X is sick
double ps_x;
//number of sick individs and their position
int Ns;
int posX;
int posY;
Individ [][] pop;
//*******************************************************
int SM,X,F,S,AS,AX,TF,Frisk;
int epidemi;
boolean isEpidemi;
ArrayList<Object> coord;
FileWriter fileWriter = null;
population()
{
//Scanner in
//****************** data input ************************
//size of the population
this.N = 3;
//probability that a sick individ infects its neighbours
this.ps = 0.2;
//the lifetime of the disease for each individ
this.min = 3;
this.max = 5;
//probability that an individ X die iff X is sick
this.ps_x = 0.2;
//number of sick individs and their position
this.Ns = 2;
this.posX = (int)(this.N/2);
this.posY = (int)(this.N/2);
//*******************************************************
/* System.out.println("X = " + this.posX);
System.out.println("X = " + this.posY);*/
this.coord = new ArrayList<Object>();
this.pop = new Individ[N][N];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
pop[i][j] = new Individ( " " + i + "" + j, i,j);
if( i == this.posX && j == this.posY)
pop[i][j] = new Individ( " " + i + "" + j, i,j,true);
}
}
//******* initialize the data output parameter *************
SM = 0;
X = 0;
F=0;
S=1;
AS=0;
AX=0;
//************** end of initialization **********************
epidemi = (int)((this.N*this.N)/2) + 1;
this.isEpidemi = false;
}
//****custom population constructor ***********
population(int maxx,int minn, int p_s,int NN,int pxx, int antal_sjuka, int [] dessplacering)
{
//Scanner in
//****************** data input ************************
//size of the population
this.N = NN;
//probability that a sick individ infects its neighbours
this.ps = p_s;
//the lifetime of the disease for each individ
this.min = maxx;
this.max = minn;
//probability that an individ X die iff X is sick
this.ps_x = pxx;
//number of sick individs and their position
this.Ns = antal_sjuka;
this.posX = (int)(this.N/2);
this.posY = (int)(this.N/2);
//*******************************************************
this.coord = new ArrayList<Object>();
this.pop = new Individ[NN][NN];
int x = 0, y = 1;
for(int i = 0, j = 1; j < dessplacering.length; i++,j++) {
int q = dessplacering[i];
int k = dessplacering[j];
pop[q][k] = new Individ(" " + q + "" + k, q, k, true);
}
for(int i = 0; i < NN; i++)
{
for(int j = 0; j < NN; j++)
{
if(pop[i][j] == null) {
pop[i][j] = new Individ(" " + i + "" + j, i, j);
}
}
}
//******* initialize the data output parameter *************
SM = 0;
X = 0;
F=0;
S=antal_sjuka;
AS=0;
AX=0;
//************** end of initialization **********************
epidemi = (int)((this.N*this.N)/2);
this.isEpidemi = false;
}
//*********************************************
public int [] getDataOutput()
{
int [] bark = {this.SM,this.X,this.F,this.S,this.AS,this.AX};
return bark;
}
public String getDataOutputString()
{
StringBuilder br = new StringBuilder();
br.append(" Smittade per day " + this.SM + " ," + NEW_LINE_SEPARATOR);
br.append(" Döda per day " + this.X + " ,"+ NEW_LINE_SEPARATOR);
br.append(" Tillfriskna per day " + this.F + " ,"+ NEW_LINE_SEPARATOR);
br.append(" Sjuka per day " + this.S + " ,"+ NEW_LINE_SEPARATOR);
/* br.append(" Accumelerad Sjuka " + this.AS + " ,"+ NEW_LINE_SEPARATOR);
br.append(" Accumelerad döda " + this.AX + " ,"+ NEW_LINE_SEPARATOR);*/
br.append(" Totalt tillfriskna " + this.TF + " ,"+ NEW_LINE_SEPARATOR);
br.append(" Friska " + this.Frisk + " ,"+ NEW_LINE_SEPARATOR);
/*return br.toString();*/
return br.toString();
}
public void getDataOutputString(int d)
{
try
{
String tr = "Dataoutput" + d + ".csv";
this.fileWriter = new FileWriter(tr);
//Write the CSV file header
this.fileWriter.append("**************** DATA OUTPUT ******************");
//Add a new line separator after the header
this.fileWriter.append(NEW_LINE_SEPARATOR);
//Write a new student object list to the CSV file
this.fileWriter.append(" Smittade per day " + this.SM);
this.fileWriter.append(NEW_LINE_SEPARATOR);
this.fileWriter.append(" Döda per day " + this.X );
this.fileWriter.append(NEW_LINE_SEPARATOR);
this.fileWriter.append(" Tillfriskna per day " + this.F );
this.fileWriter.append(NEW_LINE_SEPARATOR);
this.fileWriter.append(" Sjuka per day " + this.S );
this.fileWriter.append(NEW_LINE_SEPARATOR);
/* this.fileWriter.append(" Accumelerad Sjuka " + this.AS);
this.fileWriter.append(NEW_LINE_SEPARATOR);
this.fileWriter.append(" Accumelerad döda " + this.AX);
this.fileWriter.append(NEW_LINE_SEPARATOR);*/
this.fileWriter.append(" Totalt tillfriskna " + this.TF);
this.fileWriter.append(NEW_LINE_SEPARATOR);
this.fileWriter.append(" Friska " + this.Frisk);
this.fileWriter.append(NEW_LINE_SEPARATOR);
System.out.println("CSV file was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing fileWriter !!!");
e.printStackTrace();
}
}
}
public ArrayList<int[]> getNear(int x1, int y1)
{
return neighbours(x1,y1,this.pop);
}
public ArrayList<int[]> neighbours(int posX, int posY, Individ [][] P)
{
int dim = P[0].length;
pos point = new pos(posX,posY);
ArrayList<Object> cd = new ArrayList<Object>();
ArrayList<int[]> par = new ArrayList<int[]>();
pos [] back;
//get the situation and postion of object individ
// around the individ in index posX and posY
int sit = situation(posX,posY,dim);
// System.out.println("case is = " + sit);
//get the corresponding
// int [] cord = (int[]) coord.get(sit);
if((sit >= 0 ) && (sit <= 3)) {
//case 1
//back = new int[6];
switch (sit)
{
case 0 :
par.add(point.getRight());
par.add(point.getQ4());
par.add(point.getDown());
break;
case 1 :
par.add(point.getLeft());
par.add(point.getQ3());
par.add(point.getDown());
break;
case 2 :
par.add(point.getUp());
par.add(point.getQ1());
par.add(point.getRight());
break;
case 3 :
par.add(point.getUp());
par.add(point.getQ2());
par.add(point.getLeft());
break;
}
}
else if((sit >= 4 ) && (sit <= 7)) {
//case 2 or 1
//back = new int[12];
switch (sit)
{
case 4 :
par.add(point.getQ3());
par.add(point.getLeft());
par.add(point.getRight());
par.add(point.getQ4());
par.add(point.getDown());
break;
case 5 :
par.add(point.getLeft());
par.add(point.getQ1());
par.add(point.getUp());
par.add(point.getRight());
par.add(point.getQ2());
break;
case 6 :
par.add(point.getUp());
par.add(point.getQ1());
par.add(point.getRight());
par.add(point.getQ4());
par.add(point.getDown());
break;
case 7 :
par.add(point.getUp());
par.add(point.getQ2());
par.add(point.getLeft());
par.add(point.getQ3());
par.add(point.getDown());
break;
}
}
else {
//back = new int[16];
par.add(point.getUp());
par.add(point.getRight());
par.add(point.getDown());
par.add(point.getLeft());
par.add(point.getQ1());
par.add(point.getQ2());
par.add(point.getQ3());
par.add(point.getQ4());
}
return par;
}
public int situation(int x, int y, int n)
{
int c = 0;
//case 1 4 cases
if( x == 0 && y == 0)
return 0;
else if(x == 0 && y == n -1)
return 1;
else if(x == n - 1 && y == 0)
return 2;
else if (x == n - 1 && y == n - 1)
return 3;
//case 2 2 cases
if( x == 0)
return 4;
else if(x == n - 1)
return 5;
if( y == 0)
return 6;
else if(y == n - 1)
return 7;
return -1;
}
public void display()
{
for(int i = 0; i < this.N; i++)
{
for(int j = 0; j < this.N; j++)
{
System.out.print(" " + pop[i][j]);
}
System.out.println(" ");
}
}
//select a.size() * random
public ArrayList<int[]> random_choose(ArrayList<int[]> univ, double prob)
{
ArrayList<int[]> par = new ArrayList<int[]>();
//choose no more than this number of element
int limit = (int)(prob*univ.size());
// System.out.println("univ size = " + univ.size());
// System.out.println("limit = " + limit);
boolean [] check = new boolean[univ.size()];
int t,k;
int count = 0;
for(int i = 0; (par.size() <= limit); i++ )
{
t = (int)(univ.size() * Math.random());
if(count < limit)
{
if(check[t] == false) {
check[t] = true;
par.add(univ.get(t));
count++;
}
}
else if(count == limit) {
break;
}
else
break;
}
return par;
}
//infect will be called from another function
// every call will constituetes an iteration and
// represent one day in our model
public void infect()
{
for(int i = 0; i < this.N; i++)
{
for(int j = 0; j < this.N; j++)
{
if((this.pop[i][j].isInfect() && this.pop[i][j].isLive()) && (this.pop[i][j].isImmun() == false && (this.pop[i][j].getNewly() == 0)) )
{
//********************* handle neighbours around this individ *********************************
ArrayList<int[]> ind_toInfect = neighbours(i,j,this.pop);
ArrayList<int[]> ind_Infect = random_choose(ind_toInfect,this.ps);
//to add check if p is already infected
for(int [] p : ind_Infect) {
if(this.pop[p[0]][p[1]].isInfect() == false)
{
this.pop[p[0]][p[1]].setInfect(true);
}
}
//other cases probability that X dies iff X is sick
// in the same loop get neigbours P
// in P random choose K individs with prob p_X
// and for each individ p in K if p is infected
// set the p individ has dead setIslive = false;
ArrayList<int[]> ind_die = random_choose(ind_toInfect,this.ps_x);
for(int [] p : ind_die) {
if(this.pop[p[0]][p[1]].isInfect()) {
this.pop[p[0]][p[1]].setIslive(false);
//augment the number of dead individ
//this.X++;
}
}
//**************************************** end **********************************************
this.pop[i][j].days_infection();
//********************* handle this individ in index *****************************************
//handle different cases min och max set immun
if(this.pop[i][j].nb_days_sickness > this.pop[i][j].getMax())
{
this.pop[i][j].setImmun(true);
this.pop[i][j].setInfectImmun(false);
//Increment the number of people immune or who have been curred
//this.F++;
}
}
// *********** update the number of accumulated dead and ************
// ******** sick individ since the beginning of the infection *******
//ändra den här och uppdatera värdet efter varje iteration
// ta dag 1 får in värden och updatera sedan
//*******************************************************************
if(this.S >= epidemi)
this.isEpidemi = true;
}
}
//update for the next day
this.S = 0;
this.SM = 0;
this.F = 0;
this.X = 0;
this.TF = 0;
this.Frisk = 0;
for(int i = 0; i < this.N; i++)
{
for(int j = 0; j < this.N; j++)
{
if(this.pop[i][j].isInfect() && this.pop[i][j].isLive())
this.S = this.S + 1;
if((!this.pop[i][j].isInfect()) && this.pop[i][j].isLive())
this.Frisk = this.Frisk + 1;
if(this.pop[i][j].isInfect()) {
//peuples[i][j].days_infection();
this.pop[i][j].setNewly(0);
if(this.pop[i][j].nb_days_sickness == 1 && this.pop[i][j].isLive())
{
this.SM = this.SM + 1;
}
if(this.pop[i][j].isLive() == false)
{
this.X = this.X + 1;
}
if(this.pop[i][j].isImmun())
{
this.F = this.F + 1;
}
}
if(this.pop[i][j].isImmun())
this.TF = this.TF + 1;
}
}
}
public int stop()
{
int bk = (this.N) * (this.N);
int count = 0;
for(int i = 0; i < this.N; i++)
{
for(int j = 0; j < this.N; j++)
{
if(!this.pop[i][j].isInfect()) {
count = count + 1;
}
}
}
return count;
}
public void simul()
{
//call infect N times N = days
int i;
int count = 0;
int bk = (this.N) * (this.N) - this.X;
for(i = 1; count != bk ; i++)
{
System.out.println("************************** day " + i + " ******************************** ");
infect();
display();
System.out.println();
this.AS = this.AS + this.S;
this.AX = this.AX + this.X;
count = stop();
bk = (this.N) * (this.N) - this.X;
//System.out.println( "Count ==== " + count);
System.out.println( "Data : \n" + getDataOutputString());
getDataOutputString(i);
System.out.println(" ************************ day " + i + " finished ************************");
}
System.out.println("It is an epidemi ? " + this.isEpidemi);
}
public void simulate(int N)
{
//call infect N times N = days
int i;
for(i = 1; i < N + 1; i++)
{
System.out.println("************************** day " + i + " ******************************** ");
infect();
display();
System.out.println();
this.AS = this.AS + this.S;
this.AX = this.AX + this.X;
System.out.println( "Data : \n" + getDataOutputString());
System.out.println(" ************************ day " + i + " finished ************************");
}
getDataOutputString(i);
System.out.println("It is an epidemi ? " + this.isEpidemi);
}
public void simulate()
{
System.out.println("************************** day " + 0 + " ******************************** ");
display();
System.out.println( "Data : \n" + getDataOutputString());
System.out.println(" ************************ day " + 0 + " finished ************************");
simulation();
}
public void simulation()
{
//call infect N times N = days
int i;
for(i = 1; i < this.N + 1; i++)
{
System.out.println("************************** day " + i + " ******************************** ");
infect();
display();
System.out.println();
this.AS = this.AS + this.S;
this.AX = this.AX + this.X;
System.out.println( "Data : \n" + getDataOutputString());
System.out.println(" ************************ day " + i + " finished ************************");
}
getDataOutputString(i);
System.out.println("It is an epidemi ? " + this.isEpidemi);
}
}