]> gitweb.michael.orlitzky.com - dunshire.git/commitdiff
Add a first unit test to check that a solution is always obtained.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 10 Oct 2016 02:05:46 +0000 (22:05 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 10 Oct 2016 02:36:20 +0000 (22:36 -0400)
src/dunshire/symmetric_linear_game.py

index 857b06447455222995220646b9fdfa56423c84d6..b6489ba6de43877dd5707a39b3d7811dd9d2e4a7 100644 (file)
@@ -6,13 +6,15 @@ to solve a linear game.
 """
 
 from cvxopt import matrix, printing, solvers
+from unittest import TestCase
 
 from cones import CartesianProduct
 from errors import GameUnsolvableException
-from matrices import append_col, append_row, identity
+from matrices import append_col, append_row, identity, inner_product
+import options
 
-printing.options['dformat'] = '%.7f'
-solvers.options['show_progress'] = False
+printing.options['dformat'] = options.FLOAT_FORMAT
+solvers.options['show_progress'] = options.VERBOSE
 
 
 class Solution:
@@ -383,3 +385,35 @@ class SymmetricLinearGame:
                                    self._K, # Since "K" is symmetric.
                                    self._e2,
                                    self._e1)
+
+
+class SymmetricLinearGameTest(TestCase):
+
+    def assertEqualWithinTol(self, first, second):
+        """
+        Test that ``first`` and ``second`` are equal within our default
+        tolerance.
+        """
+        self.assertTrue(abs(first - second) < options.ABS_TOL)
+
+
+    def test_solution_exists(self):
+        """
+        Every linear game has a solution, so we should be able to solve
+        every symmetric linear game. Pick some parameters randomly and
+        give it a shot.
+        """
+        from cones import NonnegativeOrthant
+        from random import randint, uniform
+        ambient_dim = randint(1,10)
+        K = NonnegativeOrthant(ambient_dim)
+        e1 = [uniform(0.1, 10) for idx in range(ambient_dim)]
+        e2 = [uniform(0.1, 10) for idx in range(ambient_dim)]
+        L = [[uniform(-10, 10) for i in range(ambient_dim)]
+              for j in range(ambient_dim)]
+        G = SymmetricLinearGame(L, K, e1, e2)
+        soln = G.solution()
+        L_matrix = matrix(L).trans()
+        expected = inner_product(L_matrix*soln.player1_optimal(),
+                                 soln.player2_optimal())
+        self.assertEqualWithinTol(soln.game_value(), expected)