Section 5.2.5: Mixed strategies for matrix games (LP formulation)

% Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 08/24/05
%
% Player 1 wishes to choose u to minimize his expected payoff u'Pv, while
% player 2 wishes to choose v to maximize u'Pv, where P is the payoff
% matrix, u and v are the probability distributions of the choices of each
% player (i.e. u>=0, v>=0, sum(u_i)=1, sum(v_i)=1)
% LP formulation:   minimize    t
%                       s.t.    u >=0 , sum(u) = 1, P'*u <= t*1
%                   maximize    t
%                       s.t.    v >=0 , sum(v) = 1, P*v >= t*1

% Input data
randn('state',0);
n = 12;
m = 12;
P = randn(n,m);

% Optimal strategy for Player 1
fprintf(1,'Computing the optimal strategy for player 1 ... ');

cvx_begin
    variables u(n) t1
    minimize ( t1 )
    u >= 0;
    ones(1,n)*u == 1;
    P'*u <= t1*ones(m,1);
cvx_end

fprintf(1,'Done! \n');

% Optimal strategy for Player 2
fprintf(1,'Computing the optimal strategy for player 2 ... ');

cvx_begin
    variables v(m) t2
    maximize ( t2 )
    v >= 0;
    ones(1,m)*v == 1;
    P*v >= t2*ones(n,1);
cvx_end

fprintf(1,'Done! \n');

% Displaying results
disp('------------------------------------------------------------------------');
disp('The optimal strategies for players 1 and 2 are respectively: ');
disp([u v]);
disp('The expected payoffs for player 1 and player 2 respectively are: ');
[t1 t2]
disp('They are equal as expected!');
Computing the optimal strategy for player 1 ...  
Calling sedumi: 25 variables, 13 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 13, order n = 27, dim = 27, blocks = 2
nnz(A) = 180 + 0, nnz(ADA) = 169, nnz(L) = 91
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            8.24E+00 0.000
  1 :  -3.40E-01 3.71E+00 0.000 0.4499 0.9000 0.9000   2.34  1  1  6.9E+00
  2 :   1.50E-02 1.60E+00 0.000 0.4309 0.9000 0.9000   4.69  1  1  1.0E+00
  3 :   3.90E-02 4.27E-01 0.000 0.2673 0.9000 0.9000   1.49  1  1  2.2E-01
  4 :   4.34E-02 8.53E-02 0.000 0.1997 0.9000 0.9000   1.12  1  1  4.3E-02
  5 :   4.47E-02 6.94E-03 0.000 0.0814 0.9900 0.9900   1.03  1  1  3.7E-03
  6 :   4.48E-02 5.72E-05 0.000 0.0082 0.9990 0.9804   1.01  1  1  4.5E-05
  7 :   4.48E-02 1.45E-11 0.000 0.0000 1.0000 1.0000   1.00  1  1  2.2E-11

iter seconds digits       c*x               b*y
  7      0.0  10.0  4.4840422224e-02  4.4840422219e-02
|Ax-b| =   3.6e-12, [Ay-c]_+ =   1.8E-12, |x|=  8.5e-01, |y|=  4.4e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    1.000E-02    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 3.43441.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
Done! 
Computing the optimal strategy for player 2 ...  
Calling sedumi: 25 variables, 13 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 13, order n = 27, dim = 27, blocks = 2
nnz(A) = 180 + 0, nnz(ADA) = 169, nnz(L) = 91
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            8.24E+00 0.000
  1 :  -3.55E-01 3.64E+00 0.000 0.4423 0.9000 0.9000   2.39  1  1  6.7E+00
  2 :  -4.06E-02 1.65E+00 0.000 0.4525 0.9000 0.9000   4.70  1  1  1.1E+00
  3 :  -4.47E-02 4.55E-01 0.000 0.2760 0.9000 0.9000   1.52  1  1  2.4E-01
  4 :  -4.51E-02 8.72E-02 0.000 0.1916 0.9000 0.9000   1.13  1  1  4.4E-02
  5 :  -4.49E-02 1.66E-02 0.000 0.1900 0.9000 0.9000   1.03  1  1  8.5E-03
  6 :  -4.49E-02 6.66E-04 0.000 0.0402 0.9900 0.8430   1.01  1  1  5.0E-04
  7 :  -4.48E-02 1.32E-08 0.000 0.0000 1.0000 1.0000   1.00  1  1  1.6E-08
  8 :  -4.48E-02 3.35E-15 0.000 0.0000 1.0000 1.0000   1.00  1  1  5.7E-15

iter seconds digits       c*x               b*y
  8      0.0  13.7 -4.4840422221e-02 -4.4840422221e-02
|Ax-b| =   4.0e-15, [Ay-c]_+ =   8.6E-16, |x|=  1.1e+00, |y|=  4.0e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    4.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.69978.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
Done! 
------------------------------------------------------------------------
The optimal strategies for players 1 and 2 are respectively: 
    0.2695    0.0686
    0.0000    0.1619
    0.0973   -0.0000
    0.1573    0.2000
    0.1145   -0.0000
    0.0434    0.1545
    0.0000    0.1146
    0.0000    0.0000
    0.2511    0.1030
    0.0670   -0.0000
   -0.0000   -0.0000
    0.0000    0.1974

The expected payoffs for player 1 and player 2 respectively are: 

ans =

   -0.0448   -0.0448

They are equal as expected!