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

FIX: Disallow Player with 0 actions #443

Merged
merged 2 commits into from
Nov 5, 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
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