def generate_events(self, world: World): assert self.compliance <= 1 all_people = [p for p in world.all_people() if p.get_age() > self.min_age] cnt_to_Immune = int(self.compliance * len(all_people)) people_to_immune = random.sample(all_people,cnt_to_Immune) ret = [] group_index = 0 while cnt_to_Immune > 0: for i in range(min(self.people_per_day,cnt_to_Immune)): person_index = group_index * self.people_per_day + i p = people_to_immune[person_index] ok , events_to_add = p.immune_and_get_events(start_date = self.start_date, delta_time = timedelta(group_index)) # print("id:{} cnt:{}".format(p.get_id(),len(events_to_add))) ret += events_to_add cnt_to_Immune = cnt_to_Immune - 1 group_index = group_index + 1 if self.duration.days < group_index: break return ret
def generate_events(self, world: World): """ generated events that add the relevent routine change to the people that are relevant for this intervention, and events that remove it after that given duration. :param world: World object :return: list of Event objects """ new_events = [] for person in world.all_people(): if self._condition(person): if random.random() < self.compliance: curr_events = make_routine_change_events( person, self.start_date, self.end_date, self._key, self._routine_generator, self._args ) for event in curr_events: new_events.append(event) return new_events
def generate_events(self, world: World): """ generate events that are applied on the person when his state changes to symptomatic, and add the isolation routine to the whole house. After the given duration and params, an event will remove the change. :param world: World object :return: list of new Events to register on the simulation """ ret = [] for person in world.all_people(): if random.random() < self.compliance: household_environment = person.get_environment('household') add_effect = AddRoutineChangeEnvironmentEffect( environment=household_environment, routine_change_key='household_isolation', routine_change_generator=household_isolation_routine ) states = ( DiseaseState.INCUBATINGPOSTLATENT, DiseaseState.SYMPTOMATICINFECTIOUS ) person._init_event(*states) entry_moment = Event() add_trigger = AndTrigger( AfterTrigger(entry_moment), TimeRangeTrigger(self.start_date, self.end_date) ) add_event = Event( add_trigger, add_effect ) entry_moment.hook(add_event) day_event = DayEvent(self.start_date) # Wasteful in memory! day_event.hook(add_event) ret.append(day_event) if self.delay_on_enter: delay_time = self.delay_on_enter person.hook_on_change( states, Event( EmptyTrigger(), DelayedEffect(entry_moment, delay_time) ) ) else: person.hook_on_change(states, entry_moment) remove_effect = RemoveRoutineChangeEnvironmentEffect( environment=household_environment, routine_change_key='household_isolation' ) if self.is_exit_after_recovery: state_changes = list(itertools.product( [DiseaseState.SYMPTOMATICINFECTIOUS, DiseaseState.CRITICAL], [DiseaseState.IMMUNE, DiseaseState.DECEASED] )) for states in state_changes: person._init_event(*states) remove_trigger = AndTrigger( OrTrigger([ AfterTrigger(person.state_to_events[states]) for states in state_changes ]), AfterTrigger(add_event) ) remove_event = Event( remove_trigger, DelayedEffect(Event(effect=remove_effect), self.delay_on_exit) ) for states in state_changes: person.hook_on_change(states, remove_event) else: remove_trigger = AfterTrigger(add_event) remove_event = Event( remove_trigger, DelayedEffect(Event(effect=remove_effect), self.delay_on_exit) ) add_event.hook(remove_event) return ret