-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorld.py
More file actions
435 lines (353 loc) · 17.1 KB
/
World.py
File metadata and controls
435 lines (353 loc) · 17.1 KB
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
import json
import uuid
from multiprocessing import Pool
# from data_distribution import *
from timeit import default_timer as timer
from uuid import UUID
import numpy as np
from odmantic import ObjectId, SyncEngine
from constants import (PRE_UNIVERSITY_SCHOOL, PRIMARY_SCHOOL, SECONDARY_SCHOOL,
UNIVERSITY_SCHOOL)
from models.action import Action
from models.person import Person, PersonFactory, create_parallel, move_person
from models.place import Place
from models.population import Population
from models.data_source import DataSource
# from pathos import multiprocessing
# from data_loader import DataLoader
# from simulation.Person import Person as SimP
# from simulation.Household import Household
# from simulation.School import School
# from simulation.workplace import Workplace, WorkplaceSize
db = SyncEngine(database='contact_simulation_2n', )
class World:
"""Environment class where are stored all agents and data for simulation
Attributes:
-----------
neighborhoods (dict):
dictionary containing a list of Households per province representing "near" houses
schools (dict):
dictionary containing another dictionary with a list of schools per type, per province
'province1': {
'type1' : [schools]
...
}
workplaces (dict):
dictionary containing a list of workplaces per province
"""
def __init__(self, json_file_path: str | None = None, data_source: DataSource | None = None):
"""initialization function
"""
if data_source is None:
if json_file_path is None:
raise ValueError(
'json_file_path and data_source cannot be None at the same time')
with open(json_file_path, 'r') as j:
contents = json.loads(j.read())
self.data_source = DataSource(**contents)
else:
self.data_source = data_source
# self.data_source = DataSource()
# self.data_source.vectorize_data()
self.age_groups = np.array(self.data_source.age_groups)
self.people_that_work = []
self.mat = {
"work": np.zeros((len(self.data_source.age_groups),
len(self.data_source.age_groups))),
"school": np.zeros((len(self.data_source.age_groups),
len(self.data_source.age_groups))),
"neighborhood": np.zeros((len(self.data_source.age_groups),
len(self.data_source.age_groups))),
"house": np.zeros((len(self.data_source.age_groups),
len(self.data_source.age_groups)))
}
self.mat_ages = np.ones(len(self.data_source.age_groups))
self.total_population = 0
self.db = SyncEngine(database='contact_simulation_2n')
self.politics_deployed = {
'household': 1,
'school': 1,
'workplace': 1,
'neighborhood': 1,
'random place': 1
}
# multi threaded process to speed up population generation
# 16GB RAM dies with 2 threads... use under own responsability
# with Pool(n_processes) as p:
# results = []
# results.append(p.map(self.generate_neighborhoods,
# (data_source.provinces_population), 3))
def create_people_by_household(self, prov_id: str, data_source: DataSource, household: dict, schools: dict[str, list[UUID]]):
# First guarantee that there is at least one adult in the household
people = [PersonFactory.create(
data_source, household, schools, prov_id, True)]
temp = household['number_of_people'] - 1
if people[0].work == True:
self.people_that_work.append(people[0])
if temp > 0:
for i in range(temp):
pers_i = PersonFactory.create(
data_source, household, schools, prov_id)
if pers_i.work == True:
self.people_that_work.append(pers_i)
people.append(pers_i)
self.mat_ages[people[-1].age_group] += 1
db.save_all(people)
def get_age_group(self, age: int):
"""
Get the age group a person belongs to, this is to find the cells in the contact matrix that should be
updated
Args:
age (int): Person's age
Returns:
int: index in the age groups matrix
"""
interval_index = np.where(
(age >= self.age_groups[:, 0]) & (age <= self.age_groups[:, 1]))[0]
if interval_index.size > 0:
interval_index = interval_index[0]
else:
interval_index = -1
return interval_index
def generate_population(self, population_name: str, n_processes: int = 12):
"""Province by province generate population
Args:
population_name (str): Population name, this will be the name to access
the generated population from the DB
n_processes (int, optional): Number of concurrent Processes to run. Defaults to 12.
"""
population = Population(name=population_name, provinces=[])
provinces = {}
for i in self.data_source.provinces_population:
provinces[i] = str(uuid.uuid4())
start_time = timer()
# self.generate_province(province=i, n_processes=n_processes)
self.generate_province(
i, provinces[i], n_processes=n_processes)
# provinces.append(p_id)
print('Finished in', timer() - start_time)
population.provinces = provinces
self.db.save(population)
self.population_name = population_name
# for i in self.data_source.provinces_population:
# self.db.insert_one("Population", {"population_name": population_name,
# "provinces": provinces, "total_population": self.total_population})
def generate_province(self, province: str, prov_id: str, n_processes: int = 12, verbose=3):
"""generate neigborhoods for a given province
Args:
province (str):
province name, to access province related data and for taggig
the data generated here
n_processes (int): number of processes to run concurrently
verbose (int):
integer denoting log level, for debug proposes (default: 0)
"""
neighborhoods = {}
# workplaces: dict[str, list[Workplace]] = {}
# according to distribution, the province population is divided by
# the number of people per neighborhood
# resulting on the number of neighborhoods in the province
total_neighborhoods = int(
self.data_source.provinces_population[province]/(self.data_source.neighborhoods_per_thousand_people*1000))+1
if verbose >= 2:
print(province)
# print(total_neighborhoods)
# according to distribution, number of schools of each type is calculated
primary_schools_len = max(int(
(self.data_source.provinces_population[province]/1000) * self.data_source.primary_schools_per_thousand_people), 1)
secondary_schools_len = max(int(
(self.data_source.provinces_population[province]/1000) * self.data_source.secondary_schools_per_thousand_people), 1)
pre_univ_schools_len = max(int(
(self.data_source.provinces_population[province]/1000) * self.data_source.pre_universitary_schools_per_thousand_people), 1)
university_schools_len = max(int(
self.data_source.universities_per_province), 1)
schools = {
PRIMARY_SCHOOL: [],
SECONDARY_SCHOOL: [],
PRE_UNIVERSITY_SCHOOL: [],
UNIVERSITY_SCHOOL: []
}
places = []
list_ids = []
for _ in range(primary_schools_len):
p_id = str(uuid.uuid4())
list_ids.append(p_id)
places.append(
Place(place=p_id, province=prov_id, clasification="school"))
schools[PRIMARY_SCHOOL] = list_ids
list_ids = []
for _ in range(secondary_schools_len):
p_id = str(uuid.uuid4())
list_ids.append(p_id)
places.append(
Place(place=p_id, province=prov_id, clasification="school"))
schools[SECONDARY_SCHOOL] = list_ids
list_ids = []
for _ in range(pre_univ_schools_len):
p_id = str(uuid.uuid4())
list_ids.append(p_id)
places.append(
Place(place=p_id, province=prov_id, clasification="school"))
schools[PRE_UNIVERSITY_SCHOOL] = list_ids
list_ids = []
for _ in range(university_schools_len):
p_id = str(uuid.uuid4())
list_ids.append(p_id)
places.append(
Place(place=p_id, province=prov_id, clasification="school"))
schools[UNIVERSITY_SCHOOL] = list_ids
# the neighborhoods are created
possible_people_by_household = np.arange(start=1, stop=10, step=1)
inhabitants_distribution = np.array(
self.data_source.inhabitants_distribution)
total_households_by_neighborhoods = int(
700 / (np.argmax(inhabitants_distribution)+1))
people_number_by_household = np.random.choice(a=possible_people_by_household,
p=inhabitants_distribution,
size=(total_neighborhoods, total_households_by_neighborhoods))
# total_people = sum(people_number_by_household[i])
# for i in range(people_number_by_household.shape[0])])
# Create household list from people_number_by_household
households = []
for i in range(people_number_by_household.shape[0]):
neigh_id = uuid.uuid4()
places.append(Place(place=str(neigh_id),
province=prov_id, clasification="neighborhood"))
for j in range(people_number_by_household.shape[1]):
h_id = uuid.uuid4()
places.append(
Place(place=str(h_id), province=prov_id, clasification="house"))
households.append({'id': str(
h_id), 'number_of_people': people_number_by_household[i][j], 'neighborhood_id': str(neigh_id)})
start_time = timer()
for h in households:
# with Pool(30) as p:
# p.apply_async(create_people_by_household,
# args=(prov_id, self.data_source, h, schools))
self.create_people_by_household(
prov_id, self.data_source, h, schools)
# person_list.append(str(i.id))
del households
del schools
print('Finished people in ', timer() - start_time)
start_time = timer()
# people_that_work = list(self.db.find(
# Person, Person.work == True))
assert self.people_that_work is not None
assert len(self.people_that_work) is not None
print(len(self.people_that_work))
people_that_work = self.assign_workplaces_to_people(
province, prov_id, len(self.people_that_work), self.people_that_work)
self.db.save_all(self.people_that_work)
self.db.save_all(places)
print('Finished workers in ', timer() - start_time)
if verbose >= 2:
print("Finished:", province)
def assign_workplaces_to_people(self, province: str, prov_id: str, total_workers: int, people_that_works: list[Person]):
workers_count = 0
workplace_size_by_people = np.random.choice(
a=[0, 1, 2, 3], size=len(people_that_works))
people_mask = np.zeros(len(people_that_works))
workplaces = []
places = []
while workers_count < total_workers:
size = np.random.choice(a=[0, 1, 2, 3], size=1)[0]
wp = str(uuid.uuid4())
places.append(
Place(place=wp, province=prov_id, clasification='work'))
wp_current_workers = 0
for i in range(len(people_that_works)):
if people_mask[i] == 0 and workplace_size_by_people[i] == size:
people_that_works[i].workplace = wp
# workplaces.append(wp)
people_mask[i] = 1
workers_count += 1
wp_current_workers += 1
if wp_current_workers == size:
break
self.db.save_all(places)
return people_that_works
def run_simulation(self, population_name: str, n_days: int = 2, n_processes=12):
"""Code to run simulation, it gets the contacts based on some pre-stablished rules
Args:
population_name (str): name of the population to simulate, it will load the simulation from DB
n_days (int, optional): number of days to simmulate, the more days more accurate should be. Defaults to 2.
n_processes (int, optional): number of processes to concurrenlty run. Defaults to 12.
"""
start_time = timer()
population = self.db.find_one(
Population, Population.name == population_name)
if population:
provinces = population.provinces
# each day will be divided in 4 moments
# morning:
# noon:
# afternoon:
# night:
for i in range(1, n_days):
print(i)
for time in ['morning', 'noon', 'afternoon', 'night']:
for prov in provinces:
prov_id = provinces[prov]
province = self.db.find(
Person, Person.province == prov_id)
for person in province:
assert person != None, 'Person cant be None'
place = move_person(
person, i, time, self.politics_deployed, self.db)
# pers = person.__dict__
# pers['id'] = str(person.id)
# # print(str(person.id))
# person_obj = SimP.load_serialized(pers)
# place = person_obj.move(
# i, time, self.politics_deployed)
person.current_place = place
action = Action(destination=person.current_place, person=person.id,
day=i, time=time, simulation_id=1)
self.db.save(action)
self.db.save(person)
print("Implementing contact reduction politics")
politics = {
"close_shcools": {"school": 0},
"mid_shcools": {"school": 0.5},
"reduce_movility": {"neighborhood": 0.6, "random_place": 0.3},
"close_workplaces": {"workplace": 0.1},
"social_awareness": {key: 0.2 for key in self.politics_deployed.keys()}
}
print(f"Finished in {timer() - start_time}")
# apply_politics(politics)
def generate_contact_matrix(self, population_name: str, n_days: int):
population = self.db.find_one(
Population, Population.name == population_name)
for province in population.provinces:
for day in range(1, n_days+1):
print(day)
for time in ['morning', 'noon', 'afternoon', 'night']:
for place in db.find(Place, Place.province == population.provinces[province]):
actions = self.db.find(
Action, Action.destination == place.place, Action.day == day, Action.time == time)
people = [act.person for act in actions]
if len(people) == 0:
continue
# print(people)
interacted = np.random.choice(
people, np.random.poisson(lam=(len(people)/3)))
for p_id in interacted:
person = self.db.find_one(
Person, Person.id == ObjectId(p_id))
if not person:
continue
person.interacted.extend(
list(zip(interacted, [place.clasification] * len(interacted))))
if time == 'night':
self.add_contacts_to_matrix(
person.interacted, person.age_group)
person.interacted = []
# print(person)
self.db.save(person)
return self.mat
def add_contacts_to_matrix(self, interacted, age_group):
for p_id, place in interacted:
person = self.db.find_one(Person, Person.id == ObjectId(p_id))
self.mat[place][age_group][person.age_group] += 1
self.mat[place][person.age_group][age_group] += 1