Beispiel #1
0
class Player(ABC):
    def __init__(self,
                 game,
                 arg_file: typing.Optional[str] = None,
                 name: str = "",
                 parametric: bool = False) -> None:
        self.game = game
        self.player_args = arg_file
        self.parametric = parametric
        self.histories = list()
        self.history = GameHistory()
        self.name = name

    def bind_history(self, history: GameHistory) -> None:
        self.history = history

    def refresh(self, hard_reset: bool = False) -> None:
        if hard_reset:
            self.histories = list()
            self.history.refresh()
        else:
            self.histories.append(self.history)
            self.history = GameHistory()

    def observe(self, state: GameState) -> None:
        self.history.capture(state, np.array([]), 0, 0)

    def clone(self):
        return self.__class__(self.game, self.player_args)

    @abstractmethod
    def act(self, state: GameState) -> int:
        """
Beispiel #2
0
    def test_observation_stacking(self):
        # random normal variables in the form (x, y, c)
        shape = (3, 3, 8)
        dummy_observations = [
            np.random.randn(np.prod(shape)).reshape(shape) for _ in range(10)
        ]

        h = GameHistory()
        h.capture(dummy_observations[0], -1, 1, np.array([]), 0, 0)

        # Ensure correct shapes and content
        stacked_0 = h.stackObservations(0)
        stacked_1 = h.stackObservations(1)
        stacked_5 = h.stackObservations(5)

        np.testing.assert_array_equal(stacked_0.shape,
                                      shape)  # Shape didn't change
        np.testing.assert_array_equal(stacked_1.shape,
                                      shape)  # Shape didn't change
        np.testing.assert_array_equal(stacked_5.shape,
                                      np.array(shape) *
                                      np.array([1, 1, 5]))  # Channels * 5

        np.testing.assert_array_almost_equal(
            stacked_0, dummy_observations[0])  # Should be the same
        np.testing.assert_array_almost_equal(
            stacked_1, dummy_observations[0])  # Should be the same

        np.testing.assert_array_almost_equal(stacked_5[..., :-8],
                                             0)  # Should be only 0s
        np.testing.assert_array_almost_equal(
            stacked_5[...,
                      -8:], dummy_observations[0])  # Should be the first o_t

        # Check whether observation concatenation works correctly
        stacked = h.stackObservations(2, dummy_observations[1])
        expected = np.concatenate(dummy_observations[:2], axis=-1)
        np.testing.assert_array_almost_equal(stacked, expected)

        # Fill the buffer
        all([
            h.capture(x, -1, 1, np.array([]), 0, 0)
            for x in dummy_observations[1:]
        ])

        # Check whether time indexing works correctly
        stacked_1to5 = h.stackObservations(4, t=4)  # 1-4 --> t is inclusive
        stacked_last4 = h.stackObservations(4, t=9)  # 6-9
        expected_1to5 = np.concatenate(dummy_observations[1:5],
                                       axis=-1)  # t in {1, 2, 3, 4}
        expected_last4 = np.concatenate(dummy_observations[-4:],
                                        axis=-1)  # t in {6, 7, 8, 9}

        np.testing.assert_array_almost_equal(stacked_1to5, expected_1to5)
        np.testing.assert_array_almost_equal(stacked_last4, expected_last4)

        # Check if clearing works correctly
        h.refresh()
        self.assertEqual(len(h), 0)
Beispiel #3
0
class Player(ABC):
    """ Interface for players for general environment control/ game playing. """
    def __init__(self,
                 game,
                 arg_file: typing.Optional[str] = None,
                 name: str = "",
                 parametric: bool = False) -> None:
        """
        Initialization of the Base Player object.
        :param game: Game Instance of Games.Game that implements environment logic.
        :param arg_file: str Path to JSON configuration file for the agent/ player.
        :param name: str Name to annotate this player with (useful during tournaments)
        :param parametric: bool Whether the agent depends on parameters or is parameter-free.
        """
        self.game = game
        self.player_args = arg_file
        self.parametric = parametric
        self.histories = list()
        self.history = GameHistory()
        self.name = name

    def bind_history(self, history: GameHistory) -> None:
        """ Bind an external memory object for keeping track of environment observations. """
        self.history = history

    def refresh(self, hard_reset: bool = False) -> None:
        """ Refresh or reinitialize memory/ observation trajectory of the agent. """
        if hard_reset:
            self.histories = list()
            self.history.refresh()
        else:
            self.histories.append(self.history)
            self.history = GameHistory()

    def observe(self, state: GameState) -> None:
        """ Capture an environment state observation within the agent's memory. """
        self.history.capture(state, np.array([]), 0, 0)

    def clone(self):
        """ Create a new instance of this Player object using equivalent parameterization """
        return self.__class__(self.game, self.player_args)

    @abstractmethod
    def act(self, state: GameState) -> int:
        """