class TestPlayer_2opponents:
    """Test the methods of Player with two opponent players"""

    def setUp(self):
        """Setup a Player instance"""
        payoffs_2opponents = [[[3, 6], [4, 2]], [[1, 0], [5, 7]]]
        self.player = Player(payoffs_2opponents)

    def test_payoff_vector_against_pure(self):
        assert_array_equal(self.player.payoff_vector((0, 1)), [6, 0])

    def test_is_best_response_against_pure(self):
        ok_(not self.player.is_best_response(0, (1, 0)))

    def test_best_response_against_pure(self):
        eq_(self.player.best_response((1, 1)), 1)

    def test_best_response_list_when_tie(self):
        """
        best_response against a mixed action profile with
        tie_breaking=False
        """
        assert_array_equal(
            sorted(self.player.best_response(([3 / 7, 4 / 7], [1 / 2, 1 / 2]), tie_breaking=False)), sorted([0, 1])
        )
Exemple #2
0
class TestPlayer_2opponents:
    """Test the methods of Player with two opponent players"""
    def setUp(self):
        """Setup a Player instance"""
        payoffs_2opponents = [[[3, 6], [4, 2]], [[1, 0], [5, 7]]]
        self.player = Player(payoffs_2opponents)

    def test_payoff_vector_against_pure(self):
        assert_array_equal(self.player.payoff_vector((0, 1)), [6, 0])

    def test_is_best_response_against_pure(self):
        ok_(not self.player.is_best_response(0, (1, 0)))

    def test_best_response_against_pure(self):
        eq_(self.player.best_response((1, 1)), 1)

    def test_best_response_list_when_tie(self):
        """
        best_response against a mixed action profile with
        tie_breaking=False
        """
        assert_array_equal(
            sorted(
                self.player.best_response(([3 / 7, 4 / 7], [1 / 2, 1 / 2]),
                                          tie_breaking=False)), sorted([0, 1]))
Exemple #3
0
class TestPlayer_0opponents:
    """Test for trivial Player with no opponent player"""

    def setUp(self):
        """Setup a Player instance"""
        self.payoffs = [0, 1, -1]
        self.player = Player(self.payoffs)
        self.best_response_action = 1
        self.dominated_actions = [0, 2]

    def test_payoff_vector(self):
        """Trivial player: payoff_vector"""
        assert_array_equal(self.player.payoff_vector(None), self.payoffs)

    def test_is_best_response(self):
        """Trivial player: is_best_response"""
        ok_(self.player.is_best_response(self.best_response_action, None))

    def test_best_response(self):
        """Trivial player: best_response"""
        eq_(self.player.best_response(None), self.best_response_action)

    def test_is_dominated(self):
        """Trivial player: is_dominated"""
        for action in range(self.player.num_actions):
            eq_(self.player.is_dominated(action),
                (action in self.dominated_actions))

    def test_dominated_actions(self):
        """Trivial player: dominated_actions"""
        eq_(self.player.dominated_actions(), self.dominated_actions)
Exemple #4
0
 def setUp(self):
     """Setup a Player instance"""
     payoffs_2opponents = [[[3, 6],
                            [4, 2]],
                           [[1, 0],
                            [5, 7]]]
     self.player = Player(payoffs_2opponents)
def test_random_choice():
    n, m = 5, 4
    payoff_matrix = np.zeros((n, m))
    player = Player(payoff_matrix)

    eq_(player.random_choice([0]), 0)

    actions = list(range(player.num_actions))
    ok_(player.random_choice() in actions)
Exemple #6
0
def test_random_choice():
    n, m = 5, 4
    payoff_matrix = np.zeros((n, m))
    player = Player(payoff_matrix)

    eq_(player.random_choice([0]), 0)

    actions = list(range(player.num_actions))
    ok_(player.random_choice() in actions)
Exemple #7
0
class TestPlayer_1action:
    def setUp(self):
        """Setup a Player instance"""
        self.payoffs = [[0, 1]]
        self.player = Player(self.payoffs)

    def test_is_dominated(self):
        for action in range(self.player.num_actions):
            for method in LP_METHODS:
                eq_(self.player.is_dominated(action, method=method), False)

    def test_dominated_actions(self):
        for method in LP_METHODS:
            eq_(self.player.dominated_actions(method=method), [])
class TestPlayer_1action:
    def setUp(self):
        """Setup a Player instance"""
        self.payoffs = [[0, 1]]
        self.player = Player(self.payoffs)

    def test_is_dominated(self):
        for action in range(self.player.num_actions):
            for method in LP_METHODS:
                eq_(self.player.is_dominated(action, method=method), False)

    def test_dominated_actions(self):
        for method in LP_METHODS:
            eq_(self.player.dominated_actions(method=method), [])
    def test_random_matrix(self):
        seed = 12345
        rng = np.random.default_rng(seed)
        size = (10, 15)
        A = rng.normal(size=size)
        v, x, y = minmax(A)

        for z in [x, y]:
            assert_((z >= 0).all())
            assert_allclose(z.sum(), 1)

        g = NormalFormGame((Player(A), Player(-A.T)))
        NE = lemke_howson(g)
        assert_allclose(v, NE[0] @ A @ NE[1])
        assert_(g.is_nash((x, y)))
Exemple #10
0
def test_player_corner_cases():
    n, m = 3, 4
    player = Player(np.zeros((n, m)))
    for action in range(n):
        eq_(player.is_best_response(action, [1 / m] * m), True)
        for method in [None, 'simplex']:
            eq_(player.is_dominated(action, method=method), False)

    e = 1e-8
    player = Player([[-e, -e], [1, -1], [-1, 1]])
    action = 0
    eq_(player.is_best_response(action, [1 / 2, 1 / 2], tol=e), True)
    eq_(player.is_best_response(action, [1 / 2, 1 / 2], tol=e / 2), False)
    for method in [None, 'simplex']:
        eq_(player.is_dominated(action, tol=e, method=method), False)
        eq_(player.is_dominated(action, tol=e / 2, method=method), True)
 def anti_coordination(N, v):
     payoff_array = np.empty((2, ) * N)
     payoff_array[0, :] = 1
     payoff_array[1, :] = 0
     payoff_array[1].flat[0] = v
     g = NormalFormGame((Player(payoff_array), ) * N)
     return g
Exemple #12
0
 def setUp(self):
     """Setup a NormalFormGame instance"""
     payoffs_2opponents = [[[3, 6],
                            [4, 2]],
                           [[1, 0],
                            [5, 7]]]
     player = Player(payoffs_2opponents)
     self.g = NormalFormGame([player for i in range(3)])
Exemple #13
0
class TestPlayer_0opponents:
    """Test for trivial Player with no opponent player"""
    def setUp(self):
        """Setup a Player instance"""
        payoffs = [0, 1]
        self.player = Player(payoffs)

    def test_payoff_vector(self):
        """Trivial player: payoff_vector"""
        assert_array_equal(self.player.payoff_vector(None), [0, 1])

    def test_is_best_response(self):
        """Trivial player: is_best_response"""
        ok_(self.player.is_best_response(1, None))

    def test_best_response(self):
        """Trivial player: best_response"""
        eq_(self.player.best_response(None), 1)
class TestPlayer_0opponents:
    """Test for trivial Player with no opponent player"""

    def setUp(self):
        """Setup a Player instance"""
        payoffs = [0, 1]
        self.player = Player(payoffs)

    def test_payoff_vector(self):
        """Trivial player: payoff_vector"""
        assert_array_equal(self.player.payoff_vector(None), [0, 1])

    def test_is_best_response(self):
        """Trivial player: is_best_response"""
        ok_(self.player.is_best_response(1, None))

    def test_best_response(self):
        """Trivial player: best_response"""
        eq_(self.player.best_response(None), 1)
def test_player_corner_cases():
    n, m = 3, 4
    player = Player(np.zeros((n, m)))
    for action in range(n):
        eq_(player.is_best_response(action, [1/m]*m), True)
        for method in LP_METHODS:
            eq_(player.is_dominated(action, method=method), False)

    e = 1e-8
    player = Player([[-e, -e], [1, -1], [-1, 1]])
    action = 0
    eq_(player.is_best_response(action, [1/2, 1/2], tol=e), True)
    eq_(player.is_best_response(action, [1/2, 1/2], tol=e/2), False)
    for method in LP_METHODS:
        eq_(player.is_dominated(action, tol=2*e, method=method), False)
        eq_(player.dominated_actions(tol=2*e, method=method), [])

        eq_(player.is_dominated(action, tol=e/2, method=method), True)
        eq_(player.dominated_actions(tol=e/2, method=method), [action])
Exemple #16
0
def test_player_repr():
    nums_actions = (2, 3, 4)
    payoff_arrays = [
        np.arange(np.prod(nums_actions[0:i])).reshape(nums_actions[0:i])
        for i in range(1, len(nums_actions)+1)
    ]
    players = [Player(payoff_array) for payoff_array in payoff_arrays]

    for player in players:
        player_new = eval(repr(player))
        assert_array_equal(player_new.payoff_array, player.payoff_array)
class TestPlayer_1opponent:
    """Test the methods of Player with one opponent player"""

    def setUp(self):
        """Setup a Player instance"""
        coordination_game_matrix = [[4, 0], [3, 2]]
        self.player = Player(coordination_game_matrix)

    def test_best_response_against_pure(self):
        eq_(self.player.best_response(1), 1)

    def test_best_response_against_mixed(self):
        eq_(self.player.best_response([1 / 2, 1 / 2]), 1)

    def test_best_response_list_when_tie(self):
        """best_response with tie_breaking=False"""
        assert_array_equal(sorted(self.player.best_response([2 / 3, 1 / 3], tie_breaking=False)), sorted([0, 1]))

    def test_best_response_with_random_tie_breaking(self):
        """best_response with tie_breaking='random'"""
        ok_(self.player.best_response([2 / 3, 1 / 3], tie_breaking="random") in [0, 1])

        seed = 1234
        br0 = self.player.best_response([2 / 3, 1 / 3], tie_breaking="random", random_state=seed)
        br1 = self.player.best_response([2 / 3, 1 / 3], tie_breaking="random", random_state=seed)
        eq_(br0, br1)

    def test_best_response_with_smallest_tie_breaking(self):
        """best_response with tie_breaking='smallest' (default)"""
        eq_(self.player.best_response([2 / 3, 1 / 3]), 0)

    def test_best_response_with_payoff_perturbation(self):
        """best_response with payoff_perturbation"""
        eq_(self.player.best_response([2 / 3, 1 / 3], payoff_perturbation=[0, 0.1]), 1)
        eq_(self.player.best_response([2, 1], payoff_perturbation=[0, 0.1]), 1)  # int

    def test_is_best_response_against_pure(self):
        ok_(self.player.is_best_response(0, 0))

    def test_is_best_response_against_mixed(self):
        ok_(self.player.is_best_response([1 / 2, 1 / 2], [2 / 3, 1 / 3]))
    def setUp(self):
        self.game_dicts = []

        # From von Stengel 2007 in Algorithmic Game Theory
        bimatrix = [[(3, 3), (3, 3)], [(2, 2), (5, 6)], [(0, 3), (6, 1)]]
        NEs_dict = {0: ([0, 1 / 3, 2 / 3], [1 / 3, 2 / 3])}
        d = {
            'g': NormalFormGame(bimatrix),
            'NEs_dict': NEs_dict,
            'converged': True
        }
        self.game_dicts.append(d)

        # == Examples of cycles by "ad hoc" tie breaking rules == #

        # Example where tie breaking that picks the variable with
        # the smallest row index in the tableau leads to cycling
        A = np.array([[0, 0, 0], [0, 1, 1], [1, 1, 0]])
        B = np.array([[1, 0, 1], [1, 1, 0], [0, 0, 2]])
        NEs_dict = {0: ([0, 2 / 3, 1 / 3], [0, 1, 0])}
        d = {
            'g': NormalFormGame((Player(A), Player(B))),
            'NEs_dict': NEs_dict,
            'converged': True
        }
        self.game_dicts.append(d)

        # Example where tie breaking that picks the variable with
        # the smallest variable index in the tableau leads to cycling
        perm = [2, 0, 1]
        C = A[:, perm]
        D = B[perm, :]
        NEs_dict = {0: ([0, 2 / 3, 1 / 3], [0, 0, 1])}
        d = {
            'g': NormalFormGame((Player(C), Player(D))),
            'NEs_dict': NEs_dict,
            'converged': True
        }
        self.game_dicts.append(d)
Exemple #19
0
def test_normalformgame_payoff_profile_array():
    nums_actions = (2, 3, 4)
    for N in range(1, len(nums_actions) + 1):
        payoff_arrays = [
            np.arange(np.prod(nums_actions[0:N])).reshape(nums_actions[i:N] +
                                                          nums_actions[0:i])
            for i in range(N)
        ]
        players = [Player(payoff_array) for payoff_array in payoff_arrays]
        g = NormalFormGame(players)
        g_new = NormalFormGame(g.payoff_profile_array)
        for player_new, payoff_array in zip(g_new.players, payoff_arrays):
            assert_array_equal(player_new.payoff_array, payoff_array)
class TestPlayer_2opponents:
    """Test the methods of Player with two opponent players"""

    def setUp(self):
        """Setup a Player instance"""
        payoffs_2opponents = [[[3, 6],
                               [4, 2]],
                              [[1, 0],
                               [5, 7]]]
        self.player = Player(payoffs_2opponents)

    def test_delete_action(self):
        N = self.player.num_opponents + 1
        action_to_delete = 0
        actions_to_remain = \
            np.setdiff1d(np.arange(self.player.num_actions), action_to_delete)
        for i in range(N):
            player_new = self.player.delete_action(action_to_delete, i)
            assert_array_equal(
                player_new.payoff_array,
                self.player.payoff_array.take(actions_to_remain, axis=i)
            )

    def test_payoff_vector_against_pure(self):
        assert_array_equal(self.player.payoff_vector((0, 1)), [6, 0])

    def test_is_best_response_against_pure(self):
        ok_(not self.player.is_best_response(0, (1, 0)))

    def test_best_response_against_pure(self):
        eq_(self.player.best_response((1, 1)), 1)

    def test_best_response_list_when_tie(self):
        """
        best_response against a mixed action profile with
        tie_breaking=False
        """
        assert_array_equal(
            sorted(self.player.best_response(([3/7, 4/7], [1/2, 1/2]),
                                             tie_breaking=False)),
            sorted([0, 1])
        )

    def test_is_dominated(self):
        for action in range(self.player.num_actions):
            for method in LP_METHODS:
                eq_(self.player.is_dominated(action, method=method), False)

    def test_dominated_actions(self):
        for method in LP_METHODS:
            eq_(self.player.dominated_actions(method=method), [])
Exemple #21
0
def random_skew_sym(n, m=None, random_state=None):
    """
    Generate a random skew symmetric zero-sum NormalFormGame of the form
    O    B
    -B.T O
    where B is an n x m matrix.

    """
    if m is None:
        m = n
    random_state = check_random_state(random_state)
    B = random_state.random_sample((n, m))
    A = np.empty((n + m, n + m))
    A[:n, :n] = 0
    A[n:, n:] = 0
    A[:n, n:] = B
    A[n:, :n] = -B.T
    return NormalFormGame([Player(A) for i in range(2)])
Exemple #22
0
class TestPlayer_2opponents:
    """Test the methods of Player with two opponent players"""
    def setUp(self):
        """Setup a Player instance"""
        payoffs_2opponents = [[[3, 6], [4, 2]], [[1, 0], [5, 7]]]
        self.player = Player(payoffs_2opponents)

    def test_delete_action(self):
        N = self.player.num_opponents + 1
        action_to_delete = 0
        actions_to_remain = \
            np.setdiff1d(np.arange(self.player.num_actions), action_to_delete)
        for i in range(N):
            player_new = self.player.delete_action(action_to_delete, i)
            assert_array_equal(
                player_new.payoff_array,
                self.player.payoff_array.take(actions_to_remain, axis=i))

    def test_payoff_vector_against_pure(self):
        assert_array_equal(self.player.payoff_vector((0, 1)), [6, 0])

    def test_is_best_response_against_pure(self):
        ok_(not self.player.is_best_response(0, (1, 0)))

    def test_best_response_against_pure(self):
        eq_(self.player.best_response((1, 1)), 1)

    def test_best_response_list_when_tie(self):
        """
        best_response against a mixed action profile with
        tie_breaking=False
        """
        assert_array_equal(
            sorted(
                self.player.best_response(([3 / 7, 4 / 7], [1 / 2, 1 / 2]),
                                          tie_breaking=False)), sorted([0, 1]))

    def test_is_dominated(self):
        for action in range(self.player.num_actions):
            for method in LP_METHODS:
                eq_(self.player.is_dominated(action, method=method), False)

    def test_dominated_actions(self):
        for method in LP_METHODS:
            eq_(self.player.dominated_actions(method=method), [])
class TestPlayer_0opponents:
    """Test for trivial Player with no opponent player"""

    def setUp(self):
        """Setup a Player instance"""
        self.payoffs = [0, 1, -1]
        self.player = Player(self.payoffs)
        self.best_response_action = 1
        self.dominated_actions = [0, 2]

    def test_delete_action(self):
        N = self.player.num_opponents + 1
        actions_to_delete = [0, 2]
        actions_to_remain = \
            np.setdiff1d(np.arange(self.player.num_actions), actions_to_delete)
        for i in range(N):
            player_new = self.player.delete_action(actions_to_delete, i)
            assert_array_equal(
                player_new.payoff_array,
                self.player.payoff_array.take(actions_to_remain, axis=i)
            )

    def test_payoff_vector(self):
        """Trivial player: payoff_vector"""
        assert_array_equal(self.player.payoff_vector(None), self.payoffs)

    def test_is_best_response(self):
        """Trivial player: is_best_response"""
        ok_(self.player.is_best_response(self.best_response_action, None))

    def test_best_response(self):
        """Trivial player: best_response"""
        eq_(self.player.best_response(None), self.best_response_action)

    def test_is_dominated(self):
        """Trivial player: is_dominated"""
        for action in range(self.player.num_actions):
            eq_(self.player.is_dominated(action),
                (action in self.dominated_actions))

    def test_dominated_actions(self):
        """Trivial player: dominated_actions"""
        eq_(self.player.dominated_actions(), self.dominated_actions)
class TestPlayer_0opponents:
    """Test for trivial Player with no opponent player"""
    def setup(self):
        """Setup a Player instance"""
        self.payoffs = [0, 1, -1]
        self.player = Player(self.payoffs)
        self.best_response_action = 1
        self.dominated_actions = [0, 2]

    def test_delete_action(self):
        N = self.player.num_opponents + 1
        actions_to_delete = [0, 2]
        actions_to_remain = \
            np.setdiff1d(np.arange(self.player.num_actions), actions_to_delete)
        for i in range(N):
            player_new = self.player.delete_action(actions_to_delete, i)
            assert_array_equal(
                player_new.payoff_array,
                self.player.payoff_array.take(actions_to_remain, axis=i))

    def test_payoff_vector(self):
        """Trivial player: payoff_vector"""
        assert_array_equal(self.player.payoff_vector(None), self.payoffs)

    def test_is_best_response(self):
        """Trivial player: is_best_response"""
        assert_(self.player.is_best_response(self.best_response_action, None))

    def test_best_response(self):
        """Trivial player: best_response"""
        assert_(self.player.best_response(None) == self.best_response_action)

    def test_is_dominated(self):
        """Trivial player: is_dominated"""
        for action in range(self.player.num_actions):
            assert_(
                self.player.is_dominated(action) == (
                    action in self.dominated_actions))

    def test_dominated_actions(self):
        """Trivial player: dominated_actions"""
        assert_(self.player.dominated_actions() == self.dominated_actions)
Exemple #25
0
def test_normalformgame_invalid_input_players_dtype_inconsistent():
    p0 = Player(np.zeros((2, 2), dtype=int))
    p1 = Player(np.zeros((2, 2), dtype=float))
    NormalFormGame([p0, p1])
Exemple #26
0
def test_normalformgame_invalid_input_players_num_inconsistent():
    p0 = Player(np.zeros((2, 2, 2)))
    p1 = Player(np.zeros((2, 2, 2)))
    NormalFormGame([p0, p1])
Exemple #27
0
def test_player_zero_actions():
    Player([[]])
Exemple #28
0
 def setUp(self):
     """Setup a Player instance"""
     self.payoffs = [[0, 1]]
     self.player = Player(self.payoffs)
def test_normalformgame_invalid_input_players_dtype_inconsistent():
    p0 = Player(np.zeros((2, 2), dtype=int))
    p1 = Player(np.zeros((2, 2), dtype=float))
    assert_raises(ValueError, NormalFormGame, [p0, p1])
def test_normalformgame_invalid_input_players_shape_inconsistent():
    p0 = Player(np.zeros((2, 3)))
    p1 = Player(np.zeros((2, 3)))
    assert_raises(ValueError, NormalFormGame, [p0, p1])
 def setUp(self):
     """Setup a Player instance"""
     payoffs = [0, 1]
     self.player = Player(payoffs)
Exemple #32
0
class TestPlayer_1opponent:
    """Test the methods of Player with one opponent player"""
    def setUp(self):
        """Setup a Player instance"""
        coordination_game_matrix = [[4, 0], [3, 2]]
        self.player = Player(coordination_game_matrix)

    def test_delete_action(self):
        N = self.player.num_opponents + 1
        action_to_delete = 0
        actions_to_remain = \
            np.setdiff1d(np.arange(self.player.num_actions), action_to_delete)
        for i in range(N):
            player_new = self.player.delete_action(action_to_delete, i)
            assert_array_equal(
                player_new.payoff_array,
                self.player.payoff_array.take(actions_to_remain, axis=i))

    def test_best_response_against_pure(self):
        eq_(self.player.best_response(1), 1)

    def test_best_response_against_mixed(self):
        eq_(self.player.best_response([1 / 2, 1 / 2]), 1)

    def test_best_response_list_when_tie(self):
        """best_response with tie_breaking=False"""
        assert_array_equal(
            sorted(
                self.player.best_response([2 / 3, 1 / 3], tie_breaking=False)),
            sorted([0, 1]))

    def test_best_response_with_random_tie_breaking(self):
        """best_response with tie_breaking='random'"""
        ok_(
            self.player.best_response([2 / 3, 1 /
                                       3], tie_breaking='random') in [0, 1])

        seed = 1234
        br0 = self.player.best_response([2 / 3, 1 / 3],
                                        tie_breaking='random',
                                        random_state=seed)
        br1 = self.player.best_response([2 / 3, 1 / 3],
                                        tie_breaking='random',
                                        random_state=seed)
        eq_(br0, br1)

    def test_best_response_with_smallest_tie_breaking(self):
        """best_response with tie_breaking='smallest' (default)"""
        eq_(self.player.best_response([2 / 3, 1 / 3]), 0)

    def test_best_response_with_payoff_perturbation(self):
        """best_response with payoff_perturbation"""
        eq_(
            self.player.best_response([2 / 3, 1 / 3],
                                      payoff_perturbation=[0, 0.1]), 1)
        eq_(
            self.player.best_response(
                [2, 1],  # int
                payoff_perturbation=[0, 0.1]),
            1)

    def test_is_best_response_against_pure(self):
        ok_(self.player.is_best_response(0, 0))

    def test_is_best_response_against_mixed(self):
        ok_(self.player.is_best_response([1 / 2, 1 / 2], [2 / 3, 1 / 3]))

    def test_is_dominated(self):
        for action in range(self.player.num_actions):
            for method in LP_METHODS:
                eq_(self.player.is_dominated(action, method=method), False)

    def test_dominated_actions(self):
        for method in LP_METHODS:
            eq_(self.player.dominated_actions(method=method), [])
Exemple #33
0
def test_player_corner_cases():
    n, m = 3, 4
    player = Player(np.zeros((n, m)))
    for action in range(n):
        eq_(player.is_best_response(action, [1 / m] * m), True)
        for method in LP_METHODS:
            eq_(player.is_dominated(action, method=method), False)

    e = 1e-8 * 2
    player = Player([[-e, -e], [1, -1], [-1, 1]])
    action = 0
    eq_(player.is_best_response(action, [1 / 2, 1 / 2], tol=e), True)
    eq_(player.is_best_response(action, [1 / 2, 1 / 2], tol=e / 2), False)
    for method in LP_METHODS:
        eq_(player.is_dominated(action, tol=2 * e, method=method), False)
        eq_(player.dominated_actions(tol=2 * e, method=method), [])

        eq_(player.is_dominated(action, tol=e / 2, method=method), True)
        eq_(player.dominated_actions(tol=e / 2, method=method), [action])
def test_player_corner_cases():
    n, m = 3, 4
    player = Player(np.zeros((n, m)))
    for action in range(n):
        assert_(player.is_best_response(action, [1 / m] * m))
        for method in LP_METHODS:
            assert_(not player.is_dominated(action, method=method))

    e = 1e-8 * 2
    player = Player([[-e, -e], [1, -1], [-1, 1]])
    action = 0
    assert_(player.is_best_response(action, [1 / 2, 1 / 2], tol=e))
    assert_(not player.is_best_response(action, [1 / 2, 1 / 2], tol=e / 2))
    for method in LP_METHODS:
        assert_(not player.is_dominated(action, tol=2 * e, method=method))
        assert_(player.dominated_actions(tol=2 * e, method=method) == [])

        assert_(player.is_dominated(action, tol=e / 2, method=method))
        assert_(player.dominated_actions(tol=e / 2, method=method) == [action])
 def setUp(self):
     """Setup a Player instance"""
     self.payoffs = [0, 1, -1]
     self.player = Player(self.payoffs)
     self.best_response_action = 1
     self.dominated_actions = [0, 2]
Exemple #36
0
 def setUp(self):
     """Setup a Player instance"""
     coordination_game_matrix = [[4, 0], [3, 2]]
     self.player = Player(coordination_game_matrix)
Exemple #37
0
 def setUp(self):
     """Setup a Player instance"""
     self.payoffs = [0, 1, -1]
     self.player = Player(self.payoffs)
     self.best_response_action = 1
     self.dominated_actions = [0, 2]
 def setUp(self):
     """Setup a Player instance"""
     coordination_game_matrix = [[4, 0], [3, 2]]
     self.player = Player(coordination_game_matrix)
Exemple #39
0
class TestPlayer_1opponent:
    """Test the methods of Player with one opponent player"""
    def setUp(self):
        """Setup a Player instance"""
        coordination_game_matrix = [[4, 0], [3, 2]]
        self.player = Player(coordination_game_matrix)

    def test_best_response_against_pure(self):
        eq_(self.player.best_response(1), 1)

    def test_best_response_against_mixed(self):
        eq_(self.player.best_response([1 / 2, 1 / 2]), 1)

    def test_best_response_list_when_tie(self):
        """best_response with tie_breaking=False"""
        assert_array_equal(
            sorted(
                self.player.best_response([2 / 3, 1 / 3], tie_breaking=False)),
            sorted([0, 1]))

    def test_best_response_with_random_tie_breaking(self):
        """best_response with tie_breaking='random'"""
        ok_(
            self.player.best_response([2 / 3, 1 /
                                       3], tie_breaking='random') in [0, 1])

        seed = 1234
        br0 = self.player.best_response([2 / 3, 1 / 3],
                                        tie_breaking='random',
                                        random_state=seed)
        br1 = self.player.best_response([2 / 3, 1 / 3],
                                        tie_breaking='random',
                                        random_state=seed)
        eq_(br0, br1)

    def test_best_response_with_smallest_tie_breaking(self):
        """best_response with tie_breaking='smallest' (default)"""
        eq_(self.player.best_response([2 / 3, 1 / 3]), 0)

    def test_best_response_with_payoff_perturbation(self):
        """best_response with payoff_perturbation"""
        eq_(
            self.player.best_response([2 / 3, 1 / 3],
                                      payoff_perturbation=[0, 0.1]), 1)
        eq_(
            self.player.best_response(
                [2, 1],  # int
                payoff_perturbation=[0, 0.1]),
            1)

    def test_is_best_response_against_pure(self):
        ok_(self.player.is_best_response(0, 0))

    def test_is_best_response_against_mixed(self):
        ok_(self.player.is_best_response([1 / 2, 1 / 2], [2 / 3, 1 / 3]))
Exemple #40
0
def test_normalformgame_invalid_input_players_shape_inconsistent():
    p0 = Player(np.zeros((2, 3)))
    p1 = Player(np.zeros((2, 3)))
    g = NormalFormGame([p0, p1])
class TestPlayer_1opponent:
    """Test the methods of Player with one opponent player"""

    def setUp(self):
        """Setup a Player instance"""
        coordination_game_matrix = [[4, 0], [3, 2]]
        self.player = Player(coordination_game_matrix)

    def test_delete_action(self):
        N = self.player.num_opponents + 1
        action_to_delete = 0
        actions_to_remain = \
            np.setdiff1d(np.arange(self.player.num_actions), action_to_delete)
        for i in range(N):
            player_new = self.player.delete_action(action_to_delete, i)
            assert_array_equal(
                player_new.payoff_array,
                self.player.payoff_array.take(actions_to_remain, axis=i)
            )

    def test_best_response_against_pure(self):
        eq_(self.player.best_response(1), 1)

    def test_best_response_against_mixed(self):
        eq_(self.player.best_response([1/2, 1/2]), 1)

    def test_best_response_list_when_tie(self):
        """best_response with tie_breaking=False"""
        assert_array_equal(
            sorted(self.player.best_response([2/3, 1/3], tie_breaking=False)),
            sorted([0, 1])
        )

    def test_best_response_with_random_tie_breaking(self):
        """best_response with tie_breaking='random'"""
        ok_(self.player.best_response([2/3, 1/3], tie_breaking='random')
            in [0, 1])

        seed = 1234
        br0 = self.player.best_response([2/3, 1/3], tie_breaking='random',
                                        random_state=seed)
        br1 = self.player.best_response([2/3, 1/3], tie_breaking='random',
                                        random_state=seed)
        eq_(br0, br1)

    def test_best_response_with_smallest_tie_breaking(self):
        """best_response with tie_breaking='smallest' (default)"""
        eq_(self.player.best_response([2/3, 1/3]), 0)

    def test_best_response_with_payoff_perturbation(self):
        """best_response with payoff_perturbation"""
        eq_(self.player.best_response([2/3, 1/3],
                                      payoff_perturbation=[0, 0.1]),
            1)
        eq_(self.player.best_response([2, 1],  # int
                                      payoff_perturbation=[0, 0.1]),
            1)

    def test_is_best_response_against_pure(self):
        ok_(self.player.is_best_response(0, 0))

    def test_is_best_response_against_mixed(self):
        ok_(self.player.is_best_response([1/2, 1/2], [2/3, 1/3]))

    def test_is_dominated(self):
        for action in range(self.player.num_actions):
            for method in LP_METHODS:
                eq_(self.player.is_dominated(action, method=method), False)

    def test_dominated_actions(self):
        for method in LP_METHODS:
            eq_(self.player.dominated_actions(method=method), [])