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

Test lemke howson exceptions #323

Merged
merged 2 commits into from
Aug 17, 2017
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
12 changes: 8 additions & 4 deletions quantecon/game_theory/lemke_howson.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Lemke-Howson algorithm.

"""
import numbers
import numpy as np
from numba import jit
from .utilities import NashResult
Expand Down Expand Up @@ -128,11 +129,14 @@ def lemke_howson(g, init_pivot=0, max_iter=10**6, capping=None,
nums_actions = g.nums_actions
total_num = sum(nums_actions)

msg = '`init_pivot` must be an integer k' + \
'such that 0 <= k < {0}'.format(total_num)

if not isinstance(init_pivot, numbers.Integral):
raise TypeError(msg)

if not (0 <= init_pivot < total_num):
raise ValueError(
'`init_pivot` must be an integer k such that 0 <= k < {0}'
.format(total_num)
)
raise ValueError(msg)

if capping is None:
capping = max_iter
Expand Down
28 changes: 27 additions & 1 deletion quantecon/game_theory/tests/test_lemke_howson.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import numpy as np
from numpy.testing import assert_allclose
from nose.tools import eq_
from nose.tools import eq_, raises
from quantecon.game_theory import Player, NormalFormGame, lemke_howson


Expand Down Expand Up @@ -110,6 +110,32 @@ def test_lemke_howson_capping():
eq_(res.init, init_pivot-1)


@raises(TypeError)
def test_lemke_howson_invalid_g():
bimatrix = [[(3, 3), (3, 2)],
[(2, 2), (5, 6)],
[(0, 3), (6, 1)]]
lemke_howson(bimatrix)


@raises(ValueError)
def test_lemke_howson_invalid_init_pivot_integer():
bimatrix = [[(3, 3), (3, 2)],
[(2, 2), (5, 6)],
[(0, 3), (6, 1)]]
g = NormalFormGame(bimatrix)
lemke_howson(g, -1)


@raises(TypeError)
def test_lemke_howson_invalid_init_pivot_float():
bimatrix = [[(3, 3), (3, 2)],
[(2, 2), (5, 6)],
[(0, 3), (6, 1)]]
g = NormalFormGame(bimatrix)
lemke_howson(g, 1.0)


if __name__ == '__main__':
import sys
import nose
Expand Down