Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NormalFormGame: Add payoff_arrays attribute #382

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quantecon/game_theory/lemke_howson.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def lemke_howson(g, init_pivot=0, max_iter=10**6, capping=None,
if N != 2:
raise NotImplementedError('Implemented only for 2-player games')

payoff_matrices = tuple(g.players[i].payoff_array for i in range(N))
payoff_matrices = g.payoff_arrays
nums_actions = g.nums_actions
total_num = sum(nums_actions)

Expand Down
6 changes: 6 additions & 0 deletions quantecon/game_theory/normal_form_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ class NormalFormGame:
nums_actions : tuple(int)
Tuple of the numbers of actions, one for each player.

payoff_arrays : tuple(ndarray(float, ndim=N))
Tuple of the payoff arrays, one for each player.

"""
def __init__(self, data, dtype=None):
# data represents an array_like of Players
Expand Down Expand Up @@ -575,6 +578,9 @@ def __init__(self, data, dtype=None):
self.nums_actions = tuple(
player.num_actions for player in self.players
)
self.payoff_arrays = tuple(
player.payoff_array for player in self.players
)

@property
def payoff_profile_array(self):
Expand Down
28 changes: 19 additions & 9 deletions quantecon/game_theory/tests/test_normal_form_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,28 @@ class TestNormalFormGame_Asym2p:

def setUp(self):
"""Setup a NormalFormGame instance"""
matching_pennies_bimatrix = [[(1, -1), (-1, 1)],
[(-1, 1), (1, -1)]]
self.g = NormalFormGame(matching_pennies_bimatrix)
self.BoS_bimatrix = np.array([[(3, 2), (1, 1)],
[(0, 0), (2, 3)]])
self.g = NormalFormGame(self.BoS_bimatrix)

def test_getitem(self):
assert_array_equal(self.g[1, 0], [-1, 1])
action_profile = (1, 0)
assert_array_equal(self.g[action_profile],
self.BoS_bimatrix[action_profile])

def test_is_nash_against_pure(self):
ok_(not self.g.is_nash((0, 0)))
def test_is_nash_pure(self):
ok_(not self.g.is_nash((1, 0)))

def test_is_nash_mixed(self):
ok_(self.g.is_nash(([3/4, 1/4], [1/4, 3/4])))

def test_is_nash_against_mixed(self):
ok_(self.g.is_nash(([1/2, 1/2], [1/2, 1/2])))
def test_payoff_arrays(self):
assert_array_equal(
self.g.payoff_arrays[0], self.BoS_bimatrix[:, :, 0]
)
assert_array_equal(
self.g.payoff_arrays[1], self.BoS_bimatrix[:, :, 1].T
)


class TestNormalFormGame_3p:
Expand Down Expand Up @@ -349,7 +359,7 @@ def test_normalformgame_setitem_1p():
eq_(g.players[0].payoff_array[0], 10)


# Test __repre__ #
# Test __repr__ #

def test_player_repr():
nums_actions = (2, 3, 4)
Expand Down