-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path49 square.jl
452 lines (366 loc) · 8.93 KB
/
49 square.jl
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
iterations = 1000
pop = [6000 1000 1000 0 0 0 0
2000 5000 1000 0 10 10 0
500 3000 12000 0 30 0 0
0 0 10 0 0 2 0
2 10 20 10 3 20 0
5 2 0 0 0 10000 2000
3 1 0 0 1000 1000 5000].*1000
road = [11 122122
12 11132122
13 1223
21 1112223331
22 111221233233
23 13223233
31 2132
32 31223323
33 3221321223
35 25
25 35
43 5354
53 4354
52 61
61 5271
75 76
76 666777
66 766777
67 667677
77 666776]
aeroplane = [11 33
33 1177
77 33]
port = [33 43
33 35
43 33
35 33
53 52
52 53
25 26
26 25
26 46
46 26
34 56
56 34
56 66
66 56]
L = vec([.6 .5 .5 0 0 0 0
.5 .6 .6 0 .4 .3 0
.5 .6 .7 0 .3 0 0
0 0 .5 0 0 .3 0
.4 .5 .3 .2 .4 .5 0
.5 .3 0 0 0 .9 .8
.6 .4 0 0 .8 .8 .9]')'
beta = 0.7
B = vec([10000 1000 1000 0 0 0 0
1500 8000 1000 0 200 200 0
500 2000 20000 0 200 0 0
0 0 200 0 0 20 0
40 200 400 200 30 300 0
80 40 0 0 0 10000 1200
50 20 0 0 600 900 6000]')'
pop_a = vec(pop')'
pop_all = pop_a
for uk in range(1,size(pop)[1]^2-1)
pop_all = vcat(pop_all,pop_a)
end
for jkl in range(1,2*size(pop)[1]^2)
B = hcat(B,0)
end
Lst = copy(L)
for jkl in range(1,2*size(pop)[1]^2)
Lst = hcat(Lst,0)
end
D = B.*(1.-Lst)
#----------------------------------------#
# Roads
roadmax = size(road)[1]
roads = eye(size(pop)[1]^2)
ports = eye(size(pop)[1]^2)
aeroplanes = eye(size(pop)[1]^2)
roadstart = 1
while roadstart <= roadmax
place = road[roadstart,1]
pdg = digits(place)
pdg2 = pdg[end:-1:1,end:-1:1]
down = (pdg2[1]-1)*7+pdg2[2]
other = road[roadstart,2]
digs = digits(other)[end:-1:1,end:-1:1]
len = size(digs)[1]
for xyz in range(1,convert(Int64,len/2))
down2 = (digs[(xyz*2)-1]-1)*size(pop)[1]+digs[(xyz*2)]
roads[down,down2]=1
end
roadstart += 1
end
currentroad = (1-eye(size(pop)[1]^2)).*roads.*pop_all
totalroad = sum(currentroad,2)
roadx = 1.6 /(1.6+0.4+0.1)
ratioroad = currentroad ./ totalroad
roadcorrect = copy(ratioroad)
for m=1:size(roadcorrect,1)
for l=1:size(roadcorrect,2)
isnan(roadcorrect[m,l]) ? roadcorrect[m,l]=0 : roadcorrect[m,l]=roadcorrect[m,l]
end
end
#----------------------------------------#
# Aeroplane
aeromax = size(aeroplane)[1]
aerostart = 1
while aerostart <= aeromax
place = aeroplane[aerostart,1]
pdg = digits(place)
pdg2 = pdg[end:-1:1,end:-1:1]
down = (pdg2[1]-1)*7+pdg2[2]
other = aeroplane[aerostart,2]
digs = digits(other)[end:-1:1,end:-1:1]
len = size(digs)[1]
for xyz in range(1,convert(Int64,len/2))
down2 = (digs[(xyz*2)-1]-1)*size(pop)[1]+digs[(xyz*2)]
aeroplanes[down,down2]=1
end
aerostart += 1
end
currentaero = (1-eye(size(pop)[1]^2)).*aeroplanes.*pop_all
totalaero = sum(currentaero,2)
aerox = 0.1 /(1.6+0.4+0.1)
ratioaero = currentaero ./ totalaero
aerocorrect = copy(ratioaero)
for m=1:size(aerocorrect,1)
for l=1:size(aerocorrect,2)
isnan(aerocorrect[m,l]) ? aerocorrect[m,l]=0 : aerocorrect[m,l]=aerocorrect[m,l]
end
end
#----------------------------------------#
# Ports
portmax = size(port)[1]
portstart = 1
while portstart <= portmax
place = port[portstart,1]
pdg = digits(place)
pdg2 = pdg[end:-1:1,end:-1:1]
down = (pdg2[1]-1)*7+pdg2[2]
other = port[portstart,2]
digs = digits(other)[end:-1:1,end:-1:1]
len = size(digs)[1]
for xyz in range(1,convert(Int64,len/2))
down2 = (digs[(xyz*2)-1]-1)*size(pop)[1]+digs[(xyz*2)]
ports[down,down2]=1
end
portstart += 1
end
currentport = (1-eye(size(pop)[1]^2)).*ports.*pop_all
totalport = sum(currentport,2)
portx = 0.4 /(1.6+0.4+0.1)
ratioport = currentport ./ totalport
portcorrect = copy(ratioport)
for m=1:size(portcorrect,1)
for l=1:size(portcorrect,2)
isnan(portcorrect[m,l]) ? portcorrect[m,l]=0 : portcorrect[m,l]=portcorrect[m,l]
end
end
#----------------------------------------#
## ADDITIONS
rr = roadcorrect.*roadx.*0.015
pp = portcorrect.*portx.*0.015
aa = aerocorrect.*aerox.*0.015
needleft = 1 .- sum(rr+pp+aa,2)
eyeget = eye(size(pop)[1]^2) .* needleft
ssss = eyeget + aa + rr + pp
#----------------------------------------#
large = size(ssss)[1]
Ipart = zeros(1,size(pop)[1]^2)
Ipart[1,13]=1
Rpart = zeros(1,size(pop)[1]^2)
SIR = hcat(pop_a,Ipart,Rpart)
table = ["s1"]
for x in range(2,size(pop)[1]^2-1)
h = string("s",x)
table = hcat(table,h)
end
for x in range(1,size(pop)[1]^2)
h = string("i",x)
table = hcat(table,h)
end
for x in range(1,size(pop)[1]^2)
h = string("r",x)
table = hcat(table,h)
end
old_SIR = zeros(1,large*3)
mosquito = zeros(1,large)
old_ir = zeros(large,1)
#----------------------------------------#
for x in range(1,iterations)
temp = rand(10:44,1,large)
prec = rand(100:400,1,large)
#----------------------------------------#
# Temperature first
sd = 7; mean = 27; sc = 13.5
h = (1/(sd*sqrt(2*pi)))*sc
f(t) = (exp((-(t-mean).^2)/(2*sd^2)))*h
T=f(temp)
total = sum(T)
ss = size(T)[2]
temperature = eye(ss)
for uu in 1:ss
Q = copy(vec(T))
Q[uu] = 0
jj = 0
for x in Q
if x == 0
d = T[uu]
else
y = total-T[uu]
b = x/y
c = 1-T[uu]
d = c*b
end
jj += 1
temperature[uu,jj] = d
end
end
#----------------------------------------#
# Precipitation next
sd = 50; mean = 250; sc = 100
h = (1/(sd*sqrt(2*pi)))*sc
f(t) = (exp((-(t-mean).^2)/(2*sd^2)))*h
T=f(prec)
total = sum(T)
ss = size(T)[2]
precipitation = eye(ss)
for uu in 1:ss
Q = copy(vec(T))
Q[uu] = 0
jj = 0
for x in Q
if x == 0
d = T[uu]
else
y = total-T[uu]
b = x/y
c = 1-T[uu]
d = c*b
end
jj += 1
precipitation[uu,jj] = d
end
end
#----------------------------------------#
# Living standards next
T = 1 - L
total = sum(T)
ss = size(T)[2]
living = eye(ss)
for uu in 1:ss
Q = copy(vec(T))
Q[uu] = 0
jj = 0
for x in Q
if x == 0
d = T[uu]
else
y = total-T[uu]
b = x/y
c = 1-T[uu]
d = c*b
end
jj += 1
living[uu,jj] = d
end
end
#----------------------------------------#
# Now probability for mosquito
mosq_p = temperature*precipitation*living
mosq_b = rand(1000:2000,1,large)
mosq_d = rand(500:600,1,large)
w_scalar = 0.7
mosquito = (mosquito + (mosq_b*0.7 - mosq_d))*mosq_p
# Now scaling for mosquito
addup = sum(mosquito)
elements = size(mosquito)[2]
average = addup / elements
addon_all = 0
for ippa in mosquito
addon = (ippa - average)^2
addon_all = addon + addon_all
end
sd = (1/elements*addon_all)^0.5
z = (mosquito .- average) ./ sd
a = 111.*atan(27.*z/294)
b = -358.*z./23
c = b+a
d = exp(c)
attack_rate = 1./(1.+d)
#----------------------------------------#
# S -> I matrix
u = SIR[large+1:2*large] ./ (SIR[1:large]+SIR[large+1:2*large]+SIR[2*large+1:3*large])
# pq = (SIR[large+1:2*large] .- old_SIR[1+large:2*large])
# qrd = (SIR[1:large] .- old_SIR[1:large])
ratio = 0.5
IR = attack_rate' .* u .* ratio
# constant = 0.1
# IRRR = (2*exp(1/2*constant*x))/(1+exp(1/2*constant*x))
# IRR = IRRR .* (SIR[10:18]./(SIR[1:9].+SIR[10:18].+SIR[19:27]))
# IR = IRRR .* IRR
kk = 1
infection = zeros(elements)
for xp in isnan(IR)*1
if xp == 0
infection[kk] = IR[kk]
else
infection[kk] = 0.0
end
kk += 1
end
old_SIR = copy(SIR)
old_ir = copy(IR)
s_i = infection' .* eye(elements)
global(s_i)
#----------------------------------------#
# I -> R matrix
opp = 20
i_r1 = L ./opp
i_r = eye(elements).*i_r1
#----------------------------------------#
# S -> S matrix
nrow = size(ssss)[1]
h = sum(eye(nrow,nrow)-s_i,2)
s_s = ssss.*h
#----------------------------------------#
# I -> I matrix
leftover = (1-sum(i_r,2))
scaled = leftover.*s_s
other = (scaled.*(1-eye(large))).*0.5
othertotal = sum(other,2)
correct = (scaled.+othertotal).*eye(large).+other
i_i = (leftover-sum(correct,2).+correct).*eye(large).+correct.*(1-eye(large))
#----------------------------------------#
# R -> R matrix
gg = (leftover.-s_s).*eye(nrow,nrow).*2/nrow
hh = 1 .- sum(s_s+gg,2)
qp = (1.-eye(nrow,nrow)).*s_s
vv = qp ./ sum(qp,2) .*hh
ooo = 1
for xpd in isnan(vv)*1
if xpd == 1
vv[ooo] = 0
end
ooo += 1
end
r_r = gg+s_s+vv
#----------------------------------------#
z = zeros(nrow,nrow)
P1 = hcat(s_s,s_i,z)
P2 = hcat(z,i_i,i_r)
P3 = hcat(z,z,r_r)
p = vcat(P1,P2,P3)
SIR = (SIR+(B*beta-D))*p
table = vcat(table,SIR)
end
#----------------------------------------#
table = table'
starting = ["Date"]
for x in range(1,iterations)
starting = hcat(starting,x)
end
table = vcat(starting,table)
writecsv("Data.csv",table, header = false)