From 9938be8d5ae191f9ab03257b8cb9d3bff155d4d1 Mon Sep 17 00:00:00 2001 From: QBatista Date: Mon, 7 Aug 2017 16:27:04 +0900 Subject: [PATCH 1/2] TEST: Add tests for lemke_howson.py --- .../game_theory/tests/test_lemke_howson.py | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/quantecon/game_theory/tests/test_lemke_howson.py b/quantecon/game_theory/tests/test_lemke_howson.py index 6a58aecaf..4f6146214 100644 --- a/quantecon/game_theory/tests/test_lemke_howson.py +++ b/quantecon/game_theory/tests/test_lemke_howson.py @@ -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 @@ -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 From b24128db29150fe02fb12f87dc0590668b7d1109 Mon Sep 17 00:00:00 2001 From: QBatista Date: Mon, 7 Aug 2017 16:28:12 +0900 Subject: [PATCH 2/2] FIX: Add `raise TypeError` when init_pivot type is not int --- quantecon/game_theory/lemke_howson.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/quantecon/game_theory/lemke_howson.py b/quantecon/game_theory/lemke_howson.py index 38578a529..93f956041 100644 --- a/quantecon/game_theory/lemke_howson.py +++ b/quantecon/game_theory/lemke_howson.py @@ -5,6 +5,7 @@ Lemke-Howson algorithm. """ +import numbers import numpy as np from numba import jit from .utilities import NashResult @@ -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