-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation.java
258 lines (241 loc) · 8.06 KB
/
population.java
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
package com.company;
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};*/
//****************** 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;
//*******************************************************
ArrayList<Object> coord;
population()
{
//Scanner in
//****************** data input ************************
//size of the population
this.N = 11;
//probability that a sick individ infects its neighbours
this.ps = 0.4;
//the lifetime of the disease for each individ
this.min = 5;
this.max = 10;
//probability that an individ X die iff X is sick
this.ps_x = 0.55;
//number of sick individs and their position
this.Ns = 1;
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);
}
}
}
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(Individ [][] peuples)
{
for(int i = 0; i < this.N; i++)
{
for(int j = 0; j < this.N; j++)
{
if(peuples[i][j].isInfect())
{
ArrayList<int[]> ind_toInfect = neighbours(i,j,peuples);
ArrayList<int[]> ind_Infect = random_choose(ind_toInfect,this.ps);
for(int [] p : ind_Infect)
peuples[p[0]][p[1]].setInfect(true);
peuples[i][j].days_infection();
//handle different cases min och max set immun
//individ cannot infect each others the same day the infection occurs
//set two variable in the population class
//SM,X,F,S,AS,AX and update them for each iterations
}
}
}
}
}