예제 #1
0
    def successors(self, state):
        """successors.
        The successors function must return (or yield) a list of pairs (a, s) in which a is the action played to reach the state s.

        :param state: the state for which we want the successors
        """
        for action in SeegaRules.get_player_actions(state, self.position):
            next_state = deepcopy(state)
            SeegaRules.act(next_state, action, self.position)
            yield action, next_state
예제 #2
0
    def successors(self, state):
        """successors.
        The successors function must return (or yield) a list of pairs (a, s) in which a is the action played to reach the state s.

        :param state: the state for which we want the successors
        """
        next_player = state.get_next_player()
        is_our_turn = next_player == self.position
        successors = list()

        if not is_our_turn and self._already_tracked(state):
            return list()

        for action in SeegaRules.get_player_actions(state, next_player):
            next_state = deepcopy(state)
            if SeegaRules.act(next_state, action, next_player):
                successors.append((action, next_state))

        # Sort states with their evaulation values (reverse : min/max)
        successors.sort(key=lambda x: self.evaluate(x[1]),
                        reverse=not is_our_turn)
        # logging.info(f'Next states for {["oponent", "us"][is_our_turn]} -> {successors}')

        # Get all not already tracked states if loosing and is our turn
        if is_our_turn and self._alpha_winning(state) < 0.5:
            not_tracked = list(
                filter(lambda elem: not self._already_tracked(elem[1]),
                       successors))
            if not_tracked:
                successors = not_tracked

        return successors
예제 #3
0
    def play(self, state, remain_time):
        print("")
        print(f"Player {self.position} is playing.")
        print("time remain is ", remain_time, " seconds")

        if self._total_time is None:
            self._total_time = remain_time
        self._remaining_time = remain_time

        def time_policy(policy="exponential",
                        min_frac=1 / 2000,
                        max_frac=1 / 100):
            """time_policy.

            :param policy: the desired policy between ('linear', 'exponential')
            :param min_frac: the minimum fraction of total time allowed for computing time
            :param max_frac: the maximum fraction of total time allowed for computing time
            """
            min, max = self._total_time * min_frac, self._total_time * max_frac

            # Calculate the linear and exponential policy
            logging.info(f"alpha time : {self._alpha_time}")
            schedulers = dict(
                linear=min + self._alpha_time * (max - min),
                exponential=min +
                (np.exp(self._alpha_time * math.log(2)) - 1) * (max - min),
            )

            return schedulers[policy]

        self._max_running_time = time_policy("exponential")
        print(self._max_running_time)

        # Begining the search
        self._start_minimax = perf_counter()
        best_action = self.iterative_deepening(state)

        tracked_state = deepcopy(state)
        SeegaRules.act(tracked_state, best_action, self.position)
        self._track_state(tracked_state)

        return best_action
예제 #4
0
    def successors(self, state):
        # #player = state._next_player
        # actions = SeegaRules.get_player_actions(state, self.color.value)
        # SeegaRules.act(s, a, self.color.value)

        next_player = state._next_player
        actions = SeegaRules.get_player_actions(state, next_player)
        successors = list()

        for a in actions:
            s = deepcopy(state)
            possible_states = SeegaRules.act(s, a, next_player)
            if possible_states:
                successors.append((a, possible_states[0]))

        if state.phase == 2:
            successors.sort(key=lambda t: self.evaluate(t[1]),
                            reverse=next_player != self.position)

        return successors
예제 #5
0
    def successors(self, state):
        """successors.
		The successors function must return (or yield) a list of pairs (a, s) in which a is the action played to reach the state s.

		:param state: the state for which we want the successors
		"""
        next_player = state.get_next_player()
        is_our_turn = next_player == self.position
        successors = list()

        if not is_our_turn and self._already_tracked(state):
            return list()

        actions = SeegaRules.get_player_actions(state, next_player)
        if state.phase == 2:
            nn_actions = self._agent._state_to_actions(state,
                                                       reverse=not is_our_turn)
            actions = list(
                filter(
                    lambda x: any([self._action_eq(x, a) for a in actions]),
                    nn_actions,
                ))

        for action in actions:
            next_state = deepcopy(state)
            if SeegaRules.act(next_state, action, next_player):
                successors.append((action, next_state))

        # Get all not already tracked states if loosing and is our turn
        if is_our_turn and self._alpha_winning(state) < 0.5:
            not_tracked = list(
                filter(lambda elem: not self._already_tracked(elem[1]),
                       successors))
            if not_tracked:
                successors = not_tracked

        return successors