def test_extensive_to_tensor_game_payoff_tensor(self):
     turn_based_game = pyspiel.load_game_as_turn_based(
         "blotto(players=3,coins=5)")
     tensor_game1 = pyspiel.extensive_to_tensor_game(turn_based_game)
     tensor_game2 = pyspiel.load_tensor_game("blotto(players=3,coins=5)")
     self.assertEqual(tensor_game1.shape(), tensor_game2.shape())
     s0 = turn_based_game.new_initial_state()
     self.assertEqual(tensor_game1.shape()[0], s0.num_distinct_actions())
     for a0 in range(s0.num_distinct_actions()):
         s1 = s0.child(a0)
         self.assertEqual(tensor_game1.shape()[1],
                          s1.num_distinct_actions())
         for a1 in range(s1.num_distinct_actions()):
             s2 = s1.child(a1)
             self.assertEqual(tensor_game1.shape()[2],
                              s2.num_distinct_actions())
             for a2 in range(s2.num_distinct_actions()):
                 s3 = s2.child(a2)
                 self.assertTrue(s3.is_terminal())
                 for player in range(3):
                     self.assertEqual(
                         s3.returns()[player],
                         tensor_game1.player_utility(player, (a0, a1, a2)))
                     self.assertEqual(
                         s3.returns()[player],
                         tensor_game2.player_utility(player, (a0, a1, a2)))
Exemplo n.º 2
0
def game_payoffs_array(game):
    if isinstance(game, mg):
        return np.stack([game.row_utilities(), game.col_utilities()])

    if not isinstance(game, tg):
        game = extensive_to_tensor_game(game)
        return np.stack([
            game.player_utilities(player)
            for player in range(game.num_players())
        ])
Exemplo n.º 3
0
 def test_extensive_to_tensor_game_type(self):
   game = pyspiel.extensive_to_tensor_game(
       pyspiel.load_game(
           "turn_based_simultaneous_game(game=blotto(players=3,coins=5))"))
   game_type = game.get_type()
   self.assertEqual(game_type.dynamics, pyspiel.GameType.Dynamics.SIMULTANEOUS)
   self.assertEqual(game_type.chance_mode,
                    pyspiel.GameType.ChanceMode.DETERMINISTIC)
   self.assertEqual(game_type.information,
                    pyspiel.GameType.Information.ONE_SHOT)
   self.assertEqual(game_type.utility, pyspiel.GameType.Utility.ZERO_SUM)
Exemplo n.º 4
0
def game_payoffs_array(game):
  """Returns a `numpy.ndarray` of utilities for a game.

  NOTE: if the game is not a MatrixGame or a TensorGame then this may be costly.

  Args:
    game: A game.

  Returns:
    `numpy.ndarray` of dimension `num_players` + 1.
    First dimension is the player, followed by the actions of all players, e.g.
    a 3x3 game (2 players) has dimension [2,3,3].
  """
  if isinstance(game, pyspiel.MatrixGame):
    return np.stack([game.row_utilities(), game.col_utilities()])

  if not isinstance(game, pyspiel.TensorGame):
    game = pyspiel.extensive_to_tensor_game(game)
  return np.stack(
      [game.player_utilities(player) for player in range(game.num_players())])