from cvxopt import matrix, printing, solvers
from cones import CartesianProduct, IceCream, NonnegativeOrthant
from errors import GameUnsolvableException
-from matrices import append_col, append_row, identity, inner_product, norm
+from matrices import (append_col, append_row, eigenvalues_re, identity,
+ inner_product, norm)
import options
printing.options['dformat'] = options.FLOAT_FORMAT
-def _random_square_matrix(dims):
+def _random_matrix(dims):
"""
Generate a random square (``dims``-by-``dims``) matrix,
represented as a list of rows. This is used only by the
"""
return [[uniform(-10, 10) for i in range(dims)] for j in range(dims)]
+def _random_nonnegative_matrix(dims):
+ """
+ Generate a random square (``dims``-by-``dims``) matrix with
+ nonnegative entries, represented as a list of rows. This is used
+ only by the :class:`SymmetricLinearGameTest` class.
+ """
+ L = _random_matrix(dims)
+ return [[abs(entry) for entry in row] for row in L]
+
+def _random_diagonal_matrix(dims):
+ """
+ Generate a random square (``dims``-by-``dims``) matrix with nonzero
+ entries only on the diagonal, represented as a list of rows. This is
+ used only by the :class:`SymmetricLinearGameTest` class.
+ """
+ return [[uniform(-10, 10)*int(i == j) for i in range(dims)]
+ for j in range(dims)]
def _random_orthant_params():
"""
K = NonnegativeOrthant(ambient_dim)
e1 = [uniform(0.5, 10) for idx in range(K.dimension())]
e2 = [uniform(0.5, 10) for idx in range(K.dimension())]
- L = _random_square_matrix(K.dimension())
+ L = _random_matrix(K.dimension())
return (L, K, e1, e2)
fudge_factor = 1.0 / (2.0*sqrt(K.dimension() - 1.0))
e1 += [fudge_factor*uniform(0, 1) for idx in range(K.dimension() - 1)]
e2 += [fudge_factor*uniform(0, 1) for idx in range(K.dimension() - 1)]
- L = _random_square_matrix(K.dimension())
+ L = _random_matrix(K.dimension())
return (L, K, e1, e2)
"""
(L, K, e1, e2) = _random_icecream_params()
self.assert_opposite_game_works(L, K, e1, e2)
+
+
+ def assert_orthogonality(self, L, K, e1, e2):
+ """
+ Two orthogonality relations hold at an optimal solution, and we
+ check them here.
+ """
+ game = SymmetricLinearGame(L, K, e1, e2)
+ soln = game.solution()
+ x_bar = soln.player1_optimal()
+ y_bar = soln.player2_optimal()
+ value = soln.game_value()
+
+ # Make these matrices so that we can compute with them.
+ L = matrix(L).trans()
+ e1 = matrix(e1, (K.dimension(), 1))
+ e2 = matrix(e2, (K.dimension(), 1))
+
+ ip1 = inner_product(y_bar, L*x_bar - value*e1)
+ self.assert_within_tol(ip1, 0)
+
+ ip2 = inner_product(value*e2 - L.trans()*y_bar, x_bar)
+ self.assert_within_tol(ip2, 0)
+
+
+ def test_orthogonality_orthant(self):
+ """
+ Check the orthgonality relationships that hold for a solution
+ over the nonnegative orthant.
+ """
+ (L, K, e1, e2) = _random_orthant_params()
+ self.assert_orthogonality(L, K, e1, e2)
+
+
+ def test_orthogonality_icecream(self):
+ """
+ Check the orthgonality relationships that hold for a solution
+ over the ice-cream cone.
+ """
+ (L, K, e1, e2) = _random_icecream_params()
+ self.assert_orthogonality(L, K, e1, e2)
+
+
+ def test_positive_operator_value(self):
+ """
+ Test that a positive operator on the nonnegative orthant gives
+ rise to a a game with a nonnegative value.
+
+ 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.
+ L = _random_nonnegative_matrix(K.dimension())
+
+ game = SymmetricLinearGame(L, K, e1, e2)
+ self.assertTrue(game.solution().game_value() >= -options.ABS_TOL)
+
+ def test_lyapunov_orthant(self):
+ """
+ Test that a Lyapunov game on the nonnegative orthant works.
+ """
+ (_, K, e1, e2) = _random_orthant_params()
+
+ # Ignore that L, we need a diagonal (Lyapunov-like) one.
+ L = _random_diagonal_matrix(K.dimension())
+ game = SymmetricLinearGame(L, K, e1, e2)
+ soln = game.solution()
+
+ # 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.
+ L = matrix(L).trans()
+ 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)
+ 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)