X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fdunshire%2Fgames.py;h=05ab58cd702c5570221184ed47f5bb16289627c0;hb=769e1c7d09936a617f33d1496782fbbd4299851d;hp=3d4b09ad8c0c12ff1bba0294a1c5c87ae8ff72bf;hpb=b701403c21a795ff7033cab5a753807c181517b1;p=dunshire.git diff --git a/src/dunshire/games.py b/src/dunshire/games.py index 3d4b09a..05ab58c 100644 --- a/src/dunshire/games.py +++ b/src/dunshire/games.py @@ -12,11 +12,11 @@ from unittest import TestCase # These are mostly actually needed. from cvxopt import matrix, printing, solvers -from cones import CartesianProduct, IceCream, NonnegativeOrthant -from errors import GameUnsolvableException -from matrices import (append_col, append_row, eigenvalues_re, identity, - inner_product, norm) -import options +from .cones import CartesianProduct, IceCream, NonnegativeOrthant +from .errors import GameUnsolvableException +from .matrices import (append_col, append_row, eigenvalues_re, identity, + inner_product, norm) +from . import options printing.options['dformat'] = options.FLOAT_FORMAT solvers.options['show_progress'] = options.VERBOSE @@ -211,7 +211,6 @@ class SymmetricLinearGame: Examples -------- - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(3) >>> L = [[1,-5,-15],[-1,2,-3],[-12,-15,1]] >>> e1 = [1,1,1] @@ -232,7 +231,6 @@ class SymmetricLinearGame: Lists can (and probably should) be used for every argument:: - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(2) >>> L = [[1,0],[0,1]] >>> e1 = [1,1] @@ -254,7 +252,6 @@ class SymmetricLinearGame: >>> import cvxopt >>> import numpy - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(2) >>> L = [[1,0],[0,1]] >>> e1 = cvxopt.matrix([1,1]) @@ -275,7 +272,6 @@ class SymmetricLinearGame: otherwise indexed by columns:: >>> import cvxopt - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(2) >>> L = [[1,2],[3,4]] >>> e1 = [1,1] @@ -364,7 +360,6 @@ class SymmetricLinearGame: This example is computed in Gowda and Ravindran in the section "The value of a Z-transformation":: - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(3) >>> L = [[1,-5,-15],[-1,2,-3],[-12,-15,1]] >>> e1 = [1,1,1] @@ -384,7 +379,6 @@ class SymmetricLinearGame: The value of the following game can be computed using the fact that the identity is invertible:: - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(3) >>> L = [[1,0,0],[0,1,0],[0,0,1]] >>> e1 = [1,2,3] @@ -475,7 +469,6 @@ class SymmetricLinearGame: Examples -------- - >>> from cones import NonnegativeOrthant >>> K = NonnegativeOrthant(3) >>> L = [[1,-5,-15],[-1,2,-3],[-12,-15,1]] >>> e1 = [1,1,1] @@ -545,10 +538,10 @@ def _random_skew_symmetric_matrix(dims): """ strict_ut = [[uniform(-10, 10)*int(i < j) for i in range(dims)] - for j in range(dims)] + for j in range(dims)] - strict_ut = matrix(strict_ut, (dims,dims)) - return (strict_ut - strict_ut.trans()) + strict_ut = matrix(strict_ut, (dims, dims)) + return strict_ut - strict_ut.trans() def _random_lyapunov_like_icecream(dims): @@ -556,12 +549,12 @@ def _random_lyapunov_like_icecream(dims): Generate a random Lyapunov-like matrix over the ice-cream cone in ``dims`` dimensions. """ - a = matrix([uniform(-10,10)], (1,1)) - b = matrix([uniform(-10,10) for idx in range(dims-1)], (dims-1, 1)) + a = matrix([uniform(-10, 10)], (1, 1)) + b = matrix([uniform(-10, 10) for idx in range(dims-1)], (dims-1, 1)) D = _random_skew_symmetric_matrix(dims-1) + a*identity(dims-1) row1 = append_col(a, b.trans()) row2 = append_col(b, D) - return append_row(row1,row2) + return append_row(row1, row2) def _random_orthant_params(): @@ -606,7 +599,8 @@ def _random_icecream_params(): return (L, K, matrix(e1), matrix(e2)) -class SymmetricLinearGameTest(TestCase): +# Tell pylint to shut up about the large number of methods. +class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904 """ Tests for the SymmetricLinearGame and Solution classes. """ @@ -849,9 +843,7 @@ class SymmetricLinearGameTest(TestCase): This test theoretically applies to the ice-cream cone as well, but we don't know how to make positive operators on that cone. """ - (_, K, e1, e2) = _random_orthant_params() - - # Ignore that L, we need a nonnegative one. + (K, e1, e2) = _random_orthant_params()[1:] L = _random_nonnegative_matrix(K.dimension()) game = SymmetricLinearGame(L, K, e1, e2) @@ -868,14 +860,15 @@ class SymmetricLinearGameTest(TestCase): # We only check for positive/negative stability if the game # value is not basically zero. If the value is that close to # zero, we just won't check any assertions. + eigs = eigenvalues_re(L) if soln.game_value() > options.ABS_TOL: # L should be positive stable - ps = all([eig > -options.ABS_TOL for eig in eigenvalues_re(L)]) - self.assertTrue(ps) + positive_stable = all([eig > -options.ABS_TOL for eig in eigs]) + self.assertTrue(positive_stable) elif soln.game_value() < -options.ABS_TOL: # L should be negative stable - ns = all([eig < options.ABS_TOL for eig in eigenvalues_re(L)]) - self.assertTrue(ns) + negative_stable = all([eig < options.ABS_TOL for eig in eigs]) + self.assertTrue(negative_stable) # The dual game's value should always equal the primal's. dualsoln = game.dual().solution() @@ -886,10 +879,7 @@ class SymmetricLinearGameTest(TestCase): """ Test that a Lyapunov game on the nonnegative orthant works. """ - (L, K, e1, e2) = _random_orthant_params() - - # Ignore that L, we need a diagonal (Lyapunov-like) one. - # (And we don't need to transpose those.) + (K, e1, e2) = _random_orthant_params()[1:] L = _random_diagonal_matrix(K.dimension()) self.assert_lyapunov_works(L, K, e1, e2) @@ -899,10 +889,7 @@ class SymmetricLinearGameTest(TestCase): """ Test that a Lyapunov game on the ice-cream cone works. """ - (L, K, e1, e2) = _random_icecream_params() - - # Ignore that L, we need a diagonal (Lyapunov-like) one. - # (And we don't need to transpose those.) + (K, e1, e2) = _random_icecream_params()[1:] L = _random_lyapunov_like_icecream(K.dimension()) self.assert_lyapunov_works(L, K, e1, e2)