+
+
+ def assert_scaling_works(self, L, K, e1, e2):
+ """
+ Test that scaling ``L`` by a nonnegative number scales the value
+ of the game by the same number.
+ """
+ game1 = SymmetricLinearGame(L, K, e1, e2)
+ value1 = game1.solution().game_value()
+
+ alpha = uniform(0.1, 10)
+ game2 = SymmetricLinearGame(alpha*L, K, e1, e2)
+ value2 = game2.solution().game_value()
+ self.assert_within_tol(alpha*value1, value2)
+
+
+ def test_scaling_orthant(self):
+ """
+ Test that scaling ``L`` by a nonnegative number scales the value
+ of the game by the same number over the nonnegative orthant.
+ """
+ (L, K, e1, e2) = _random_orthant_params()
+ self.assert_scaling_works(L, K, e1, e2)
+
+
+ def test_scaling_icecream(self):
+ """
+ The same test as :meth:`test_nonnegative_scaling_orthant`,
+ except over the ice cream cone.
+ """
+ (L, K, e1, e2) = _random_icecream_params()
+ self.assert_scaling_works(L, K, e1, e2)
+
+
+ def assert_translation_works(self, L, K, e1, e2):
+ """
+ Check that translating ``L`` by alpha*(e1*e2.trans()) increases
+ the value of the associated game by alpha.
+ """
+ # We need to use ``L`` later, so make sure we transpose it
+ # before passing it in as a column-indexed matrix.
+ game1 = SymmetricLinearGame(L.trans(), K, e1, e2)
+ soln1 = game1.solution()
+ value1 = soln1.game_value()
+ x_bar = soln1.player1_optimal()
+ y_bar = soln1.player2_optimal()
+
+ alpha = uniform(-10, 10)
+ tensor_prod = e1*e2.trans()
+
+ # This is the "correct" representation of ``M``, but COLUMN
+ # indexed...
+ M = L + alpha*tensor_prod
+
+ # so we have to transpose it when we feed it to the constructor.
+ game2 = SymmetricLinearGame(M.trans(), K, e1, e2)
+ value2 = game2.solution().game_value()
+
+ self.assert_within_tol(value1 + alpha, value2)
+
+ # Make sure the same optimal pair works.
+ self.assert_within_tol(value2, inner_product(M*x_bar, y_bar))
+
+
+ def test_translation_orthant(self):
+ """
+ Test that translation works over the nonnegative orthant.
+ """
+ (L, K, e1, e2) = _random_orthant_params()
+ self.assert_translation_works(L, K, e1, e2)
+
+
+ def test_translation_icecream(self):
+ """
+ The same as :meth:`test_translation_orthant`, except over the
+ ice cream cone.
+ """
+ (L, K, e1, e2) = _random_icecream_params()
+ self.assert_translation_works(L, K, e1, e2)
+
+
+ def assert_opposite_game_works(self, L, K, e1, e2):
+ """
+ Check the value of the "opposite" game that gives rise to a
+ value that is the negation of the original game. Comes from
+ some corollary.
+ """
+ # We need to use ``L`` later, so make sure we transpose it
+ # before passing it in as a column-indexed matrix.
+ game1 = SymmetricLinearGame(L.trans(), K, e1, e2)
+
+ # This is the "correct" representation of ``M``, but
+ # COLUMN indexed...
+ M = -L.trans()
+
+ # so we have to transpose it when we feed it to the constructor.
+ game2 = SymmetricLinearGame(M.trans(), K, e2, e1)
+
+ soln1 = game1.solution()
+ x_bar = soln1.player1_optimal()
+ y_bar = soln1.player2_optimal()
+ soln2 = game2.solution()
+
+ self.assert_within_tol(-soln1.game_value(), soln2.game_value())
+
+ # Make sure the switched optimal pair works.
+ self.assert_within_tol(soln2.game_value(),
+ inner_product(M*y_bar, x_bar))
+
+
+ def test_opposite_game_orthant(self):
+ """
+ Test the value of the "opposite" game over the nonnegative
+ orthant.
+ """
+ (L, K, e1, e2) = _random_orthant_params()
+ self.assert_opposite_game_works(L, K, e1, e2)
+
+
+ def test_opposite_game_icecream(self):
+ """
+ Like :meth:`test_opposite_game_orthant`, except over the
+ ice-cream cone.
+ """
+ (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.
+ """
+ # We need to use ``L`` later, so make sure we transpose it
+ # before passing it in as a column-indexed matrix.
+ game = SymmetricLinearGame(L.trans(), K, e1, e2)
+ soln = game.solution()
+ x_bar = soln.player1_optimal()
+ y_bar = soln.player2_optimal()
+ value = soln.game_value()
+
+ 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 assert_lyapunov_works(self, L, K, e1, e2):
+ """
+ Check that Lyapunov games act the way we expect.
+ """
+ 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.
+ 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)
+
+ # The dual game's value should always equal the primal's.
+ dualsoln = game.dual().solution()
+ self.assert_within_tol(dualsoln.game_value(), soln.game_value())
+
+
+ def test_lyapunov_orthant(self):
+ """
+ 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.)
+ L = _random_diagonal_matrix(K.dimension())
+
+ self.assert_lyapunov_works(L, K, e1, e2)
+
+
+ def test_lyapunov_icecream(self):
+ """
+ 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.)
+ L = _random_lyapunov_like_icecream(K.dimension())
+
+ self.assert_lyapunov_works(L, K, e1, e2)