Skip to content

Commit

Permalink
Merge pull request #382 from QuantEcon/payoff_arrays
Browse files Browse the repository at this point in the history
NormalFormGame: Add `payoff_arrays` attribute
  • Loading branch information
mmcky authored Jan 9, 2018
2 parents e61702f + e439ba8 commit d73c959
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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

0 comments on commit d73c959

Please sign in to comment.