-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsource.py
266 lines (178 loc) · 6.93 KB
/
source.py
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
import random
import secrets
import matplotlib.pyplot as plt
'''
Glossary:
Vertex of graph -
'trunc_gauss' - Normal distribution for a fixed range from bottom to top
i.e. in our case bottom = 0, top = n-1
line ( - ) - Input values
effectiveness - actually 1-effectiveness
normal_community - An adjacency matrix that corresponds to a community
where there are no preventive measures against COVID
cautious_community - An adjacency matrix that corresponds to a community
where there are preventive measures against COVID
people_labels - List of people, here each person is identified by an integer
(0 to population -1)
mask_list - List of people who wear mask (Randomised)
sanitize_list - List of people who use sanitiser (Randomised)
social_dist_list - List of people who follow social distancing (Randomised)
prioratise and randomise - sort the 'individuals_at_risk' list in non-decresing order to
ensure that those who don't take a lot of
preventive measures are sooner to get infected
- randomise to ensure that the event of infection
is a random event
'''
population = 100
percent_of_ppl_wear_mask = 0.55
no_of_ppl_wear_mask = int(percent_of_ppl_wear_mask * population)
effectiveness_of_mask = 0.7
percent_of_ppl_sanitizer = 0.2
no_of_ppl_sanitizer = int(percent_of_ppl_sanitizer * population)
effectiveness_of_sanitizer = 0.3
percent_of_ppl_social_dist = 0.80
no_of_ppl_social_dist = int(percent_of_ppl_social_dist * population)
effectiveness_of_social_dist = 0.1
def trunc_gauss(mu, sigma, bottom, top):
a = random.gauss(mu,sigma)
while (bottom <= a <= top) == False:
a = random.gauss(mu,sigma)
return a
people_labels = [i for i in range(0,population)]
normal_community = [[0 for i in range(0,population)] for j in range(0,population)]
cautious_community = [[0 for i in range(0,population)] for j in range(0,population)]
for i in range(0,population):
for j in range(i,population):
cautious_community[i][j] = cautious_community[j][i] = normal_community[i][j] = normal_community[j][i] = secrets.randbelow(2)
if(i==j):
cautious_community[i][j] = cautious_community[j][i] = normal_community[i][j] = normal_community[j][i] = 0 #since you can't transmit disease to your self so matrix at i==j is zero
mask_list = random.sample(people_labels,no_of_ppl_wear_mask)
sanitize_list = random.sample(people_labels,no_of_ppl_sanitizer)
social_dist_list = random.sample(people_labels,no_of_ppl_social_dist)
for i in range(0,len(mask_list)):
for j in range(0,population):
cautious_community[mask_list[i]][j] = cautious_community[mask_list[i]][j]*effectiveness_of_mask
cautious_community[j][mask_list[i]] = cautious_community[j][mask_list[i]]*effectiveness_of_mask
for i in range(0,len(sanitize_list)):
for j in range(0,population):
cautious_community[sanitize_list[i]][j] = cautious_community[sanitize_list[i]][j]*effectiveness_of_sanitizer
cautious_community[j][sanitize_list[i]] = cautious_community[j][sanitize_list[i]]*effectiveness_of_sanitizer
for i in range(0,len(social_dist_list)):
for j in range(0,population):
cautious_community[social_dist_list[i]][j] = cautious_community[social_dist_list[i]][j]*effectiveness_of_social_dist
cautious_community[j][social_dist_list[i]] = cautious_community[j][social_dist_list[i]]*effectiveness_of_social_dist
def prob(x):
l1=[ i for i in range(0,int(10000*x))]
y=secrets.randbelow(10000)
if y in l1:
return 1
else:
return 0
#Normal Community
adj_list_normal=[]
for i in range(population):
l1=[]
for j in range(population):
if(normal_community[i][j]==1):
l1.append(j)
adj_list_normal.append(l1)
rrand = trunc_gauss(2,0.3,0,population)
tot_infection = [i for i in range(0,population)]
infected = []
infected.append(0) #infecting first person
carriers_on_day = []
carriers_on_day.append(0)
dummy = []
day_count = 0
ppl_inf={}
for k2 in range(10000):
dummy=[]
for i in carriers_on_day:
rrand = int(trunc_gauss(2.7,0.3,0,population))
count1 = min(rrand,len(adj_list_normal[i]))
j = 0
k3 = 0
while j<count1 and k3<len(adj_list_normal[i]):
if(adj_list_normal[i][k3] not in infected):
infected.append(adj_list_normal[i][k3])
dummy.append(adj_list_normal[i][k3])
j+=1
k3+=1
carriers_on_day.remove(i)
for k in dummy:
carriers_on_day.append(k)
day_count += 1
if(sorted(infected)==tot_infection):
break
if(len(carriers_on_day)==0):
break
# print("\nDay number : ",day_count)
# print("People infected : ",infected)
ppl_inf[day_count]=len(infected)
print("\n\n\n\n")
dc=day_count
#Cautious community
adj_list_normal=[]
for i in range(population):
l1=[]
for j in range(population):
if(cautious_community[i][j]!=0):
l1.append(j)
adj_list_normal.append(l1)
rrand = trunc_gauss(2,0.3,0,population)
tot_infection = [i for i in range(0,population)]
infected = []
infected.append(0) #infecting first person
carriers_on_day = []
carriers_on_day.append(0)
dummy = []
day_count = 0
ppl_inf2={}
for k2 in range(10000):
dummy=[]
for i in carriers_on_day:
rrand = int(trunc_gauss(2.7,0.3,0,population))
count1 = min(rrand,len(adj_list_normal[i]))
j = 0
k3 = 0
while j<count1 and k3<len(adj_list_normal[i]):
if(adj_list_normal[i][k3] not in infected):
p = prob(cautious_community[i][adj_list_normal[i][k3]])
if(p==1):
infected.append(adj_list_normal[i][k3])
dummy.append(adj_list_normal[i][k3])
j+=1
k3+=1
carriers_on_day.remove(i)
for k in infected:
carriers_on_day.append(k)
day_count += 1
if(sorted(infected)==tot_infection):
break
if(len(carriers_on_day)==0):
break
# print("\nDay number : ",day_count)
# print("People infected : ",infected)
ppl_inf2[day_count]=len(infected)
x=[]
y=[]
x1=[]
y1=[]
for k in ppl_inf.keys():
y.append(ppl_inf[k])
x.append(k)
for k in ppl_inf2.keys():
y1.append(ppl_inf2[k])
x1.append(k)
plt.plot(x, y)
plt.xlabel('no of days')
plt.ylabel('no of people infected')
plt.title('Virus sppread in a non-cautious community:')
plt.show()
print("No of days until total infection: ",dc)
plt.plot(x1, y1)
plt.xlabel('no of days')
plt.ylabel('no of people infected')
plt.title('Virus sppread in a cautious community:')
plt.show()
print("No of days until total infection: ",day_count)