Skip to content

Commit c1ba9bf

Browse files
committed
Zhong scenarios generator
1 parent adad2ca commit c1ba9bf

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
import argparse
3+
4+
parser = argparse.ArgumentParser(description='Auxiliar script to generate Zhong-based CA models')
5+
6+
parser.add_argument('-i', '--incubation_steps', type=int, default=2, help='Number of days for the incubation state')
7+
parser.add_argument('-f', '--infected_steps', type=int, default=2, help='Number of days for the infected state')
8+
parser.add_argument('-l', '--latent_steps', type=int, default=2, help='Number of days for the latent state')
9+
parser.add_argument('-r', '--recovered_steps', type=int, default=3, help='Number of days for the recovered state')
10+
parser.add_argument('-t', '--template_ma', type=str, default="template_sirs_zhong_ma", help='Path to zhong .ma template')
11+
parser.add_argument('-m', '--template_macros', type=str, default="template_sirs_zhong_macros", help='Path to zhong macros template')
12+
parser.add_argument('-o', '--out_ma_file', type=str, default="out.ma", help='Output path of the .ma file')
13+
parser.add_argument('-p', '--out_macros_file', type=str, default="sirs_zhong_macros.inc", help='Output path of the macros file')
14+
15+
args = parser.parse_args()
16+
17+
i = 1
18+
states = ["sus_0"]
19+
neighbors = ["(1,0)", "(0,-1)", "(0,1)", "(-1,0)"]
20+
21+
while i <= args.incubation_steps:
22+
states.append("inc_%d" % i)
23+
i += 1
24+
25+
while i <= args.incubation_steps + args.infected_steps:
26+
states.append("inf_%d" % i)
27+
i += 1
28+
29+
while i <= args.incubation_steps + args.infected_steps + args.latent_steps:
30+
states.append("lat_%d" % i)
31+
i += 1
32+
33+
while i <= args.incubation_steps + args.infected_steps + args.latent_steps + args.recovered_steps:
34+
states.append("rec_%d" % i)
35+
i += 1
36+
37+
##############################
38+
# GENERATION OF THE .MA FILE
39+
##############################
40+
content = ""
41+
with open(args.template_ma, "r") as f:
42+
content = f.read()
43+
44+
content = content.replace("[[internal_vars]]", " ".join(["i_%s" % s for s in states]))
45+
content = content.replace("[[internal_vars_values]]", " ".join(["1.0"] + ["0.0"] * (len(states)-1)))
46+
update_ports_str = " ".join(["~%s := $i_%s;" % (s, s) for s in states])
47+
content = content.replace("[[initial_ports_set_up]]", update_ports_str)
48+
content = content.replace("[[update_ports_step]]", update_ports_str)
49+
content = content.replace("[[ports]]", " ".join(states))
50+
51+
update_rules = ""
52+
idx_first_rec = len(states)-args.recovered_steps
53+
for i in range(len(states)-1, idx_first_rec, -1):
54+
update_rules += "$i_%s := $i_%s;\n" % (states[i], states[i-1])
55+
update_rules += "$i_%s := $i_%s + #macro(local_cured);\n\n" % (states[idx_first_rec], states[idx_first_rec-1])
56+
57+
for i in range(idx_first_rec-1, 1, -1):
58+
update_rules += "$i_%s := trunc(min((1 - $cured_rate) * $i_%s, 1)*100)/100;\n" % (states[i], states[i - 1])
59+
update_rules += "$i_%s := #macro(internal_infected) + #macro(external_infected);\n\n" % states[1]
60+
61+
update_rules += "$i_%s := 1 " % states[0]
62+
update_rules += " ".join(["- $i_%s" % s for s in states[1:]])
63+
update_rules += ";\n"
64+
65+
content = content.replace("[[update_vars_step]]", update_rules)
66+
67+
with open(args.out_ma_file, "w") as f:
68+
f.write(content)
69+
70+
#################################
71+
# GENERATION OF THE MACROS FILE
72+
#################################
73+
74+
content = ""
75+
with open(args.template_macros, "r") as f:
76+
content = f.read()
77+
78+
local_cured_rules = []
79+
for i in range(idx_first_rec-2, 0, -1):
80+
local_cured_rules.append("trunc(min($cured_rate * $i_%s, 1)*100)" % states[i])
81+
82+
content = content.replace("[[local_cured]]", "(( %s ) / 100 )" % " +\n".join(local_cured_rules))
83+
content = content.replace("[[infected_vars]]", " + ".join(["i_%s" % s for s in states[idx_first_rec:0]]))
84+
85+
external_infected_comp = \
86+
"""( trunc(min(( $connection *
87+
$contact_rate *
88+
(($i_sus_0 * $population) / $area) *
89+
$i_sus_0 *
90+
([[external_infected]])
91+
) / $p, 1)*100) / 100 )
92+
"""
93+
94+
external_infected_rules = []
95+
for nei in neighbors:
96+
inf = " + ".join(["%s~%s" % (nei, s) for s in states[idx_first_rec-1:0:-1]])
97+
external_infected_rules.append(external_infected_comp.replace("[[external_infected]]", inf))
98+
99+
content = content.replace("[[external_infected]]", "+\n".join(external_infected_rules))
100+
101+
with open(args.out_macros_file, "w") as f:
102+
f.write(content)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include(sirs_zhong_macros.inc)
2+
[top]
3+
components : area
4+
5+
[area]
6+
type : cell
7+
width : 50
8+
height : 50
9+
delay : transport
10+
defaultDelayTime : 1
11+
border : wrapped
12+
neighbors : area(-1,0)
13+
neighbors : area(0,-1) area(0,0) area(0,1)
14+
neighbors : area(1,0)
15+
initialvalue : -1
16+
17+
localtransition : sirs-zhong-rule
18+
19+
statevariables: population area p connection contact_rate cured_rate t_i t_p t_l t_r [[internal_vars]]
20+
statevalues: 100 1 500 1 1 0.00 2 2 2 3 [[internal_vars_values]]
21+
initialvariablesvalue: sirs_zhong.var
22+
23+
neighborports : initial pop [[ports]]
24+
25+
[sirs-zhong-rule]
26+
rule : {~initial := 0; ~pop := $population; [[initial_ports_set_up]]}
27+
1 {(0,0)~initial = -1}
28+
29+
rule : {
30+
~pop := $population; [[update_ports_step]]
31+
}
32+
{
33+
[[update_vars_step]]
34+
}
35+
1 { (0,0)~initial != -1 }
36+
37+
38+
39+
40+
41+
42+
43+
44+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#BeginMacro(local_cured)
3+
[[local_cured]]
4+
#EndMacro
5+
6+
#BeginMacro(internal_infected)
7+
( trunc(min(($contact_rate *
8+
(($i_sus_0 * $population) / $area) *
9+
$i_sus_0 *
10+
($i_lat_6 + $i_lat_5 + $i_inf_4 + $i_inf_3 + $i_inc_2 + $i_inc_1)
11+
) / $p, 1) * 100) / 100 )
12+
#EndMacro
13+
14+
#BeginMacro(external_infected)
15+
[[external_infected]]
16+
#EndMacro

0 commit comments

Comments
 (0)