Ejemplo n.º 1
0
def main(_):
    games_list = pyspiel.registered_games()
    print("Registered games:")
    print(games_list)

    # Load a two-player normal-form game as a two-player matrix game.
    blotto_matrix_game = pyspiel.load_matrix_game("blotto")
    print(
        "Number of rows in 2-player Blotto with default settings is {}".format(
            blotto_matrix_game.num_rows()))

    # Several ways to load/create the same game of matching pennies.
    print("Creating matrix game...")
    game = pyspiel.load_matrix_game("matrix_mp")
    game = _manually_create_game()
    game = _import_data_create_game()
    game = _easy_create_game()
    game = _even_easier_create_game()

    # Quick test: inspect top-left utility values:
    print("Values for joint action ({},{}) is {},{}".format(
        game.row_action_name(0), game.col_action_name(0),
        game.player_utility(0, 0, 0), game.player_utility(1, 0, 0)))

    state = game.new_initial_state()

    # Print the initial state
    print("State:")
    print(str(state))

    assert state.is_simultaneous_node()

    # Simultaneous node: sample actions for all players.
    chosen_actions = [
        random.choice(state.legal_actions(pid))
        for pid in range(game.num_players())
    ]
    print("Chosen actions: ", [
        state.action_to_string(pid, action)
        for pid, action in enumerate(chosen_actions)
    ])
    state.apply_actions(chosen_actions)

    assert state.is_terminal()

    # Game is now done. Print utilities for each player
    returns = state.returns()
    for pid in range(game.num_players()):
        print("Utility for player {} is {}".format(pid, returns[pid]))
Ejemplo n.º 2
0
    def test_constant_sum_transition_matrix(self):
        """Tests closed-form transition matrix computation for constant-sum case."""

        game = pyspiel.load_matrix_game("matrix_rps")
        payoff_tables = utils.nfg_to_ndarray(game)

        # Checks if the game is symmetric and runs single-population analysis if so
        _, payoff_tables = utils.is_symmetric_matrix_game(payoff_tables)
        payoffs_are_hpt_format = utils.check_payoffs_are_hpt(payoff_tables)

        m = 20
        alpha = 0.1

        # Case 1) General-sum game computation (slower)
        game_is_constant_sum = False
        use_local_selection_model = False
        payoff_sum = None
        c1, rhos1 = alpharank._get_singlepop_transition_matrix(
            payoff_tables[0], payoffs_are_hpt_format, m, alpha,
            game_is_constant_sum, use_local_selection_model, payoff_sum)

        # Case 2) Constant-sum closed-form computation (faster)
        game_is_constant_sum, payoff_sum = utils.check_is_constant_sum(
            payoff_tables[0], payoffs_are_hpt_format)
        c2, rhos2 = alpharank._get_singlepop_transition_matrix(
            payoff_tables[0], payoffs_are_hpt_format, m, alpha,
            game_is_constant_sum, use_local_selection_model, payoff_sum)

        # Ensure both cases match
        np.testing.assert_array_almost_equal(c1, c2)
        np.testing.assert_array_almost_equal(rhos1, rhos2)
Ejemplo n.º 3
0
 def test_single_step(self):
     game = pyspiel.load_matrix_game("matrix_rps")
     solver = double_oracle.DoubleOracleSolver(game)
     solver.subgame_strategies = [[0], [0]]
     best_response, best_response_utility = solver.step()
     self.assertListEqual(best_response, [1, 1])
     self.assertListEqual(best_response_utility, [1.0, 1.0])
  def test_plot_pi_vs_alpha(self, mock_plt):
    # Construct game
    game = pyspiel.load_matrix_game("matrix_rps")
    payoff_tables = utils.game_payoffs_array(game)
    _, payoff_tables = utils.is_symmetric_matrix_game(payoff_tables)
    payoffs_are_hpt_format = utils.check_payoffs_are_hpt(payoff_tables)

    # Compute alpharank
    alpha = 1e2
    _, _, pi, num_profiles, num_strats_per_population = (
        alpharank.compute(payoff_tables, alpha=alpha))
    strat_labels = utils.get_strat_profile_labels(payoff_tables,
                                                  payoffs_are_hpt_format)
    num_populations = len(payoff_tables)

    # Construct synthetic pi-vs-alpha history
    pi_list = np.empty((num_profiles, 0))
    alpha_list = []
    for _ in range(2):
      pi_list = np.append(pi_list, np.reshape(pi, (-1, 1)), axis=1)
      alpha_list.append(alpha)

    # Test plotting code (via pyplot mocking to prevent plot pop-up)
    alpharank_visualizer.plot_pi_vs_alpha(
        pi_list.T,
        alpha_list,
        num_populations,
        num_strats_per_population,
        strat_labels,
        num_strats_to_label=0)
    self.assertTrue(mock_plt.show.called)
 def test_expected_payoff(self, strategy):
     logging.info("Testing expected payoff for matrix game.")
     game = pyspiel.load_matrix_game("matrix_rps")
     payoff_tables = utils.game_payoffs_array(game)
     table = heuristic_payoff_table.from_matrix_game(payoff_tables[0])
     expected_payoff = table.expected_payoff(strategy)
     print(expected_payoff)
     assert len(expected_payoff) == table._num_strategies
Ejemplo n.º 6
0
 def test_nfg_to_ndarray_pd(self):
     """Test `nfg_to_ndarray` for prisoners' dilemma."""
     game = pyspiel.load_matrix_game("matrix_pd")
     payoff_matrix = np.empty(shape=(2, 2, 2))
     payoff_row = np.array([[5., 0.], [10., 1.]])
     payoff_matrix[0] = payoff_row
     payoff_matrix[1] = payoff_row.T
     np.testing.assert_allclose(utils.nfg_to_ndarray(game), payoff_matrix)
Ejemplo n.º 7
0
 def test_multi_population_rps(self):
     game = pyspiel.load_matrix_game('matrix_rps')
     payoff_matrix = game_payoffs_array(game)
     rd = dynamics.replicator
     dyn = dynamics.MultiPopulationDynamics(payoff_matrix, [rd] * 2)
     x = np.concatenate(
         [np.ones(k) / float(k) for k in payoff_matrix.shape[1:]])
     np.testing.assert_allclose(dyn(x), np.zeros((6, )), atol=1e-15)
Ejemplo n.º 8
0
 def test_nfg_to_ndarray_rps(self):
     """Test `nfg_to_ndarray` for rock-paper-scissors."""
     game = pyspiel.load_matrix_game("matrix_rps")
     payoff_matrix = np.empty(shape=(2, 3, 3))
     payoff_row = np.array([[0., -1., 1.], [1., 0., -1.], [-1., 1., 0.]])
     payoff_matrix[0] = payoff_row
     payoff_matrix[1] = -1. * payoff_row
     np.testing.assert_allclose(utils.nfg_to_ndarray(game), payoff_matrix)
Ejemplo n.º 9
0
 def test_rock_paper_scissors(self):
     game = pyspiel.load_matrix_game("matrix_rps")
     solver = double_oracle.DoubleOracleSolver(game)
     solution, iteration, value = solver.solve(
         initial_strategies=[[0], [0]])
     np.testing.assert_allclose(solution[0], np.ones(3) / 3.)
     np.testing.assert_allclose(solution[1], np.ones(3) / 3.)
     self.assertEqual(iteration, 3)
     self.assertAlmostEqual(value, 0.0)
Ejemplo n.º 10
0
 def test_rd_rps_pure_fixed_points(self):
     game = pyspiel.load_matrix_game('matrix_rps')
     payoff_matrix = game_payoffs_array(game)
     rd = dynamics.replicator
     dyn = dynamics.SinglePopulationDynamics(payoff_matrix, rd)
     x = np.eye(3)
     np.testing.assert_allclose(dyn(x[0]), np.zeros((3, )))
     np.testing.assert_allclose(dyn(x[1]), np.zeros((3, )))
     np.testing.assert_allclose(dyn(x[2]), np.zeros((3, )))
 def test_solve_blotto(self):
     blotto_matrix_game = pyspiel.load_matrix_game("blotto")
     p0_sol, p1_sol, p0_sol_val, p1_sol_val = (
         lp_solver.solve_zero_sum_matrix_game(blotto_matrix_game))
     self.assertEqual(len(p0_sol), blotto_matrix_game.num_rows())
     self.assertEqual(len(p1_sol), blotto_matrix_game.num_cols())
     # Symmetric game, must be zero
     self.assertAlmostEqual(p0_sol_val, 0.0)
     self.assertAlmostEqual(p1_sol_val, 0.0)
Ejemplo n.º 12
0
    def test_constant_sum_checker(self):
        """Tests if verification of constant-sum game is correct."""

        game = pyspiel.load_matrix_game("matrix_rps")
        payoff_tables = utils.nfg_to_ndarray(game)
        payoffs_are_hpt_format = utils.check_payoffs_are_hpt(payoff_tables)
        game_is_constant_sum, payoff_sum = utils.check_is_constant_sum(
            payoff_tables[0], payoffs_are_hpt_format)
        self.assertTrue(game_is_constant_sum)
        self.assertEqual(payoff_sum, 0.)
Ejemplo n.º 13
0
    def test_extensive_to_matrix_game_payoff_matrix(self):
        turn_based_game = pyspiel.load_game_as_turn_based("matrix_pd")
        matrix_game = pyspiel.extensive_to_matrix_game(turn_based_game)
        orig_game = pyspiel.load_matrix_game("matrix_pd")

        for row in range(orig_game.num_rows()):
            for col in range(orig_game.num_cols()):
                for player in range(2):
                    self.assertEqual(
                        orig_game.player_utility(player, row, col),
                        matrix_game.player_utility(player, row, col))
Ejemplo n.º 14
0
def simulate_dynamics(args):
    game = pyspiel.load_matrix_game('matrix_' + args.game)
    payoff_matrix = game_payoffs_array(game)
    game_dim = len(payoff_matrix[0])
    print(payoff_matrix)
    func = getattr(algos, args.algo)
    if game_dim == 3:
        dyn = build_3x3_dynamics(payoff_matrix, func)
    else:
        dyn = build_2x2_dynamics(payoff_matrix, func)
    return game_dim, dyn
 def test_iterated_dominance_prisoners_dilemma(self):
     # find the strictly dominant (D, D) strategy
     pd = pyspiel.load_matrix_game("matrix_pd")
     pd_dom, pd_live = self._checked_iterated_dominance(
         pd, lp_solver.DOMINANCE_STRICT)
     self.assertEqual(pd_dom.num_rows(), 1)
     self.assertEqual(pd_dom.num_cols(), 1)
     self.assertEqual(pd_dom.row_action_name(0), "Defect")
     self.assertEqual(pd_dom.col_action_name(0), "Defect")
     self.assertListEqual(pd_live[0].tolist(), [False, True])
     self.assertListEqual(pd_live[1].tolist(), [False, True])
 def test_dominance_prisoners_dilemma(self):
     self._assert_dominated(0, pyspiel.load_matrix_game("matrix_pd"), 1,
                            lp_solver.DOMINANCE_STRICT)
     self._assert_undominated(1, pyspiel.load_matrix_game("matrix_pd"), 1,
                              lp_solver.DOMINANCE_VERY_WEAK)
Ejemplo n.º 17
0
 def test_dynamics_rps_mixed_fixed_point(self, func):
     game = pyspiel.load_matrix_game('matrix_rps')
     payoff_matrix = game_payoffs_array(game)
     dyn = dynamics.SinglePopulationDynamics(payoff_matrix, func)
     x = np.ones(shape=(3, )) / 3.
     np.testing.assert_allclose(dyn(x), np.zeros((3, )), atol=1e-15)
 def test_from_matrix_game(self, game):
     game = pyspiel.load_matrix_game(game)
     payoff_tables = utils.game_payoffs_array(game)
     logging.info("Testing payoff table construction for matrix game.")
     table = heuristic_payoff_table.from_matrix_game(payoff_tables[0])
     print(table())