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)
Пример #2
0
def get_alpha_rank_pi(payoff_table):

    # matrix must be symmetric
    assert len(np.shape(payoff_table)) == 2
    assert np.shape(payoff_table)[0] == np.shape(payoff_table)[1]

    payoff_tables = (payoff_table, payoff_table.T)
    payoff_tables = [
        heuristic_payoff_table.from_matrix_game(payoff_tables[0]),
        heuristic_payoff_table.from_matrix_game(payoff_tables[1].T)
    ]

    # Check if the game is symmetric (i.e., players have identical strategy sets
    # and payoff tables) and return only a single-player’s payoff table if so.
    # This ensures Alpha-Rank automatically computes rankings based on the
    # single-population dynamics.
    is_symmetric, payoff_tables = utils.is_symmetric_matrix_game(payoff_tables)

    assert is_symmetric

    # Compute Alpha-Rank
    (rhos, rho_m, pi, num_profiles,
     num_strats_per_population) = alpharank.compute(payoff_tables, alpha=1e2)

    for i in range(len(pi)):
        if np.isclose(pi[i], 0.0):
            pi[i] = 0.0

    return pi
Пример #3
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)
Пример #4
0
def get_projected_replicator_dynamics_pi(payoff_table):
    # matrix must be symmetric
    assert len(np.shape(payoff_table)) == 2
    assert np.shape(payoff_table)[0] == np.shape(payoff_table)[1]

    payoff_tables = [payoff_table, payoff_table.T]
    # payoff_tables = [heuristic_payoff_table.from_matrix_game(payoff_tables[0]),
    #                  heuristic_payoff_table.from_matrix_game(payoff_tables[1].T)]

    # Check if the game is symmetric (i.e., players have identical strategy sets
    # and payoff tables) and return only a single-player’s payoff table if so.
    # This ensures Alpha-Rank automatically computes rankings based on the
    # single-population dynamics.
    is_symmetric, _ = utils.is_symmetric_matrix_game(payoff_tables)

    assert is_symmetric
    assert len(payoff_tables) == 2

    pi = projected_replicator_dynamics(payoff_tensors=payoff_tables,
                                       prd_initial_strategies=None,
                                       prd_iterations=int(1e6),
                                       prd_dt=1e-3,
                                       prd_gamma=0.0,
                                       average_over_last_n_strategies=None)

    return pi
Пример #5
0
    def __init__(self, game, enforce_symmetry=False):
        """Initializes the Double Oracle solver.

    Args:
      game: pyspiel.MatrixGame (zero-sum).
      enforce_symmetry: If True, enforces symmetry in the strategies appended by
        each player, by using the first player's best response for the second
        player as well; also asserts the game is symmetric and that players are
        seeded with identical initial_strategies, default: False.
    """
        assert isinstance(game, pyspiel.MatrixGame)
        assert game.get_type().utility == pyspiel.GameType.Utility.ZERO_SUM
        # convert matrix game to numpy.ndarray of shape [2,rows,columns]
        self.payoffs = utils.game_payoffs_array(game)
        self.subgame_strategies = [[], []]
        self.enforce_symmetry = enforce_symmetry
        if self.enforce_symmetry:
            assert utils.is_symmetric_matrix_game(self.payoffs), (
                "enforce_symmetry is True, but payoffs are asymmetric!")