def do_tick(self):
        """
        Performs a tick of the simulation
        :return Log line
        """

        notifications.clear_received()
        last_rounds_notifications = notifications.get_last()
        notifications.clear()
        self.turn += 1
        actions = [
            actor.tick_action(last_rounds_notifications)
            for actor in self.actors
        ]
        for action in actions:
            action()

        number_notifications = len(last_rounds_notifications.get())
        active_interventions = len(CityMap.get_interventions())
        self._csv_log([
            self.turn, number_notifications, notifications.received,
            active_interventions, CityMap.moves,
            len(self.get_ambulances()),
            len(self.get_policemen()), CityMap.ambulance_moves,
            CityMap.policeman_moves,
            len(self.intervening_policemen()),
            len(self.patroling_policemen()),
            len(self.assisting_ambulances())
        ])

        CityMap.clear_moves()
Exemplo n.º 2
0
def insert_random_intervention(sim_manager):
    from smart_intervention.globals import CityMap
    from smart_intervention.events.intervention_event import InterventionEvent

    def generate_random_intervention():
        locations = CityMap.get_locations()
        forbidden_locations = [
            *sim_manager.police_outposts, sim_manager.ambulance_hq
        ]
        free_locations = [
            location for location in locations
            if not location.intervention_event
            and location not in forbidden_locations
        ]
        if not free_locations:
            return
        danger = round(random(), 2)
        health_index = random() * 10
        location = pick_random(free_locations)
        location.intervention_event = InterventionEvent(
            danger, health_index, location)
        print(
            f'Generated event: Location: {id(location)}, Health: {health_index}, Danger: {danger}',
            flush=True)

    active_interventions = len(CityMap.get_interventions())
    if active_interventions >= 2:
        return
    elif active_interventions == 1 and random_decision(0.2):
        generate_random_intervention()
    elif active_interventions == 0 and random_decision(0.4):
        generate_random_intervention()
Exemplo n.º 3
0
 def action():
     processable_len = len(processable_notifications.get())
     self.log.debug(f'Received {processable_len} processable notifications')
     notifications.declare_received(processable_len)
     ManagementCenterNotificationProcessor(self).process(processable_notifications)
     interventions = CityMap.get_interventions()
     not_gunfight_interventions = [
         intervention for intervention in interventions
         if not intervention.armed_combat and intervention.active
     ]
     not_gunfight_interventions.sort(
         key=lambda intervention: len(self._resource_monitor.get_dispatched_and_intervening(intervention))
     )
     self._process_interventions(not_gunfight_interventions)