def test_get_evolutionary_best_response_with_K_equal_1_against_2_defectors():
    """
    We see that the expected best response against 2 defectors is:

    [1, 0.45420466, 0, 0.03799109]

    - the first term: indicates that when the player is playing against itself
      it knows to always cooperate.
    - The second term (P_CD): it will still cooperate with relatively high
      probability (0.454...).
    - The third term (P_DC): it will never cooperate after defecting against one
      a player like itself.
    - The final term (P_DD): it is very likely to defect on the occasions where
      it defected and so did it's opponent (probably likely that the opponent is
      defector?).
    """
    opponents = [np.array([0, 0, 0, 0])] * 2

    axl.seed(2)
    (
        best_ev_response,
        hist,
        history_length,
    ) = opt_mo.get_evolutionary_best_response(
        opponents, opt_mo.get_memory_one_best_response, K=1)

    expected_best_response = np.array([1, 0.45420466, 0, 0.03799109])
    assert type(best_ev_response) is np.ndarray
    assert len(best_ev_response) == 4
    assert np.allclose(expected_best_response, best_ev_response)
def test_get_evolutionary_best_response():
    axl.seed(2)
    random_opponents = [[random.random() for _ in range(4)] for _ in range(1)]

    best_ev_response, hist, history_length = opt_mo.get_evolutionary_best_response(
        random_opponents, opt_mo.get_memory_one_best_response)

    assert type(best_ev_response) is np.ndarray
    assert len(best_ev_response) == 4
Esempio n. 3
0
def obtain_payoff_matrix(opponent, K, N=4):
    """
    Obtain the payoff matrix for K best response players in a population of a
    total of N individuals.
    """
    best_ev_response, _, _ = opt_mo.get_evolutionary_best_response(
        [opponent] * (N - K), opt_mo.get_memory_one_best_response, K=K)
    players = [best_ev_response, opponent]
    return (
        np.array(
            [[opt_mo.match_utility(player, opponent) for opponent in players]
             for player in players]),
        best_ev_response,
    )
def test_get_evolutionary_best_response_against_random_opponent():
    axl.seed(2)
    random_opponents = [[random.random() for _ in range(4)]]

    (
        best_ev_response,
        hist,
        history_length,
    ) = opt_mo.get_evolutionary_best_response(
        random_opponents, opt_mo.get_memory_one_best_response)

    expected_best_response = np.array([0.0, 0.0, 0.0, 0.0])
    assert type(best_ev_response) is np.ndarray
    assert np.allclose(expected_best_response, best_ev_response)
def test_get_evolutionary_best_response_with_K_equal_4_against_random_opponent(
):
    axl.seed(2)
    random_opponents = [[random.random() for _ in range(4)]]

    (
        best_ev_response,
        hist,
        history_length,
    ) = opt_mo.get_evolutionary_best_response(
        random_opponents, opt_mo.get_memory_one_best_response, K=4)

    expected_best_response = np.array([0.01547687, 0, 0, 0])
    assert type(best_ev_response) is np.ndarray
    assert len(best_ev_response) == 4
    assert np.allclose(expected_best_response, best_ev_response, atol=1e-3)
def test_get_evolutionary_best_response_with_K_equal_10_against_2_defectors():
    """
    With K = 10 and 2 defectors the best response changes from `[1, 0.45420466,
    0, 0.03799109]` (for K = 1) to:

    [1, 0.36819048, 1, 0.75011483]
    """
    opponents = [np.array([0, 0, 0, 0])] * 2

    axl.seed(2)
    (
        best_ev_response,
        hist,
        history_length,
    ) = opt_mo.get_evolutionary_best_response(
        opponents, opt_mo.get_memory_one_best_response, K=10)

    expected_best_response = np.array([1, 0.36819048, 1, 0.75011483])
    assert type(best_ev_response) is np.ndarray
    assert len(best_ev_response) == 4
    assert np.allclose(expected_best_response, best_ev_response, atol=1e-3)