Skip to content

Commit 88e30c8

Browse files
add lockdown capability
1 parent 34efd3e commit 88e30c8

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

simulation.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
#set seed for reproducibility
1515
np.random.seed(100)
1616

17-
1817
def update(frame, population, destinations, pop_size, infection_range=0.01,
19-
infection_chance=0.03, recovery_duration=(200, 500), mortality_chance=0.02,
18+
infection_chance=0.03, speed=0.01, recovery_duration=(200, 500), mortality_chance=0.02,
2019
xbounds=[0.02, 0.98], ybounds=[0.02, 0.98], x_plot=[0, 1],
2120
y_plot=[0, 1], wander_range=0.05, risk_age=55,
2221
critical_age=75, critical_mortality_chance=0.1,
2322
risk_increase='quadratic', no_treatment_factor=3,
24-
treatment_factor=0.5, healthcare_capacity=250, age_dependent_risk=True,
25-
treatment_dependent_risk=True, visualise=True, verbose=True,
23+
treatment_factor=0.5, healthcare_capacity=250, age_dependent_risk=False,
24+
treatment_dependent_risk=False, visualise=False, verbose=False,
2625
self_isolate=True, self_isolate_proportion=0.6, isolation_bounds=[0, 0, 0.1, 0.1],
27-
traveling_infects=True):
26+
traveling_infects=False, lockdown=False, lockdown_percentage=0.1,
27+
lockdown_vector=[]):
2828

2929
#add one infection to jumpstart
3030
if frame == 50:
@@ -52,9 +52,25 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
5252
population[population[:,11] == 0] = out_of_bounds(population[population[:,11] == 0],
5353
_xbounds, _ybounds)
5454

55-
#update randoms
56-
population = update_randoms(population, pop_size)
55+
56+
if lockdown:
57+
if len(infected_plot) == 0:
58+
mx = 0
59+
else:
60+
mx = np.max(infected_plot)
61+
62+
if len(population[population[:,6] == 1]) >= len(population) * lockdown_percentage or\
63+
mx >= (len(population) * lockdown_percentage):
64+
#set speeds of complying people to 0
65+
population[:,5][lockdown_vector == 0] = 0
66+
else:
67+
#update randoms
68+
population = update_randoms(population, pop_size, speed=speed)
69+
else:
70+
#update randoms
71+
population = update_randoms(population, pop_size, speed=speed)
5772

73+
5874
#for dead ones: set speed and heading to 0
5975
population[:,3:5][population[:,6] == 3] = 0
6076

@@ -154,12 +170,12 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
154170
###############################
155171
#set simulation parameters
156172
simulation_steps = 10000 #total simulation steps performed
157-
save_population = False #whether to dump population to data/population_{num}.npy
173+
save_population = True #whether to dump population to data/population_{num}.npy
158174
#size of the simulated world in coordinates
159-
xbounds = [0.1, 1.1]
175+
xbounds = [0, 1]
160176
ybounds = [0, 1]
161177

162-
x_plot = [0, 1.1]
178+
x_plot = [0, 1]
163179
y_plot = [0, 1]
164180

165181
visualise = True #whether to visualise the simulation
@@ -169,6 +185,7 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
169185
pop_size = 2000
170186
mean_age=55
171187
max_age=105
188+
speed=0.01
172189

173190
#motion parameters
174191
mean_speed = 0.01 # the mean speed (defined as heading * speed)
@@ -187,16 +204,23 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
187204
mortality_chance=0.02 #global baseline chance of dying from the disease
188205

189206
#self isolation
190-
self_isolate = True #whether infected people will self-isolate
191-
self_isolate_proportion = 0.8 #proportion of infected
207+
self_isolate = False #whether infected people will self-isolate
208+
self_isolate_proportion = 0.85 #proportion of infected
192209
isolation_bounds = [0.01, 0.01, 0.1, 0.99] #[xmin, ymin, xmax, ymax]
193210
traveling_infects = False #Whether those traveling to isolation can still infect others
194211

212+
#lock down
213+
lockdown = True #whether to implement a lockdown
214+
lockdown_percentage = 0.1 #after this proportion is infected, lock-down begins
215+
lockdown_compliance = 0.95 #fraction of the population that will obey the lockdown
216+
lockdown_vector = np.zeros((pop_size,))
217+
#lockdown vector is 1 for those not complying
218+
lockdown_vector[np.random.uniform(size=(pop_size,)) >= lockdown_compliance] = 1
219+
195220
#healthcare parameters
196221
healthcare_capacity = 300 #capacity of the healthcare system
197222
treatment_factor = 0.5 #when in treatment, affect risk by this factor
198223
no_treatment_factor = 3 #risk increase factor to use if healthcare system is full
199-
200224
#risk parameters
201225
age_dependent_risk = True #whether risk increases with age
202226
risk_age = 55 #age where mortality risk starts increasing
@@ -210,7 +234,7 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
210234
######################################
211235
##### END OF SETTABLE PARAMETERS #####
212236
######################################
213-
237+
214238
#initialise population
215239
population = initialize_population(pop_size, mean_age, max_age, xbounds, ybounds)
216240

@@ -235,13 +259,14 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
235259
fatalities_plot = []
236260

237261
#define arguments for visualisation loop
238-
fargs = (population, destinations, pop_size, infection_range, infection_chance,
262+
fargs = (population, destinations, pop_size, infection_range, infection_chance, speed,
239263
recovery_duration, mortality_chance, xbounds, ybounds, x_plot, y_plot,
240264
wander_range, risk_age, critical_age, critical_mortality_chance,
241265
risk_increase, no_treatment_factor, treatment_factor,
242266
healthcare_capacity, age_dependent_risk, treatment_dependent_risk,
243267
visualise, verbose, self_isolate, self_isolate_proportion,
244-
isolation_bounds,traveling_infects,)
268+
isolation_bounds,traveling_infects, lockdown, lockdown_percentage,
269+
lockdown_vector,)
245270

246271
#start animation loop through matplotlib visualisation
247272
if visualise:
@@ -250,13 +275,14 @@ def update(frame, population, destinations, pop_size, infection_range=0.01,
250275
else:
251276
#alternatively dry run simulation without visualising
252277
for i in range(simulation_steps):
253-
population = update(i, population, destinations, pop_size, infection_range, infection_chance,
278+
population = update(i, population, destinations, pop_size, infection_range, infection_chance, speed
254279
recovery_duration, mortality_chance, xbounds, ybounds, x_plot, y_plot,
255280
wander_range, risk_age, critical_age, critical_mortality_chance,
256281
risk_increase, no_treatment_factor, treatment_factor,
257282
healthcare_capacity, age_dependent_risk, treatment_dependent_risk,
258283
visualise, verbose, self_isolate, self_isolate_proportion,
259-
isolation_bounds, traveling_infects)
284+
isolation_bounds, traveling_infects, lockdown, lockdown_percentage,
285+
lockdown_vector)
260286
if len(population[population[:,6] == 1]) == 0 and i > 100:
261287
print('\n-----stopping-----\n')
262288
print('total dead: %i' %len(population[population[:,6] == 3]))

0 commit comments

Comments
 (0)