示例#1
0

def _line_value(line):
    """Checks a possible line, returning the winning symbol if any."""
    if all(line == "x") or all(line == "o"):
        return line[0]


def _line_exists(board):
    """Checks if a line exists, returns "x" or "o" if so, and None otherwise."""
    return (_line_value(board[0]) or _line_value(board[1])
            or _line_value(board[2]) or _line_value(board[:, 0])
            or _line_value(board[:, 1]) or _line_value(board[:, 2])
            or _line_value(board.diagonal())
            or _line_value(np.fliplr(board).diagonal()))


def _coord(move):
    """Returns (row, col) from an action id."""
    return (move // _NUM_COLS, move % _NUM_COLS)


def _board_to_string(board):
    """Returns a string representation of the board."""
    return "\n".join("".join(row) for row in board)


# Register the game with the OpenSpiel library

pyspiel.register_game(_GAME_TYPE, TicTacToeGame)
        return (f"p0:{self.action_history_string(0)} "
                f"p1:{self.action_history_string(1)}")

    def action_history_string(self, player):
        return "".join(
            self._action_to_string(pa.player, pa.action)[0]
            for pa in self.full_history() if pa.player == player)


class IteratedPrisonersDilemmaObserver:
    """Observer, conforming to the PyObserver interface (see observation.py)."""
    def __init__(self, iig_obs_type, params):
        """Initializes an empty observation tensor."""
        assert not bool(params)
        self.iig_obs_type = iig_obs_type
        self.tensor = None
        self.dict = {}

    def set_from(self, state, player):
        pass

    def string_from(self, state, player):
        """Observation of `state` from the PoV of `player`, as a string."""
        return (f"us:{state.action_history_string(player)} "
                f"op:{state.action_history_string(1 - player)}")


# Register the game with the OpenSpiel library

pyspiel.register_game(_GAME_TYPE, IteratedPrisonersDilemmaGame)
示例#3
0
            "y": self.tensor[self.size:self.size * 2],
            "t": self.tensor[self.size * 2:]
        }

    def set_from(self, state: MFGPredatorPreyState, player: int):
        """Updates `tensor` and `dict` to reflect `state` from PoV of `player`."""
        del player
        # We update the observation via the shaped tensor since indexing is more
        # convenient than with the 1-D tensor. Both are views onto the same memory.
        self.tensor.fill(0)
        # state.pos is None for the initial (blank) state, don't set any
        # position bit in that case.
        if state.pos is not None:
            if not (state.pos >= 0).all() or not (state.pos < self.size).all():
                raise ValueError(
                    f"Expected {state} positions to be in [0, {self.size})")
            self.dict["x"][state.pos[0]] = 1
            self.dict["y"][state.pos[1]] = 1
        if not 0 <= state.t <= self.horizon:
            raise ValueError(
                f"Expected {state} time to be in [0, {self.horizon}]")
        self.dict["t"][state.t] = 1

    def string_from(self, state, player):
        """Observation of `state` from the PoV of `player`, as a string."""
        del player
        return str(state)


pyspiel.register_game(_GAME_TYPE, MFGPredatorPreyGame)
示例#4
0
    tensor: list of location for each time step.
  """

  def __init__(self, num_vehicles: int, num_time: int):
    """Initializes an empty observation tensor."""
    shape = (num_time + 1, num_vehicles + 1)
    self.tensor = np.zeros(np.prod(shape), np.float32)
    self.dict = {"observation": np.reshape(self.tensor, shape)}

  def set_from(self, state, player):
    """Update the state tensor.

    Put the locations of each players in the tensor row corresponding to
    the current time step. Insert the current player location at the
    beginning of the row.
    Args:
      state: the state,
      player: the player.
    """
    vehicles = state.get_current_vehicle_locations_as_int()
    vehicles.insert(0, state.get_location_as_int(player))
    self.dict["observation"][state.current_time_step, :] = vehicles

  def string_from(self, state, player):
    """Return the state history string."""
    return f"{player}: {state.history_str()}"


# Register the game with the OpenSpiel library
pyspiel.register_game(_GAME_TYPE, DynamicRoutingGame)
示例#5
0
    dict: dictionary {"observation": tensor}.
    tensor: list of location for each time step.
  """
    def __init__(self, num_time: int):
        """Initializes an empty observation tensor."""
        self.tensor = np.zeros(num_time + 1, np.float32)
        self.dict = {"observation": self.tensor}

    def set_from(self, state, player):
        """Update the state tensor.

    Put the locations of each players in the tensor row corresponding to
    the current time step. Insert the current player location at the
    beginning of the row.
    Args:
      state: state of the game.
      player: player id that should play.
    """
        assert player == pyspiel.PlayerId.DEFAULT_PLAYER_ID
        self.dict["observation"][
            state.current_time_step] = state.get_location_as_int()

    def string_from(self, state, player):
        """Return the state history string."""
        assert player == pyspiel.PlayerId.DEFAULT_PLAYER_ID
        return str(state)


# Register the game with the OpenSpiel library
pyspiel.register_game(_GAME_TYPE, MeanFieldRoutingGame)
示例#6
0
    def set_from(self, state, player):
        """Updates `tensor` and `dict` to reflect `state` from PoV of `player`."""
        self.tensor.fill(0)
        if "player" in self.dict:
            self.dict["player"][player] = 1
        if "private_card" in self.dict and len(state.cards) > player:
            self.dict["private_card"][state.cards[player]] = 1
        if "pot_contribution" in self.dict:
            self.dict["pot_contribution"][:] = state.pot
        if "betting" in self.dict:
            for turn, action in enumerate(state.bets):
                self.dict["betting"][turn, action] = 1

    def string_from(self, state, player):
        """Observation of `state` from the PoV of `player`, as a string."""
        pieces = []
        if "player" in self.dict:
            pieces.append(f"p{player}")
        if "private_card" in self.dict and len(state.cards) > player:
            pieces.append(f"card:{state.cards[player]}")
        if "pot_contribution" in self.dict:
            pieces.append(f"pot[{int(state.pot[0])} {int(state.pot[1])}]")
        if "betting" in self.dict and state.bets:
            pieces.append("".join("pb"[b] for b in state.bets))
        return " ".join(str(p) for p in pieces)


# Register the game with the OpenSpiel library

pyspiel.register_game(_GAME_TYPE, KuhnPokerGame)
示例#7
0
            "t": self.tensor[self.size:]
        }

    def set_from(self, state: MFGCrowdModellingState, player: int):
        """Updates `tensor` and `dict` to reflect `state` from PoV of `player`."""
        del player
        # We update the observation via the shaped tensor since indexing is more
        # convenient than with the 1-D tensor. Both are views onto the same memory.
        self.tensor.fill(0)
        # state.x is None for the initial (blank) state, don't set any
        # position bit in that case.
        if state.x is not None:
            if not 0 <= state.x < self.size:
                raise ValueError(
                    f"Expected {state} x position to be in [0, {self.size})")
            self.dict["x"][state.x] = 1
        if not 0 <= state.t <= self.horizon:
            raise ValueError(
                f"Expected {state} time to be in [0, {self.horizon}]")
        self.dict["t"][state.t] = 1

    def string_from(self, state, player):
        """Observation of `state` from the PoV of `player`, as a string."""
        del player
        return str(state)


# Register the game with the OpenSpiel library

pyspiel.register_game(_GAME_TYPE, MFGCrowdModellingGame)