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
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)
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
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())