From b753e58ade3acb5dc1ad325c2b59b28549311125 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 5 Oct 2016 17:25:08 -0400 Subject: [PATCH] Remove member vars from SymmetricLinearCone and work on the solution dict. --- symmetric_linear_game.py | 57 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/symmetric_linear_game.py b/symmetric_linear_game.py index 29f64ad..a33c345 100644 --- a/symmetric_linear_game.py +++ b/symmetric_linear_game.py @@ -1,6 +1,7 @@ from cvxopt import matrix, printing, solvers from cones import CartesianProduct +from errors import GameUnsolvableException, GameValueMismatchException from matrices import append_col, append_row, identity printing.options['dformat'] = '%.7f' @@ -42,41 +43,41 @@ class SymmetricLinearGame: """ self._K = K - self._C = CartesianProduct(K, K) self._e1 = matrix(e1, (K.dimension(), 1)) self._e2 = matrix(e2, (K.dimension(), 1)) + self._L = matrix(L, (K.dimension(), K.dimension())) if not K.contains_strict(self._e1): raise ValueError('the point e1 must lie in the interior of K') + if not K.contains_strict(self._e2): raise ValueError('the point e2 must lie in the interior of K') - self._L = matrix(L, (K.dimension(), K.dimension())) - self._b = matrix([1], tc='d') + def solution(self): + + C = CartesianProduct(K, K) + b = matrix([1], tc='d') # A column of zeros that fits K. - zero = matrix(0, (K.dimension(), 1), tc='d') - self._h = matrix([zero, zero]) - self._c = matrix([-1, zero]) - self._G = append_row(append_col(zero, -identity(K.dimension())), - append_col(self._e1, -self._L)) - self._A = matrix([0, self._e1], (1, K.dimension() + 1), 'd') + zero = matrix(0, (self._K.dimension(), 1), tc='d') + h = matrix([zero, zero]) + c = matrix([-1, zero]) + G = append_row(append_col(zero, -identity(K.dimension())), + append_col(self._e1, -self._L)) + A = matrix([0, self._e1], (1, K.dimension() + 1), 'd') - def solution(self): - soln = solvers.conelp(self._c, - self._G, - self._h, - self._C.cvxopt_dims(), - self._A, - self._b) - return soln - - def solve(self): - soln = self.solution() - - print('Value of the game (player one): {:f}'.format(soln['x'][0])) - print('Optimal strategy (player one):') - print(soln['x'][1:]) - - print('Value of the game (player two): {:f}'.format(soln['y'][0])) - print('Optimal strategy (player two):') - print(soln['z'][self._K.dimension():]) + soln = solvers.conelp(c, G, h, C.cvxopt_dims(), A, b) + + #if soln['status'] != 'optimal': + raise GameUnsolvableException(soln['status'], soln) + + p1_value = soln['x'][0] + p2_value = soln['y'][0] + p1_strategy = soln['x'][1:] + p2_strategy = soln['z'][self._K.dimension():] + + #if p1_value != p2_value: + raise GameValueMismatchException(p1_value, p2_value, soln) + + return {'game value': p1_value, + 'player one': p1_strategy, + 'player two': p2_strategy} -- 2.44.2