]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - test/symmetric_linear_game_test.py
Remove a superfluous transpose.
[dunshire.git] / test / symmetric_linear_game_test.py
index f5b48f4ac1f3e19bcec92d9357a511ebfd794df5..cf305f0ae133699a1be00a3dce8c27c72c9f2a7c 100644 (file)
@@ -4,14 +4,13 @@ Unit tests for the :class:`SymmetricLinearGame` class.
 
 from unittest import TestCase
 
-from dunshire.cones import NonnegativeOrthant
 from dunshire.games import SymmetricLinearGame
 from dunshire.matrices import eigenvalues_re, inner_product, norm
 from dunshire import options
-from .randomgen import (RANDOM_MAX, random_icecream_game,
-                        random_ll_icecream_game, random_ll_orthant_game,
-                        random_nn_scaling, random_orthant_game,
-                        random_positive_orthant_game, random_translation)
+from .randomgen import (random_icecream_game, random_ll_icecream_game,
+                        random_ll_orthant_game, random_nn_scaling,
+                        random_orthant_game, random_positive_orthant_game,
+                        random_translation)
 
 
 # Tell pylint to shut up about the large number of methods.
@@ -43,6 +42,102 @@ class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
         self.assertTrue(abs(first - second) < options.ABS_TOL*modifier)
 
 
+    def test_solutions_dont_change_orthant(self):
+        """
+        If we solve the same game twice over the nonnegative orthant,
+        then we should get the same solution both times. The solution to
+        a game is not unique, but the process we use is (as far as we
+        know) deterministic.
+        """
+        G = random_orthant_game()
+        self.assert_solutions_dont_change(G)
+
+    def test_solutions_dont_change_icecream(self):
+        """
+        If we solve the same game twice over the ice-cream cone, then we
+        should get the same solution both times. The solution to a game
+        is not unique, but the process we use is (as far as we know)
+        deterministic.
+        """
+        G = random_icecream_game()
+        self.assert_solutions_dont_change(G)
+
+    def assert_solutions_dont_change(self, G):
+        """
+        Solve ``G`` twice and check that the solutions agree.
+        """
+        soln1 = G.solution()
+        soln2 = G.solution()
+        p1_diff = norm(soln1.player1_optimal() - soln2.player1_optimal())
+        p2_diff = norm(soln1.player2_optimal() - soln2.player2_optimal())
+        gv_diff = abs(soln1.game_value() - soln2.game_value())
+
+        p1_close = p1_diff < options.ABS_TOL
+        p2_close = p2_diff < options.ABS_TOL
+        gv_close = gv_diff < options.ABS_TOL
+
+        self.assertTrue(p1_close and p2_close and gv_close)
+
+
+    def assert_player1_start_valid(self, G):
+        """
+        Ensure that player one's starting point satisfies both the
+        equality and cone inequality in the CVXOPT primal problem.
+        """
+        x = G.player1_start()['x']
+        s = G.player1_start()['s']
+        s1 = s[0:G.dimension()]
+        s2 = s[G.dimension():]
+        self.assert_within_tol(norm(G.A()*x - G.b()), 0)
+        self.assertTrue((s1, s2) in G.C())
+
+
+    def test_player1_start_valid_orthant(self):
+        """
+        Ensure that player one's starting point is feasible over the
+        nonnegative orthant.
+        """
+        G = random_orthant_game()
+        self.assert_player1_start_valid(G)
+
+
+    def test_player1_start_valid_icecream(self):
+        """
+        Ensure that player one's starting point is feasible over the
+        ice-cream cone.
+        """
+        G = random_icecream_game()
+        self.assert_player1_start_valid(G)
+
+
+    def assert_player2_start_valid(self, G):
+        """
+        Check that player two's starting point satisfies both the
+        cone inequality in the CVXOPT dual problem.
+        """
+        z = G.player2_start()['z']
+        z1 = z[0:G.dimension()]
+        z2 = z[G.dimension():]
+        self.assertTrue((z1, z2) in G.C())
+
+
+    def test_player2_start_valid_orthant(self):
+        """
+        Ensure that player two's starting point is feasible over the
+        nonnegative orthant.
+        """
+        G = random_orthant_game()
+        self.assert_player2_start_valid(G)
+
+
+    def test_player2_start_valid_icecream(self):
+        """
+        Ensure that player two's starting point is feasible over the
+        ice-cream cone.
+        """
+        G = random_icecream_game()
+        self.assert_player2_start_valid(G)
+
 
     def test_condition_lower_bound(self):
         """
@@ -65,9 +160,13 @@ class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
         of the game by the same number.
         """
         (alpha, H) = random_nn_scaling(G)
-        value1 = G.solution().game_value()
-        value2 = H.solution().game_value()
-        modifier = 4*max(abs(alpha), 1)
+        soln1 = G.solution()
+        soln2 = H.solution()
+        value1 = soln1.game_value()
+        value2 = soln2.game_value()
+        modifier1 = G.tolerance_scale(soln1)
+        modifier2 = H.tolerance_scale(soln2)
+        modifier = max(modifier1, modifier2)
         self.assert_within_tol(alpha*value1, value2, modifier)
 
 
@@ -106,7 +205,7 @@ class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
         (alpha, H) = random_translation(G)
         value2 = H.solution().game_value()
 
-        modifier = 4*max(abs(alpha), 1)
+        modifier = G.tolerance_scale(soln1)
         self.assert_within_tol(value1 + alpha, value2, modifier)
 
         # Make sure the same optimal pair works.
@@ -136,27 +235,25 @@ class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
         value that is the negation of the original game. Comes from
         some corollary.
         """
-        # This is the "correct" representation of ``M``, but
-        # COLUMN indexed...
-        M = -G.L().trans()
-
-        # so we have to transpose it when we feed it to the constructor.
+        # Since L is a CVXOPT matrix, it will be transposed automatically.
         # Note: the condition number of ``H`` should be comparable to ``G``.
-        H = SymmetricLinearGame(M.trans(), G.K(), G.e2(), G.e1())
+        H = SymmetricLinearGame(-G.L(), G.K(), G.e2(), G.e1())
 
         soln1 = G.solution()
         x_bar = soln1.player1_optimal()
         y_bar = soln1.player2_optimal()
         soln2 = H.solution()
 
-        # The modifier of 4 is because each could be off by 2*ABS_TOL,
-        # which is how far apart the primal/dual objectives have been
-        # observed being.
-        self.assert_within_tol(-soln1.game_value(), soln2.game_value(), 4)
+        modifier = G.tolerance_scale(soln1)
+        self.assert_within_tol(-soln1.game_value(),
+                               soln2.game_value(),
+                               modifier)
 
         # Make sure the switched optimal pair works. Since x_bar and
         # y_bar come from G, we use the same modifier.
-        self.assert_within_tol(soln2.game_value(), H.payoff(y_bar, x_bar), 4)
+        self.assert_within_tol(soln2.game_value(),
+                               H.payoff(y_bar, x_bar),
+                               modifier)
 
 
 
@@ -189,10 +286,11 @@ class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
         value = soln.game_value()
 
         ip1 = inner_product(y_bar, G.L()*x_bar - value*G.e1())
-        self.assert_within_tol(ip1, 0)
-
         ip2 = inner_product(value*G.e2() - G.L().trans()*y_bar, x_bar)
-        self.assert_within_tol(ip2, 0)
+
+        modifier = G.tolerance_scale(soln)
+        self.assert_within_tol(ip1, 0, modifier)
+        self.assert_within_tol(ip2, 0, modifier)
 
 
     def test_orthogonality_orthant(self):
@@ -248,11 +346,9 @@ class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
             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.
-        # The modifier of 4 is because even though the games are dual,
-        # CVXOPT doesn't know that, and each could be off by 2*ABS_TOL.
         dualsoln = G.dual().solution()
-        self.assert_within_tol(dualsoln.game_value(), soln.game_value(), 4)
+        mod = G.tolerance_scale(soln)
+        self.assert_within_tol(dualsoln.game_value(), soln.game_value(), mod)
 
 
     def test_lyapunov_orthant(self):