From daf22c1bf9368faf42d9b045e8ad437183542362 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 9 Oct 2016 22:05:46 -0400 Subject: [PATCH] Add a first unit test to check that a solution is always obtained. --- src/dunshire/symmetric_linear_game.py | 40 +++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/dunshire/symmetric_linear_game.py b/src/dunshire/symmetric_linear_game.py index 857b064..b6489ba 100644 --- a/src/dunshire/symmetric_linear_game.py +++ b/src/dunshire/symmetric_linear_game.py @@ -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) -- 2.43.2