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) self._e1 = matrix(e1, (K.dimension(), 1)) self._e2 = matrix(e2, (K.dimension(), 1)) 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') # 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') 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():])