% 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
Split 1 free variables
eqs m = 13, order n = 27, dim = 27, blocks = 1
nnz(A) = 192 + 0, nnz(ADA) = 169, nnz(L) = 91
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            8.33E+00 0.000
  1 :  -3.23E-01 3.71E+00 0.000 0.4455 0.9000 0.9000   2.42  1  1  6.7E+00
  2 :   1.15E-02 1.61E+00 0.000 0.4343 0.9000 0.9000   4.60  1  1  1.0E+00
  3 :   3.84E-02 4.33E-01 0.000 0.2688 0.9000 0.9000   1.51  1  1  2.2E-01
  4 :   4.33E-02 8.56E-02 0.000 0.1975 0.9000 0.9000   1.12  1  1  4.3E-02
  5 :   4.47E-02 6.91E-03 0.000 0.0808 0.9900 0.9900   1.03  1  1  3.6E-03
  6 :   4.48E-02 5.43E-05 0.000 0.0079 0.9990 0.9825   1.01  1  1  
iter seconds digits       c*x               b*y
  6      0.0   Inf  4.4840422221e-02  4.4840422221e-02
|Ax-b| =   1.7e-16, [Ay-c]_+ =   1.2E-16, |x|=  8.4e-01, |y|=  4.4e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 5.12072.
------------------------------------------------------------
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
Split 1 free variables
eqs m = 13, order n = 27, dim = 27, blocks = 1
nnz(A) = 192 + 0, nnz(ADA) = 169, nnz(L) = 91
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            8.33E+00 0.000
  1 :  -3.47E-01 3.67E+00 0.000 0.4406 0.9000 0.9000   2.44  1  1  6.6E+00
  2 :  -4.31E-02 1.64E+00 0.000 0.4457 0.9000 0.9000   4.60  1  1  1.1E+00
  3 :  -4.52E-02 4.48E-01 0.000 0.2738 0.9000 0.9000   1.52  1  1  2.3E-01
  4 :  -4.50E-02 8.38E-02 0.000 0.1872 0.9000 0.9000   1.13  1  1  4.2E-02
  5 :  -4.49E-02 7.41E-03 0.000 0.0884 0.9900 0.9900   1.03  1  1  3.9E-03
  6 :  -4.48E-02 1.39E-05 0.000 0.0019 0.9990 0.9990   1.01  1  1  
iter seconds digits       c*x               b*y
  6      0.0   Inf -4.4840422221e-02 -4.4840422221e-02
|Ax-b| =   3.8e-16, [Ay-c]_+ =   1.2E-16, |x|=  1.0e+00, |y|=  4.0e-01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    2.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.40372.
------------------------------------------------------------
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!