]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - test/randomgen.py
Put the MAX_COND at 150 for now... until it causes problems.
[dunshire.git] / test / randomgen.py
index 395408c6626ec4ee48dcb95e3b2e684daea88f4a..76d5f7874b38a7c0d45d2f57355be29053409af2 100644 (file)
@@ -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 = 150
 """
-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.
 """
 
 RANDOM_MAX = 10
@@ -314,11 +318,17 @@ def random_orthant_game():
     SymmetricLinearGame
         A random game over some nonnegative orthant.
 
+    Examples
+    --------
+
+        >>> random_orthant_game()
+        <dunshire.games.SymmetricLinearGame object at 0x...>
+
     """
     ambient_dim = random_natural() + 1
     K = NonnegativeOrthant(ambient_dim)
-    e1 = [random_nn_scalar() for _ in range(K.dimension())]
-    e2 = [random_nn_scalar() for _ in range(K.dimension())]
+    e1 = [0.1 + random_nn_scalar() for _ in range(K.dimension())]
+    e2 = [0.1 + random_nn_scalar() for _ in range(K.dimension())]
     L = random_matrix(K.dimension())
     G = SymmetricLinearGame(L, K, e1, e2)
 
@@ -343,6 +353,12 @@ def random_icecream_game():
     SymmetricLinearGame
         A random game over some ice-cream cone.
 
+    Examples
+    --------
+
+        >>> random_icecream_game()
+        <dunshire.games.SymmetricLinearGame object at 0x...>
+
     """
     # Use a minimum dimension of two to avoid divide-by-zero in
     # the fudge factor we make up later.
@@ -387,18 +403,24 @@ def random_ll_orthant_game():
         A random game over some nonnegative orthant whose ``payoff`` method
         is based on a Lyapunov-like ``L`` operator.
 
+    Examples
+    --------
+
+        >>> random_ll_orthant_game()
+        <dunshire.games.SymmetricLinearGame object at 0x...>
+
     """
     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
 
@@ -419,18 +441,24 @@ def random_ll_icecream_game():
         A random game over some ice-cream cone whose ``payoff`` method
         is based on a Lyapunov-like ``L`` operator.
 
+    Examples
+    --------
+
+        >>> random_ll_icecream_game()
+        <dunshire.games.SymmetricLinearGame object at 0x...>
+
     """
     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
 
@@ -452,19 +480,25 @@ def random_positive_orthant_game():
         A random game over some nonnegative orthant whose ``payoff`` method
         is based on a positive ``L`` operator.
 
+    Examples
+    --------
+
+        >>> random_positive_orthant_game()
+        <dunshire.games.SymmetricLinearGame object at 0x...>
+
     """
 
     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
 
@@ -487,14 +521,30 @@ def random_nn_scaling(G):
     (float, SymmetricLinearGame)
         A pair containing the both the scaling factor and the new scaled game.
 
+    Examples
+    --------
+
+        >>> from dunshire.matrices import norm
+        >>> from dunshire.options import ABS_TOL
+        >>> G = random_orthant_game()
+        >>> (alpha, H) = random_nn_scaling(G)
+        >>> alpha >= 0
+        True
+        >>> G.K() == H.K()
+        True
+        >>> norm(G.e1() - H.e1()) < ABS_TOL
+        True
+        >>> 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)
 
@@ -518,16 +568,30 @@ def random_translation(G):
         A pair containing the both the translation distance and the new
         scaled game.
 
+    Examples
+    --------
+
+        >>> from dunshire.matrices import norm
+        >>> from dunshire.options import ABS_TOL
+        >>> G = random_orthant_game()
+        >>> (alpha, H) = random_translation(G)
+        >>> G.K() == H.K()
+        True
+        >>> norm(G.e1() - H.e1()) < ABS_TOL
+        True
+        >>> 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)