Skip to content

Commit

Permalink
Merge pull request #443 from QuantEcon/fix-0-num_actions
Browse files Browse the repository at this point in the history
FIX: Disallow Player with 0 actions
  • Loading branch information
mmcky authored Nov 5, 2018
2 parents 13f49fa + 281ce2c commit 3bb4d7d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions quantecon/game_theory/normal_form_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ def __init__(self, payoff_array):

if self.payoff_array.ndim == 0:
raise ValueError('payoff_array must be an array_like')
if np.prod(self.payoff_array.shape) == 0:
raise ValueError('every player must have at least one action')

self.num_opponents = self.payoff_array.ndim - 1
self.num_actions = self.payoff_array.shape[0]
Expand Down Expand Up @@ -418,6 +420,8 @@ def is_dominated(self, action, tol=None, method=None):
ind[action] = False
D = payoff_array[ind]
D -= payoff_array[action]
if D.shape[0] == 0: # num_actions == 1
return False
if self.num_opponents >= 2:
D.shape = (D.shape[0], np.prod(D.shape[1:]))

Expand Down
28 changes: 28 additions & 0 deletions quantecon/game_theory/tests/test_normal_form_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,24 @@ def test_normalformgame_setitem_1p():
eq_(g.players[0].payoff_array[0], 10)


# Trivial cases with one action #

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), [])


# Test __repr__ #

def test_player_repr():
Expand All @@ -393,6 +411,11 @@ def test_player_repr():

# Invalid inputs #

@raises(ValueError)
def test_player_zero_actions():
p = Player([[]])


@raises(ValueError)
def test_normalformgame_invalid_input_players_shape_inconsistent():
p0 = Player(np.zeros((2, 3)))
Expand Down Expand Up @@ -424,6 +447,11 @@ def test_normalformgame_invalid_input_payoff_profiles():
g = NormalFormGame(np.zeros((2, 2, 1)))


@raises(ValueError)
def test_normalformgame_zero_actions():
g = NormalFormGame((2, 0))


# Utility functions #

def test_pure2mixed():
Expand Down

0 comments on commit 3bb4d7d

Please sign in to comment.