from cvxopt import matrix, printing, solvers from cones import CartesianProduct from matrices import append_col, append_row, identity printing.options['dformat'] = '%.7f' solvers.options['show_progress'] = False class SymmetricLinearGame: """ A representation of a symmetric linear game. The data for a linear game are, * A "payoff" operator ``L``. * A cone ``K``. * A point ``e`` in the interior of ``K``. * A point ``f`` in the interior of the dual of ``K``. In a symmetric game, the cone ``K`` is be self-dual. We therefore name the two interior points ``e1`` and ``e2`` to indicate that they come from the same cone but are "chosen" by players one and two respectively. The ambient space is assumed to be the span of ``K``. """ def __init__(self, L, K, e1, e2): """ INPUT: - ``L`` -- an n-by-b matrix represented as a list of lists of real numbers. - ``K`` -- a SymmetricCone instance. - ``e1`` -- the interior point of ``K`` belonging to player one, as a column vector. - ``e2`` -- the interior point of ``K`` belonging to player two, as a column vector. """ self._K = K self._C = CartesianProduct(K, K) n = self._K.dimension() self._L = matrix(L, (n,n)) self._e1 = matrix(e1, (n,1)) # TODO: check that e1 and e2 self._e2 = matrix(e2, (n,1)) # are in the interior of K... self._h = matrix(0, (2*n,1), 'd') self._b = matrix(1, (1,1), 'd') self._c = matrix([-1] + [0]*n, (n+1,1), 'd') self._G = append_row(append_col(matrix(0,(n,1)), -identity(n)), append_col(self._e1, -self._L)) self._A = matrix([0] + e1, (1, n+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() nu = soln['x'][0] print('Value of the game (player one): {:f}'.format(nu)) opt1 = soln['x'][1:] print('Optimal strategy (player one):') print(opt1) omega = soln['y'][0] n = self._K.dimension() opt2 = soln['z'][n:] print('Value of the game (player two): {:f}'.format(omega)) print('Optimal strategy (player two):') print(opt2)