Ejemplo n.º 1
0
    def process_round(self, state: GameState):
        pathogens_without_medication = set(state.pathogens).difference(
            state.pathogens_with_medication)
        pathogens_without_medication = list(
            pathogens_without_medication.difference(
                state.pathogens_with_medication_in_development))
        if pathogens_without_medication:
            # develop all medications first
            if state.points >= 20:
                return develop_medication(
                    pathogens_without_medication[0].index)
            else:
                return end_round()
        else:
            pathogens_with_medication = state.pathogens_with_medication
            if pathogens_with_medication:
                if state.points >= 10:
                    # collect all not medicated outbreaks
                    outbreaks_to_medicate = []
                    for city in state.cities:
                        events = city.events
                        medicated_pathogens = list(
                            map(
                                lambda event: event.pathogen,
                                filter(
                                    lambda event: event.event_type ==
                                    "medicationDeployed", events)))
                        outbreaks = list(
                            filter(
                                lambda event: event.event_type == "outbreak",
                                events))
                        outbreaks = list(
                            filter(
                                lambda outbreak_event: outbreak_event.pathogen
                                in pathogens_with_medication, outbreaks))
                        outbreaks = list(
                            filter(
                                lambda outbreak_event: outbreak_event.pathogen
                                not in medicated_pathogens, outbreaks))
                        outbreaks_to_medicate.extend(
                            list(
                                map(
                                    lambda outbreak_event:
                                    (city, outbreak_event), outbreaks)))

                    if outbreaks_to_medicate:
                        outbreaks_to_medicate.sort(key=self.outbreak_priority,
                                                   reverse=True)
                        outbreak_to_vaccinate = outbreaks_to_medicate[0]
                        return deploy_medication(
                            outbreak_to_vaccinate[1].pathogen.index,
                            outbreak_to_vaccinate[0].city_id)
                    else:
                        return end_round()
                else:
                    return end_round()
            else:
                return end_round()
Ejemplo n.º 2
0
    def process_round(self, state: GameState):
        pathogens_without_vaccine = set(state.pathogens).difference(
            state.pathogens_with_vaccination)
        pathogens_without_vaccine = list(
            pathogens_without_vaccine.difference(
                state.pathogens_with_vaccination_in_development))
        if pathogens_without_vaccine:
            # develop all vaccines first
            if state.points >= 40:
                return develop_vaccine(pathogens_without_vaccine[0].index)
            else:
                return end_round()
        else:
            pathogens_with_vaccine = state.pathogens_with_vaccination
            if pathogens_with_vaccine:
                if state.points >= 5:
                    # collect all not vaccinated outbreaks
                    outbreaks_to_vaccinate = []
                    for city in state.cities:
                        events: list = city.events
                        vaccinated_pathogens = list(
                            map(
                                lambda event: event.pathogen,
                                filter(
                                    lambda event: event.event_type ==
                                    "vaccineDeployed", events)))
                        outbreaks = list(
                            filter(
                                lambda event: event.event_type == "outbreak",
                                events))
                        outbreaks = list(
                            filter(
                                lambda outbreak_event: outbreak_event.pathogen
                                in pathogens_with_vaccine, outbreaks))
                        outbreaks = list(
                            filter(
                                lambda outbreak_event: outbreak_event.pathogen
                                not in vaccinated_pathogens, outbreaks))
                        outbreaks_to_vaccinate.extend(
                            list(
                                map(
                                    lambda outbreak_event:
                                    (city, outbreak_event), outbreaks)))

                    if outbreaks_to_vaccinate:
                        outbreaks_to_vaccinate.sort(key=self.outbreak_priority,
                                                    reverse=True)
                        outbreak_to_vaccinate = outbreaks_to_vaccinate[0]
                        return deploy_vaccine(
                            outbreak_to_vaccinate[1].pathogen.index,
                            outbreak_to_vaccinate[0].index)
                    else:
                        return end_round()
                else:
                    return end_round()
            else:
                return end_round()
Ejemplo n.º 3
0
    def process_round(self, state: GameState):
        if state.points >= 3:
            cities = state.cities
            current_round = state.round
            possible_actions = []

            for city in cities:
                events = city.events

                influence_events = self.get_events_in_current_round(
                    events, "influenceExerted", current_round)
                if not influence_events:
                    influence_priority = self.calculate_priority_random(
                        city.population, city.economy_strength)
                    possible_actions.append(
                        Action(influence_priority,
                               exert_political_influence(city.index)))

                elections_events = self.get_events_in_current_round(
                    events, "electionsCalled", current_round)
                if not elections_events:
                    elections_priority = self.calculate_priority_random(
                        city.population, city.government_stability)
                    possible_actions.append(
                        Action(elections_priority,
                               call_for_elections(city.index)))

                hygienic_events = self.get_events_in_current_round(
                    events, "hygienicMeasuresApplied", current_round)
                if not hygienic_events:
                    hygienic_priority = self.calculate_priority_increase(
                        city.population, city.hygiene_standards)
                    possible_actions.append(
                        Action(hygienic_priority,
                               apply_hygienic_measures(city.index)))

                campaign_events = self.get_events_in_current_round(
                    events, "campaignLaunched", current_round)
                if not campaign_events:
                    campaign_priority = self.calculate_priority_increase(
                        city.population, city.population_awareness)
                    possible_actions.append(
                        Action(campaign_priority, launch_campaign(city.index)))

            if possible_actions:
                possible_actions.sort(key=lambda action: action.effectiveness,
                                      reverse=True)
                return possible_actions[0].action
            else:
                return end_round()
        else:
            return end_round()
Ejemplo n.º 4
0
    def process_round(self, state: GameState):
        possible_actions = []
        points = state.points

        for city in state.cities:
            if points >= 5:
                not_vaccinated_outbreaks = self.get_not_vaccinated_outbreaks_for_city(city)
                actions = list(map(
                    lambda outbreak: self.generate_possible_action(outbreak, city, True,
                                                                   state.pathogens_with_vaccination),
                    not_vaccinated_outbreaks))
                possible_actions.extend(actions)

            if points >= 10:
                not_medicated_outbreaks = self.get_not_medicated_outbreaks_for_city(city)
                actions = list(map(
                    lambda outbreak: self.generate_possible_action(outbreak, city, False,
                                                                   state.pathogens_with_medication),
                    not_medicated_outbreaks))
                possible_actions.extend(actions)

        if possible_actions:
            possible_actions.sort(key=lambda action: action.priority, reverse=True)

            action = possible_actions[0]
            pathogen = action.pathogen
            if action.is_vaccine:
                if pathogen in state.pathogens_with_vaccination:
                    return deploy_vaccine(pathogen.index, action.city_id)
                elif pathogen not in state.pathogens_with_vaccination_in_development:
                    if points >= 40:
                        return develop_vaccine(pathogen.index)
                    else:
                        return end_round()
                else:
                    return self.generate_deployment_action(possible_actions, state)
            else:
                if pathogen in state.pathogens_with_medication:
                    return deploy_medication(pathogen.index, action.city_id)
                elif pathogen not in state.pathogens_with_medication_in_development:
                    if points >= 20:
                        return develop_medication(pathogen.index)
                    else:
                        return end_round()
                else:
                    return self.generate_deployment_action(possible_actions, state)
        else:
            return end_round()
Ejemplo n.º 5
0
        def handle_round_outcome(self, state: GameState):
            if state.outcome == 'pending':
                action, action_penalty = external_env.get_action(
                    self._controller.eid, state)
                invalid_action_taken = False
                if action == INVALID_ACTION:
                    invalid_action_taken = True
                    action = actions.end_round()
                self.update_controller(state, action_penalty,
                                       invalid_action_taken)
                print(action.json)
                return action.json

            elif state.outcome == 'loss':
                external_env.end_episode(self._controller.eid, state)
                self._controller.new_eid()
                self._controller.is_first_round = True
                return END_EPISODE_RESPONSE

            elif state.outcome == 'win':
                external_env.end_episode(self._controller.eid, state)
                self._controller.new_eid()
                self._controller.is_first_round = True
                return END_EPISODE_RESPONSE

            else:
                raise Exception(f"Unknown outcome: {state.outcome}")
Ejemplo n.º 6
0
 def generate_deployment_action(self, possible_actions: list, state: GameState):
     executable_actions = list(filter(lambda action: self.is_action_executable(action, state), possible_actions))
     if executable_actions:
         action = executable_actions[0]
         if action.is_vaccine:
             return deploy_vaccine(action.pathogen.index, action.city_id)
         else:
             return deploy_medication(action.pathogen.index, action.city_id)
     else:
         return end_round()
    def _choose_actionable_action(self, state: GameState) -> Action:
        processed_state = self.obs_state_processor.preprocess_obs(state)
        mapped_action = INVALID_ACTION

        trial_count = 0
        while (mapped_action == INVALID_ACTION or mapped_action.cost > state.points) \
                or mapped_action not in actions.generate_possible_actions(state):
            action = self.trainer.compute_action(observation=processed_state)
            mapped_action, _ = self.act_state_processor.map_action(
                action, state)

            trial_count += 1
            if trial_count >= self.trial_max:
                mapped_action = actions.end_round()
                break

        return mapped_action
Ejemplo n.º 8
0
def index():
    game_json = request.json
    state = state_from_json(game_json)
    print(
        f'round: {state.round}, points: {state.points}, outcome: {state.outcome}'
    )
    if "error" in game_json:
        print(game_json["error"])
    if state.outcome == 'pending':
        action = process_round(state)
        print(action.json)
        log_to_file(action.json)
        log_to_file("")
        return action.json
    else:
        process_game_end(state)
        return end_round()
    def _map_global_actions(self, chosen_action: int,
                            game_state: GameState) -> Optional[Action]:
        assert chosen_action < GLOBAL_ACTIONSPACE
        options = {0: actions.end_round()}
        basic_options_len = len(options)
        if chosen_action < basic_options_len:
            mapped_action = options.get(chosen_action)
        else:
            ordered_available_pathogens = self.sort_pathogens(
                game_state.pathogens, game_state)
            pathogens_with_vaccine = [
                *game_state.pathogens_with_vaccination,
                *game_state.pathogens_with_vaccination_in_development
            ]
            pathogens_with_meds = [
                *game_state.pathogens_with_medication,
                *game_state.pathogens_with_medication_in_development
            ]

            vaccine_actions = {
                i: action
                for i, action in enumerate(
                    self._generate_global_vaccine_actions(
                        ordered_available_pathogens, pathogens_with_vaccine),
                    start=basic_options_len)
            }

            medication_actions = {
                i: action
                for i, action in enumerate(self._generate_global_med_actions(
                    ordered_available_pathogens, pathogens_with_meds),
                                           start=basic_options_len +
                                           MAX_PATHOGENS)
            }

            options.update(vaccine_actions)
            options.update(medication_actions)
            mapped_action = options.get(chosen_action)

        return mapped_action
Ejemplo n.º 10
0
 def process_round(self, state: GameState):
     return end_round()
Ejemplo n.º 11
0
    def process_round(self, state: GameState):
        cities = state.cities
        available_points = state.points
        possible_actions = []

        for city in cities:
            priority_for_city = 0
            pathogens_in_city = city.pathogens
            connected_cities = list(
                map(
                    lambda connection_name: self.get_city_for_name(
                        cities, connection_name),
                    self.get_open_connections_for_city(city)))
            airport_open = not city.airport_closed

            for other_city in connected_cities:
                priority_for_connection = 0
                pathogens_in_other_city = other_city.pathogens

                for pathogen_in_city in set(pathogens_in_city).difference(
                        pathogens_in_other_city):
                    priority_for_connection += self.calculate_priority_for_connection(
                        city, other_city, pathogen_in_city)

                # the closing of a connection, unlike the closing of an airport, is not bidirectional
                rounds = 1
                while available_points >= self.cost_for_connection_close(
                        rounds) and rounds <= 6:
                    effectiveness = priority_for_connection * rounds / self.cost_for_connection_close(
                        rounds)
                    action = close_airway(city.index, other_city.index, rounds)
                    possible_actions.append(Action(effectiveness, action))
                    rounds += 1

            if airport_open:
                for other_city in map(
                        lambda city_name: self.get_city_for_name(
                            cities, city_name), city.connections):
                    priority_for_connection = 0
                    pathogens_in_other_city = other_city.pathogens

                    for pathogen_in_other_city in set(
                            pathogens_in_other_city).difference(
                                pathogens_in_city):
                        priority_for_connection += self.calculate_priority_for_connection(
                            other_city, city, pathogen_in_other_city)

                    priority_for_city += priority_for_connection

                rounds = 1
                while available_points >= self.cost_for_airport_close(rounds):
                    effectiveness = priority_for_city * rounds / self.cost_for_airport_close(
                        rounds)
                    action = close_airport(city.index, rounds)
                    possible_actions.append(Action(effectiveness, action))
                    rounds += 1

        if possible_actions:
            possible_actions.sort(key=lambda action: action.effectiveness,
                                  reverse=True)
            chosen_action = possible_actions[0]
            if chosen_action.effectiveness > 0:
                return chosen_action.action
            else:
                return end_round()
        else:
            return end_round()