"""
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:
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)