Esempio n. 1
0
 def successors(self, state):
     possible_actions = SeegaRules.get_player_actions(
         state, self.color.value)
     for action in possible_actions:
         next_state, done = SeegaRules.make_move(deepcopy(state), action,
                                                 self.color.value)
         yield action, next_state
Esempio n. 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
Esempio n. 3
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
Esempio n. 4
0
 def successors(self, state):
     possible_actions = SeegaRules.get_player_actions(
         state, self.color.value)
     print("POSSIBLE MOVES :")
     for i, action in enumerate(possible_actions):
         print(i, action)
     move = int(input("SELECTED MOVE ?"))
     next_state, done = SeegaRules.make_move(deepcopy(state),
                                             possible_actions[move],
                                             self.color.value)
     print(f"SELECTION : {move} - {possible_actions[move]}\n{next_state}")
     yield possible_actions[move], next_state
Esempio n. 5
0
    def successors(self, state: SeegaState):
        """
        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.
        """
        if state in self.cache_successors:
            self.cache_successors['hits'] += 1
            return self.cache_successors[state]

        next_player = state.get_next_player()
        possible_actions = SeegaRules.get_player_actions(state, next_player)
        succ = []
        for action in possible_actions:
            next_state, done = SeegaRules.make_move(deepcopy(state), action,
                                                    next_player)
            succ.append((action, next_state))

        self.cache_successors['misses'] += 1
        self.cache_successors[state] = succ
        return succ
Esempio n. 6
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
Esempio n. 7
0
    def play(self, state, remaining_time):
        self.move_nb += 1
        state.__class__ = State
        print(
            f"\nPlayer {self.ME} is playing with {remaining_time} seconds remaining for move #{self.move_nb}"
        )
        print(f"CacheInfo : "
              f"hits={self.cache_successors['hits']}, "
              f"misses={self.cache_successors['misses']}, "
              f"currsize={len(self.cache_successors) - 2}")
        print(f"{state} evaluation={self.evaluate(state):.2f}\n")

        # TODO remove obsolete since stuck player fix
        # if self.repeat_boring_moves:  # fast-forward to save time
        #     assert state.get_latest_player() == self.ME, \
        #         " - ERROR : May not repeat boring moves, latest player isn't self"
        #     print(" - PLAYING BOREDOM")
        #     return self.reverse_last_move(state)

        if self.max_time is None:
            self.max_time = remaining_time
            self.typical_time = remaining_time / self.max_nb_moves
        self.remaining_time = remaining_time

        possible_actions = SeegaRules.get_player_actions(
            state, self.color.value)
        if len(possible_actions) == 1:
            best_action = possible_actions[0]
        elif state.phase == 1:
            best_action = SeegaRules.random_play(
                state, self.ME)  # TODO play smart during phase 1
        else:  # phase == 2
            # TODO remove obsolete since stuck player fix
            # if self.can_start_self_play(state):
            #     best_action = self.make_self_play_move(state, fallback_function=self.iterative_deepening)
            best_action = self.iterative_deepening(state)

        print(f" - SELECTED ACTION : {best_action}")
        self.last_action = best_action
        return best_action
    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