Exemple #1
0
    def act(self, state):
        model = state_to_model(state)
        own_id = state["you"]
        _, _, is_endgame, min_player_ids = voronoi(model, own_id)
        if own_id in min_player_ids:
            min_player_ids.remove(own_id)
        if not is_endgame and len(min_player_ids) > 1:
            pos = model.get_agent_by_id(own_id).pos
            opponent_pos = model.get_agent_by_id(min_player_ids[0]).pos
            distance_to_next_opponent = distance.euclidean(pos, opponent_pos)
            state = reduce_state_to_sliding_window(
                state,
                distance_to_next_opponent,
                min_sliding_window_size=self.min_sliding_window_size,
                sliding_window_size_offset=self.sliding_window_size_offset)

        move = multiprocessing.Value('i', 4)
        reached_depth = multiprocessing.Value('i', 0)
        p = multiprocessing.Process(
            target=self.depth_first_iterative_deepening,
            name="DFID",
            args=(move, reached_depth, state))
        p.start()
        p.join(self.time_for_move)

        # Force termination
        if p.is_alive():
            p.terminate()
            p.join()

        return Action(move.value)
Exemple #2
0
 def voronoi_region_sizes(model, max_player, min_player):
     voronoi_cells, voronoi_counter, _, _ = voronoi(model,
                                                    max_player.unique_id)
     max_player_size = voronoi_counter[
         max_player.
         unique_id] if max_player.unique_id in voronoi_counter.keys() else 0
     min_player_size = voronoi_counter[
         min_player.
         unique_id] if min_player.unique_id in voronoi_counter.keys() else 0
     return voronoi_cells, max_player_size, min_player_size
Exemple #3
0
    def init_multi_minimax(self, game_state):
        game_state["step"] = self.game_step
        model = state_to_model(game_state)
        own_id = game_state["you"]
        _, _, is_endgame, _ = voronoi(model, own_id)
        max_player = model.get_agent_by_id(own_id)

        min_player_ids = list(
            map(lambda a: a.unique_id, model.active_speed_agents))
        min_player_ids.remove(own_id)

        move_to_make = Action.CHANGE_NOTHING
        max_move = float("-inf")
        alpha = float("-inf")
        actions = self.init_actions()
        return model, max_player, min_player_ids, is_endgame, move_to_make, max_move, alpha, actions
Exemple #4
0
    def act(self, state):
        self.reached_depth = (False, 0)
        model = state_to_model(state)
        own_id = state["you"]
        _, _, is_endgame, min_player_ids = voronoi(model, own_id)
        if own_id in min_player_ids:
            min_player_ids.remove(own_id)
        self.is_endgame = is_endgame
        if not is_endgame and len(min_player_ids) > 1:
            pos = model.get_agent_by_id(own_id).pos
            opponent_pos = model.get_agent_by_id(min_player_ids[0]).pos
            distance_to_next_opponent = distance.euclidean(pos, opponent_pos)
            state = reduce_state_to_sliding_window(
                state,
                distance_to_next_opponent,
                min_sliding_window_size=self.min_sliding_window_size,
                sliding_window_size_offset=self.sliding_window_size_offset)

        self.depth_first_iterative_deepening(state)
        return Action(self.move_to_make)
Exemple #5
0
    def act(self, state):
        model = state_to_model(state)
        own_id = state["you"]
        _, _, is_endgame, min_player_ids = voronoi(model, own_id)
        if own_id in min_player_ids:
            min_player_ids.remove(own_id)
        if not is_endgame:
            pos = model.get_agent_by_id(own_id).pos
            opponent_pos = model.get_agent_by_id(min_player_ids[0]).pos
            distance_to_next_opponent = distance.euclidean(pos, opponent_pos)
            state = reduce_state_to_sliding_window(
                state,
                distance_to_next_opponent,
                min_sliding_window_size=self.min_sliding_window_size,
                sliding_window_size_offset=self.sliding_window_size_offset)

        move = multiprocessing.Value('i', 4)
        reached_depth = multiprocessing.Value('i', 0)
        p = multiprocessing.Process(
            target=self.depth_first_iterative_deepening,
            name="DFID",
            args=(move, reached_depth, state))
        p.start()
        send_time = 1
        deadline = datetime.datetime.strptime(state["deadline"],
                                              "%Y-%m-%dT%H:%M:%SZ")
        response = requests.get(self.server_time_url)
        server_time = datetime.datetime.strptime(response.json()["time"],
                                                 "%Y-%m-%dT%H:%M:%SZ")
        av_time = (deadline - server_time).total_seconds() - send_time
        p.join(av_time)

        # If thread is active
        if p.is_alive():
            # Terminate foo
            p.terminate()
            p.join()

        self.game_step += 1
        return Action(move.value)