X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=test%2Frandomgen.py;h=3d6484a7d280385c88ae778eecce8f02b310f1c0;hb=bf9cec19830acdf93bb0f5ce0a38a9f57f0db629;hp=97dd0b93157a7b534c7cb4c6acb5de1740de661e;hpb=2399e11170c25f5af276f8c9c3140bbc1f579474;p=dunshire.git diff --git a/test/randomgen.py b/test/randomgen.py index 97dd0b9..3d6484a 100644 --- a/test/randomgen.py +++ b/test/randomgen.py @@ -9,9 +9,13 @@ from dunshire.cones import NonnegativeOrthant, IceCream from dunshire.games import SymmetricLinearGame from dunshire.matrices import (append_col, append_row, identity) -MAX_COND = 250 +MAX_COND = 125 """ -The maximum condition number of a randomly-generated game. +The maximum condition number of a randomly-generated game. When the +condition number of the games gets too high, we start to see +:class:`PoorScalingException` being thrown. There's no science to +choosing the upper bound -- it got lowered until those exceptions +stopped popping up. It's at ``125`` because ``129`` doesn't work. """ RANDOM_MAX = 10 @@ -29,8 +33,8 @@ def random_scalar(): ------- float - A random real number between ``-RANDOM_MAX`` and ``RANDOM_MAX``, - inclusive. + A random real number between negative and positive + :const:`RANDOM_MAX`, inclusive. Examples -------- @@ -50,8 +54,8 @@ def random_nn_scalar(): ------- float - A random nonnegative real number between zero and ``RANDOM_MAX``, - inclusive. + A random nonnegative real number between zero and + :const:`RANDOM_MAX`, inclusive. Examples -------- @@ -71,7 +75,8 @@ def random_natural(): ------- int - A random natural number between ``1`` and ``RANDOM_MAX`` inclusive. + A random natural number between ``1`` and :const:`RANDOM_MAX`, + inclusive. Examples -------- @@ -101,8 +106,8 @@ def random_matrix(row_count, column_count=None): ------- matrix - A new matrix whose entries are random floats chosen uniformly from - the interval ``[-RANDOM_MAX, RANDOM_MAX]``. + A new matrix whose entries are random floats chosen uniformly + between negative and positive :const:`RANDOM_MAX`. Examples -------- @@ -184,7 +189,7 @@ def random_diagonal_matrix(dims): matrix A new matrix whose diagonal entries are random floats chosen - using func:`random_scalar` and whose off-diagonal entries are + using :func:`random_scalar` and whose off-diagonal entries are zero. Examples @@ -267,7 +272,7 @@ def random_lyapunov_like_icecream(dims): matrix A new matrix, Lyapunov-like on the ice-cream cone in ``dims`` dimensions, whose free entries are random floats chosen uniformly - from the interval ``[-RANDOM_MAX, RANDOM_MAX]``. + between negative and positive :const:`RANDOM_MAX`. References ---------- @@ -306,7 +311,7 @@ def random_orthant_game(): We generate each of ``L``, ``K``, ``e1``, and ``e2`` randomly within the constraints of the nonnegative orthant, and then construct a game from them. The process is repeated until we generate a game with - a condition number under ``MAX_COND``. + a condition number under :const:`MAX_COND`. Returns ------- @@ -341,7 +346,7 @@ def random_icecream_game(): We generate each of ``L``, ``K``, ``e1``, and ``e2`` randomly within the constraints of the ice-cream cone, and then construct a game from them. The process is repeated until we generate a game with a - condition number under ``MAX_COND``. + condition number under :const:`MAX_COND`. Returns ------- @@ -382,6 +387,36 @@ def random_icecream_game(): return random_icecream_game() +def random_game(): + """ + Return a random game. + + One of the functions, + + 1. :func:`random_orthant_game` + 2. :func:`random_icecream_game` + + is chosen at random and used to generate a random game. + + Returns + ------- + + SymmetricLinearGame + A random game. + + Examples + -------- + + >>> random_game() + + + """ + cone_type = randint(0,1) + if cone_type == 0: + return random_orthant_game() + elif cone_type == 1: + return random_icecream_game() + def random_ll_orthant_game(): """ Return a random Lyapunov game over some nonnegative orthant. @@ -390,14 +425,17 @@ def random_ll_orthant_game(): to have a :func:`random_diagonal_matrix` as its operator. Such things are Lyapunov-like on the nonnegative orthant. That process is repeated until the condition number of the resulting game is within - ``MAX_COND``. + :const:`MAX_COND`. Returns ------- SymmetricLinearGame - A random game over some nonnegative orthant whose ``payoff`` method - is based on a Lyapunov-like ``L`` operator. + + A random game over some nonnegative orthant whose + :meth:`dunshire.games.SymmetricLinearGame.payoff` method is + based on a Lyapunov-like + :meth:`dunshire.games.SymmetricLinearGame.L` operator. Examples -------- @@ -407,16 +445,16 @@ def random_ll_orthant_game(): """ G = random_orthant_game() - L = random_diagonal_matrix(G._K.dimension()) + L = random_diagonal_matrix(G.dimension()) # Replace the totally-random ``L`` with random Lyapunov-like one. - G = SymmetricLinearGame(L, G._K, G._e1, G._e2) + G = SymmetricLinearGame(L, G.K(), G.e1(), G.e2()) while G.condition() > MAX_COND: # Try again until the condition number is satisfactory. G = random_orthant_game() - L = random_diagonal_matrix(G._K.dimension()) - G = SymmetricLinearGame(L, G._K, G._e1, G._e2) + L = random_diagonal_matrix(G.dimension()) + G = SymmetricLinearGame(L, G.K(), G.e1(), G.e2()) return G @@ -428,14 +466,16 @@ def random_ll_icecream_game(): We first construct a :func:`random_icecream_game` and then modify it to have a :func:`random_lyapunov_like_icecream` operator. That process is repeated until the condition number of the resulting game - is within ``MAX_COND``. + is within :const:`MAX_COND`. Returns ------- SymmetricLinearGame - A random game over some ice-cream cone whose ``payoff`` method - is based on a Lyapunov-like ``L`` operator. + A random game over some ice-cream cone whose + :meth:`dunshire.games.SymmetricLinearGame.payoff` method + is based on a Lyapunov-like + :meth:`dunshire.games.SymmetricLinearGame.L` operator. Examples -------- @@ -445,16 +485,16 @@ def random_ll_icecream_game(): """ G = random_icecream_game() - L = random_lyapunov_like_icecream(G._K.dimension()) + L = random_lyapunov_like_icecream(G.dimension()) # Replace the totally-random ``L`` with random Lyapunov-like one. - G = SymmetricLinearGame(L, G._K, G._e1, G._e2) + G = SymmetricLinearGame(L, G.K(), G.e1(), G.e2()) while G.condition() > MAX_COND: # Try again until the condition number is satisfactory. G = random_icecream_game() - L = random_lyapunov_like_icecream(G._K.dimension()) - G = SymmetricLinearGame(L, G._K, G._e1, G._e2) + L = random_lyapunov_like_icecream(G.dimension()) + G = SymmetricLinearGame(L, G.K(), G.e1(), G.e2()) return G @@ -467,14 +507,16 @@ def random_positive_orthant_game(): We first construct a :func:`random_orthant_game` and then modify it to have a :func:`random_nonnegative_matrix` as its operator. That process is repeated until the condition number of the resulting game - is within ``MAX_COND``. + is within :const:`MAX_COND`. Returns ------- SymmetricLinearGame - A random game over some nonnegative orthant whose ``payoff`` method - is based on a positive ``L`` operator. + A random game over some nonnegative orthant whose + :meth:`dunshire.games.SymmetricLinearGame.payoff` method + is based on a positive + :meth:`dunshire.games.SymmetricLinearGame.L` operator. Examples -------- @@ -485,16 +527,16 @@ def random_positive_orthant_game(): """ G = random_orthant_game() - L = random_nonnegative_matrix(G._K.dimension()) + L = random_nonnegative_matrix(G.dimension()) # Replace the totally-random ``L`` with the random nonnegative one. - G = SymmetricLinearGame(L, G._K, G._e1, G._e2) + G = SymmetricLinearGame(L, G.K(), G.e1(), G.e2()) while G.condition() > MAX_COND: # Try again until the condition number is satisfactory. G = random_orthant_game() - L = random_nonnegative_matrix(G._K.dimension()) - G = SymmetricLinearGame(L, G._K, G._e1, G._e2) + L = random_nonnegative_matrix(G.dimension()) + G = SymmetricLinearGame(L, G.K(), G.e1(), G.e2()) return G @@ -526,21 +568,21 @@ def random_nn_scaling(G): >>> (alpha, H) = random_nn_scaling(G) >>> alpha >= 0 True - >>> G._K == H._K + >>> G.K() == H.K() True - >>> norm(G._e1 - H._e1) < ABS_TOL + >>> norm(G.e1() - H.e1()) < ABS_TOL True - >>> norm(G._e2 - H._e2) < ABS_TOL + >>> norm(G.e2() - H.e2()) < ABS_TOL True """ alpha = random_nn_scalar() - H = SymmetricLinearGame(alpha*G._L.trans(), G._K, G._e1, G._e2) + H = SymmetricLinearGame(alpha*G.L().trans(), G.K(), G.e1(), G.e2()) while H.condition() > MAX_COND: # Loop until the condition number of H doesn't suck. alpha = random_nn_scalar() - H = SymmetricLinearGame(alpha*G._L.trans(), G._K, G._e1, G._e2) + H = SymmetricLinearGame(alpha*G.L().trans(), G.K(), G.e1(), G.e2()) return (alpha, H) @@ -571,23 +613,23 @@ def random_translation(G): >>> from dunshire.options import ABS_TOL >>> G = random_orthant_game() >>> (alpha, H) = random_translation(G) - >>> G._K == H._K + >>> G.K() == H.K() True - >>> norm(G._e1 - H._e1) < ABS_TOL + >>> norm(G.e1() - H.e1()) < ABS_TOL True - >>> norm(G._e2 - H._e2) < ABS_TOL + >>> norm(G.e2() - H.e2()) < ABS_TOL True """ alpha = random_scalar() - tensor_prod = G._e1 * G._e2.trans() - M = G._L + alpha*tensor_prod + tensor_prod = G.e1() * G.e2().trans() + M = G.L() + alpha*tensor_prod - H = SymmetricLinearGame(M.trans(), G._K, G._e1, G._e2) + H = SymmetricLinearGame(M.trans(), G.K(), G.e1(), G.e2()) while H.condition() > MAX_COND: # Loop until the condition number of H doesn't suck. alpha = random_scalar() - M = G._L + alpha*tensor_prod - H = SymmetricLinearGame(M.trans(), G._K, G._e1, G._e2) + M = G.L() + alpha*tensor_prod + H = SymmetricLinearGame(M.trans(), G.K(), G.e1(), G.e2()) return (alpha, H)