コード例 #1
0
    def test_stationary_distribution(self):
        """Tests stationary distribution using payoffs from Han et al., 2013."""
        r = 1.
        t = 2.
        p = 0.
        s = -1.
        delta = 4.
        eps = 0.25
        payoff_tables = [
            np.asarray([[r - eps / 2., r - eps, 0, s + delta - eps, r - eps],
                        [r, r, s, s, s], [0, t, p, p, p],
                        [t - delta, t, p, p, p], [r, t, p, p, p]])
        ]

        m = 20
        alpha = 0.1
        expected_pi = np.asarray(
            [0.40966787, 0.07959841, 0.20506998, 0.08505983, 0.2206039])

        # Test payoffs in matrix format
        _, _, pi_matrix, _, _ = alpharank.compute(
            payoff_tables, m=m, alpha=alpha, use_local_selection_model=False)
        np.testing.assert_array_almost_equal(pi_matrix, expected_pi, decimal=4)

        # Test payoffs in HPT format
        hpts = [heuristic_payoff_table.from_matrix_game(payoff_tables[0])]
        _, _, pi_hpts, _, _ = alpharank.compute(
            hpts, m=m, alpha=alpha, use_local_selection_model=False)
        np.testing.assert_array_almost_equal(pi_hpts, expected_pi, decimal=4)
コード例 #2
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)
コード例 #3
0
def main(unused_arg):
    # Construct meta-game payoff tables
    # payoff_tables = get_kuhn_poker_data()

    payoff_tables = [
        np.array([[1.1, -10.0], [1.0, -1.0], [-1.0, 1.0]]),
        np.array([[-1.1, 10.0], [-1.0, 1.0], [1.0, -1.0]])
    ]

    payoffs_are_hpt_format = utils.check_payoffs_are_hpt(payoff_tables)
    strat_labels = utils.get_strat_profile_labels(payoff_tables,
                                                  payoffs_are_hpt_format)

    # Run AlphaRank

    rhos, rho_m, pi, _, _ = alpharank.compute(payoff_tables, alpha=1e1)

    # Report & plot results
    alpharank.print_results(payoff_tables,
                            payoffs_are_hpt_format,
                            rhos=rhos,
                            rho_m=rho_m,
                            pi=pi)
    utils.print_rankings_table(payoff_tables, pi, strat_labels)
    m_network_plotter = alpharank_visualizer.NetworkPlot(payoff_tables,
                                                         rhos,
                                                         rho_m,
                                                         pi,
                                                         strat_labels,
                                                         num_top_profiles=8)
    m_network_plotter.compute_and_draw_network()
コード例 #4
0
ファイル: measurements.py プロジェクト: ziyuwan/pipeline-psro
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
コード例 #5
0
ファイル: utils.py プロジェクト: wjzhong/open_spiel
def alpharank_strategy(solver, return_joint=False, **unused_kwargs):
  """Returns AlphaRank distribution on meta game matrix.

  This method works for general games.

  Args:
    solver: GenPSROSolver instance.
    return_joint: a boolean specifying whether to return player-wise
      marginals.

  Returns:
    marginals: a list, specifying for each player the alpharank marginal
      distributions on their strategies.
    joint_distr: a list, specifying the joint alpharank distributions for all
      strategy profiles.
  """
  meta_games = solver.get_meta_game()
  meta_games = [np.asarray(x) for x in meta_games]

  if solver.symmetric_game:
    meta_games = [meta_games[0]]

    # Get alpharank distribution via alpha-sweep
    _, _, joint_distr, _, _ = alpharank.compute(
        meta_games)
    joint_distr = remove_epsilon_negative_probs(joint_distr)

    marginals = 2 * [joint_distr]
    joint_distr = get_joint_strategy_from_marginals(marginals)
    if return_joint:
      return marginals, joint_distr
    else:
      return joint_distr

  else:
    _, _, joint_distr, _, _ = alpharank.compute(meta_games)
    joint_distr = remove_epsilon_negative_probs(joint_distr)

    if return_joint:
      marginals = get_alpharank_marginals(meta_games, joint_distr)
      return marginals, joint_distr
    else:
      return joint_distr
コード例 #6
0
if __name__ == '__main__':
    csv_path = "alpha_rank_all_matches_simple.csv"

    # Import the csv
    df = pd.read_csv(csv_path, index_col=False)

    # Convert to payoff_tables
    payoff_tables, strat_labels = payoff_tables_from_df(df)
    # payoff_tables = heuristic_payoff_table.from_elo_scores([1286, 1322, 1401, 1440, 1457, 1466, 1470])

    payoffs_are_hpt_format = utils.check_payoffs_are_hpt(payoff_tables)

    # strat_labels = utils.get_strat_profile_labels(payoff_tables(), payoffs_are_hpt_format)

    # Run AlphaRank
    rhos, rho_m, pi, _, _ = alpharank.compute(payoff_tables, alpha=1e-1)

    # Report & plot results
    alpharank.print_results(payoff_tables,
                            payoffs_are_hpt_format,
                            rhos=rhos,
                            rho_m=rho_m,
                            pi=pi)

    utils.print_rankings_table(payoff_tables, pi, strat_labels)

    m_network_plotter = alpharank_visualizer.NetworkPlot(payoff_tables,
                                                         rhos,
                                                         rho_m,
                                                         pi,
                                                         strat_labels,