Skip to content

Commit

Permalink
Very small performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
AshtonIzmev committed May 17, 2020
1 parent 58f9433 commit 0356214
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test:
python -m tests.run

coverage:
coverage run -m pytest
coverage run -m pytest && coverage xml

report:
coverage report
Expand Down
2 changes: 2 additions & 0 deletions simulator/constants/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
WI_K = "work_to_individual_mapping"
HA_K = "house_to_adult_mapping"
HS_K = "house_to_store_mapping"
IS_K = "individual_to_store_mapping"
SI_K = "store_to_individual_mapping"
ITI_K = "individual_transport_individual_mapping"

HB_K = "house_to_block_mapping"
Expand Down
14 changes: 7 additions & 7 deletions simulator/helper/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def update_infection_period(newly_infected_individuals_arg, virus_dic):
for i in newly_infected_individuals_arg:
if virus_dic[STA_K][i] == HEALTHY_V:
virus_dic[STA_K][i] = INFECTED_V
virus_dic[NC_K] = virus_dic[NC_K] + 1
virus_dic[NC_K] += 1


def update_immunity_state(virus_dic):
for i in get_immune_people(virus_dic):
# Losing immunity
virus_dic[IMM_K][i] = virus_dic[IMM_K][i] - 1
virus_dic[IMM_K][i] -= 1
if virus_dic[IMM_K][i] == 0:
virus_dic[STA_K][i] = HEALTHY_V
virus_dic[CON_K][i] = virus_dic[CON_INIT_K][i]
Expand Down Expand Up @@ -42,9 +42,9 @@ def decide_life_immunity(env_dic, virus_dic, icu_factor):

def decrement_virus_carrier_periods(virus_dic):
for i in get_virus_carrier_people(virus_dic):
virus_dic[CON_K][i] = virus_dic[CON_K][i] - 1
virus_dic[HOS_K][i] = virus_dic[HOS_K][i] - 1
virus_dic[DEA_K][i] = virus_dic[DEA_K][i] - 1
virus_dic[CON_K][i] -= 1
virus_dic[HOS_K][i] -= 1
virus_dic[DEA_K][i] -= 1


def increment_pandemic_1_day(env_dic, virus_dic, available_beds):
Expand Down Expand Up @@ -189,14 +189,14 @@ def propagate_to_stores(env_dic, virus_dic, probability_store_infection_arg, sam
# [(2, 0), (3, 1), (6, 2)] 2, 3 and 6 are infected and going to the stores 0, 1 and 2
# we divide same_store_preference by behavior since high behavior value is bad behavior
individuals_healthy_gotostore = [
(i, choose_weight_order(env_dic[HS_K][env_dic[IH_K][i]], same_store_preference / env_dic[IBE_K][i]))
(i, choose_weight_order(env_dic[IS_K][i], same_store_preference / env_dic[IBE_K][i]))
for i in individuals_gotostore if not is_contagious(i, virus_dic)
]

# Stores that will be visited by a contagious person
# [ (0, 1), (0, 1.3), (2, 1.2)] where stores 0 and 2 are infected with 1, 1.3 and 1.2 weights
infected_stores = [
(choose_weight_order(env_dic[HS_K][env_dic[IH_K][i]], same_store_preference / env_dic[IBE_K][i]), env_dic[IBE_K][i])
(choose_weight_order(env_dic[IS_K][i], same_store_preference / env_dic[IBE_K][i]), env_dic[IBE_K][i])
for i in individuals_infected_gotostore
]

Expand Down
18 changes: 17 additions & 1 deletion simulator/helper/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from scipy import spatial

from simulator.constants.keys import nindividual_key, store_per_house_key, nb_1d_block_key, store_nb_choice_key, \
transport_contact_cap_key, IH_K, HI_K, IAD_K, IAG_K, IW_K, WI_K, HA_K, HS_K, ITI_K, HB_K, IBE_K, IDEA_K, IHOS_K
transport_contact_cap_key, IH_K, HI_K, IAD_K, IAG_K, IW_K, WI_K, HA_K, HS_K, ITI_K, HB_K, IBE_K, IDEA_K, IHOS_K, \
IS_K, SI_K
from simulator.constants.parameters import TPE_MAX_EMPLOYEES, PME_MAX_EMPLOYEES, GE_MAX_EMPLOYEES, covid_mortality_rate, \
covid_hospitalization_rate
from simulator.constants.parameters import age_dist_adults_cs, age_dist_adults, \
Expand Down Expand Up @@ -38,6 +39,8 @@ def get_environment_simulation(params_arg):
geo_store = build_geo_positions_store(int(len(house_indiv) / number_store_per_house_arg))

house_store = build_house_store_map(geo_store, geo_house, nb_store_choice)
indiv_store = build_individual_store_map(indiv_house, house_store)
store_indiv = build_store_individual_map(indiv_store)

house_block = build_block_assignment(geo_house, nb_1d_block_arg)
workplace_block = build_block_assignment(geo_workplace, nb_1d_block_arg)
Expand All @@ -63,6 +66,8 @@ def get_environment_simulation(params_arg):
WI_K: workplace_indiv,
HA_K: house_adult,
HS_K: house_store,
IS_K: indiv_store,
SI_K: store_indiv,
ITI_K: indiv_transport_indiv,
HB_K: house_block,
IBE_K: indiv_behavior
Expand Down Expand Up @@ -188,6 +193,17 @@ def build_house_store_map(geo_position_store_arg, geo_position_house_arg, nb_sto
return all_hou_sto


def build_individual_store_map(indiv_house_arg, house_store_arg):
all_ind_sto = {}
for i in range(len(indiv_house_arg)):
all_ind_sto[i] = house_store_arg[indiv_house_arg[i]]
return all_ind_sto


def build_store_individual_map(indiv_store_arg):
return invert_map_list(indiv_store_arg)


def build_individual_work_map(individual_adult_map_arg):
# Only adults work
workers = list([ind for ind, is_adult in individual_adult_map_arg.items() if is_adult == 1])
Expand Down
30 changes: 28 additions & 2 deletions tests/simulator/environment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from simulator.helper.environment import build_individual_houses_map, build_individual_adult_map, \
build_individual_age_map, build_house_adult_map, build_2d_item_behavior, build_1d_item_behavior, \
build_house_store_map, build_individual_work_map, build_individual_workblock_map, \
build_individual_individual_transport_map, build_individual_death_rate_map, build_individual_hospitalization_map
build_house_store_map, build_individual_work_map, build_individual_workblock_map, build_individual_store_map, \
build_individual_individual_transport_map, build_individual_death_rate_map, build_individual_hospitalization_map, \
build_store_individual_map
from simulator.helper.utils import invert_map_list, invert_map
from tests.utils import g_d

Expand Down Expand Up @@ -88,6 +89,31 @@ def test_build_house_store_map(self):
4: [4, 3, 5],
5: [5, 4, 3]})

def test_build_indiv_store_map(self):
ind_hou = g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2])
hou_sto = g_d([[0, 1, 2], [1, 2, 3], [4, 5, 3]])
result = build_individual_store_map(ind_hou, hou_sto)
self.assertEqual(result, {0: [0, 1, 2],
1: [0, 1, 2],
2: [0, 1, 2],
3: [0, 1, 2],
4: [1, 2, 3],
5: [1, 2, 3],
6: [1, 2, 3],
7: [1, 2, 3],
8: [4, 5, 3],
9: [4, 5, 3]})

def test_build_store_individual_map(self):
ind_sto = {0: [0, 1, 2], 1: [2, 3, 4]}
result = build_store_individual_map(ind_sto)
self.assertEqual(result, {0: [0], 1: [0], 2: [0, 1], 3: [1], 4: [1]})

def test_build_store_individual_map2(self):
ind_sto = {0: [0, 1], 1: [3, 4]}
result = build_store_individual_map(ind_sto)
self.assertEqual(result, {0: [0], 1: [0], 3: [1], 4: [1]})

def test_build_individual_work_map(self):
input_individual_adult_map = {
0: 1, 1: 1, 2: 0, 3: 0,
Expand Down
48 changes: 36 additions & 12 deletions tests/simulator/store_propagation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def test_propagate_to_stores_child(self):
# he can't infect anyone
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [0, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2},
IBE_K: {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
}
Expand All @@ -40,7 +42,9 @@ def test_propagate_to_stores_adult_notcontagious(self):
# but i8 and i9 stay healthy
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [0, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2},
IBE_K: {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
}
Expand All @@ -63,7 +67,9 @@ def test_propagate_to_stores_adult_contagious(self):
# i9 becomes infected
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [0, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2},
IBE_K: {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}
}
Expand All @@ -86,7 +92,9 @@ def test_propagate_to_stores_adult_contagious_bad_gobal_behavior(self):
# They meet and i1 contaminates i9
env_dic = {
HA_K: {0: [1, 0], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [1, 0, 0], 1: [1, 1, 1], 2: [1, 0, 0]},
IS_K: {0: [1, 0, 0], 1: [1, 0, 0], 2: [1, 0, 0], 3: [1, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [1, 0, 0], 9: [1, 0, 0]},
IH_K: {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2},
IBE_K: {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}
}
Expand All @@ -109,7 +117,9 @@ def test_propagate_to_stores_adult_contagious_bad_individual_behavior(self):
# They meet and i1 contaminates i9
env_dic = {
HA_K: {0: [1, 0], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [1, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [1, 0, 0], 1: [1, 0, 0], 2: [1, 0, 0], 3: [1, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2},
IBE_K: {0: 1, 1: 100, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}
}
Expand All @@ -133,7 +143,9 @@ def test_propagate_to_stores_adult_contagious_bad_2individual_behavior(self):
# They meet and i1 contaminates i9
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [1, 0, 0], 1: [1, 1, 1], 2: [1, 0, 0]},
IS_K: {0: [1, 0, 0], 1: [1, 0, 0], 2: [1, 0, 0], 3: [1, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [1, 0, 0], 9: [1, 0, 0]},
IH_K: {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2},
IBE_K: {0: 100, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 100}
}
Expand All @@ -155,7 +167,9 @@ def test_propagate_to_stores_child2(self):
# he can't infect anyone
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [0, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2]),
IBE_K: g_d([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
}
Expand All @@ -178,7 +192,9 @@ def test_propagate_to_stores_adult_notcontagious2(self):
# but i8 and i9 stay healthy
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [0, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2]),
IBE_K: g_d([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
}
Expand All @@ -201,7 +217,9 @@ def test_propagate_to_stores_adult_contagious2(self):
# i9 becomes infected
env_dic = {
HA_K: {0: [0, 1], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [0, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2]),
IBE_K: g_d([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
}
Expand All @@ -224,7 +242,9 @@ def test_propagate_to_stores_adult_contagious_bad_gobal_behavior2(self):
# They meet and i1 contaminates i9
env_dic = {
HA_K: {0: [1, 0], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [1, 0, 0], 1: [1, 1, 1], 2: [1, 0, 0]},
IS_K: {0: [1, 0, 0], 1: [1, 0, 0], 2: [1, 0, 0], 3: [1, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [1, 0, 0], 9: [1, 0, 0]},
IH_K: g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2]),
IBE_K: g_d([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
}
Expand All @@ -247,7 +267,9 @@ def test_propagate_to_stores_adult_contagious_bad_individual_behavior2(self):
# They meet and i1 contaminates i9
env_dic = {
HA_K: {0: [1, 0], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [1, 0, 0], 1: [1, 1, 1], 2: [0, 0, 0]},
IS_K: {0: [1, 0, 0], 1: [1, 0, 0], 2: [1, 0, 0], 3: [1, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [0, 0, 0], 9: [0, 0, 0]},
IH_K: g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2]),
IBE_K: g_d([1, 100, 1, 1, 1, 1, 1, 1, 1, 1])
}
Expand All @@ -272,7 +294,9 @@ def test_propagate_to_stores_adult_contagious_bad_2individual_behavior2(self):
# They meet and i1 contaminates i9
env_dic = {
HA_K: {0: [1, 0], 1: [4, 5], 2: [8, 9]},
HS_K: {0: [1, 0, 0], 1: [1, 1, 1], 2: [1, 0, 0]},
IS_K: {0: [1, 0, 0], 1: [1, 0, 0], 2: [1, 0, 0], 3: [1, 0, 0],
4: [1, 1, 1], 5: [1, 1, 1], 6: [1, 1, 1], 7: [1, 1, 1],
8: [1, 0, 0], 9: [1, 0, 0]},
IH_K: g_d([0, 0, 0, 0, 1, 1, 1, 1, 2, 2]),
IBE_K: g_d([1, 100, 1, 1, 1, 1, 1, 1, 1, 100])
}
Expand Down

0 comments on commit 0356214

Please sign in to comment.