% 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 SDPT3: 25 variables, 13 equality constraints
------------------------------------------------------------

 num. of constraints = 13
 dim. of linear var  = 24
 dim. of free   var  =  1 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|7.5e+01|2.0e+01|2.4e+03| 2.875957e-10  0.000000e+00| 0:0:00| chol  1  1 
 1|0.903|0.656|7.3e+00|7.1e+00|6.8e+02| 1.084223e+01 -8.279551e+00| 0:0:00| chol  1  1 
 2|1.000|0.981|2.5e-06|1.5e-01|3.2e+01| 1.144905e+01 -1.008569e+01| 0:0:00| chol  1  1 
 3|1.000|0.806|1.5e-05|2.9e-02|5.8e+00| 2.825809e+00 -2.488865e+00| 0:0:00| chol  1  1 
 4|1.000|0.067|8.2e-07|2.8e-02|3.3e+00| 6.379819e-01 -2.400851e+00| 0:0:00| chol  1  1 
 5|0.915|0.884|1.6e-06|3.2e-03|5.4e-01| 1.130049e-01 -4.134015e-01| 0:0:00| chol  1  1 
 6|1.000|0.652|9.5e-08|1.1e-03|3.5e-01| 1.157364e-01 -2.254496e-01| 0:0:00| chol  1  1 
 7|0.933|0.584|1.6e-07|4.7e-04|1.1e-01|-1.533932e-02 -1.260174e-01| 0:0:00| chol  1  1 
 8|0.942|0.375|4.8e-08|2.9e-04|6.2e-02|-3.640272e-02 -9.820508e-02| 0:0:00| chol  1  1 
 9|1.000|0.529|1.7e-08|1.4e-04|2.9e-02|-4.200414e-02 -7.055158e-02| 0:0:00| chol  1  1 
10|1.000|0.575|2.4e-09|5.8e-05|1.2e-02|-4.410085e-02 -5.584656e-02| 0:0:00| chol  1  1 
11|1.000|0.637|5.6e-10|2.1e-05|4.2e-03|-4.475713e-02 -4.888417e-02| 0:0:00| chol  1  1 
12|0.989|0.963|7.6e-11|7.9e-07|1.5e-04|-4.483902e-02 -4.499178e-02| 0:0:00| chol  1  1 
13|0.989|0.988|3.2e-12|1.7e-06|1.9e-06|-4.484041e-02 -4.484217e-02| 0:0:00| chol  1  1 
14|1.000|0.989|3.3e-14|2.2e-08|4.1e-08|-4.484041e-02 -4.484045e-02| 0:0:00| chol  1  1 
15|1.000|0.989|1.5e-15|4.6e-10|7.7e-10|-4.484042e-02 -4.484042e-02| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 15
 primal objective value = -4.48404220e-02
 dual   objective value = -4.48404227e-02
 gap := trace(XZ)       = 7.68e-10
 relative gap           = 7.05e-10
 actual relative gap    = 6.70e-10
 rel. primal infeas     = 1.54e-15
 rel. dual   infeas     = 4.60e-10
 norm(X), norm(y), norm(Z) = 9.7e-01, 4.0e-01, 7.4e-01
 norm(A), norm(b), norm(C) = 1.4e+01, 2.0e+00, 2.4e+00
 Total CPU time (secs)  = 0.16  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.5e-15  0.0e+00  5.5e-10  0.0e+00  6.7e-10  7.1e-10
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
 
Done! 
Computing the optimal strategy for player 2 ...  
Calling SDPT3: 25 variables, 13 equality constraints
------------------------------------------------------------

 num. of constraints = 13
 dim. of linear var  = 24
 dim. of free   var  =  1 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|7.9e+01|2.0e+01|2.4e+03|-2.875957e-10  0.000000e+00| 0:0:00| chol  1  1 
 1|0.904|0.649|7.5e+00|7.2e+00|6.8e+02| 1.098635e+01 -8.128345e+00| 0:0:00| chol  1  1 
 2|1.000|0.981|2.7e-06|1.4e-01|3.1e+01| 1.147210e+01 -9.969407e+00| 0:0:00| chol  1  1 
 3|1.000|0.815|1.6e-05|2.7e-02|5.5e+00| 2.771718e+00 -2.299255e+00| 0:0:00| chol  1  1 
 4|1.000|0.064|8.1e-07|2.6e-02|3.2e+00| 6.734830e-01 -2.219711e+00| 0:0:00| chol  1  1 
 5|1.000|0.856|1.6e-06|3.7e-03|9.5e-01| 4.523807e-01 -4.802449e-01| 0:0:00| chol  1  1 
 6|0.972|0.412|2.0e-07|2.2e-03|5.5e-01| 1.986795e-01 -3.427010e-01| 0:0:00| chol  1  1 
 7|1.000|0.376|3.1e-07|1.4e-03|3.7e-01| 1.401321e-01 -2.248989e-01| 0:0:00| chol  1  1 
 8|1.000|0.388|6.3e-08|8.4e-04|2.3e-01| 9.168130e-02 -1.354947e-01| 0:0:00| chol  1  1 
 9|1.000|0.404|2.4e-08|5.0e-04|1.4e-01| 7.060727e-02 -6.977975e-02| 0:0:00| chol  1  1 
10|1.000|0.611|7.1e-09|1.9e-04|5.2e-02| 4.979776e-02 -1.869084e-03| 0:0:00| chol  1  1 
11|0.924|0.758|1.2e-09|4.7e-05|1.2e-02| 4.559065e-02  3.325888e-02| 0:0:00| chol  1  1 
12|0.996|0.859|5.6e-11|6.6e-06|1.7e-03| 4.486235e-02  4.319745e-02| 0:0:00| chol  1  1 
13|0.988|0.983|8.2e-12|1.9e-05|3.0e-05| 4.484071e-02  4.481317e-02| 0:0:00| chol  1  1 
14|0.989|0.989|1.1e-13|3.4e-07|3.3e-07| 4.484043e-02  4.484012e-02| 0:0:00| chol  1  1 
15|1.000|0.989|5.0e-15|3.8e-09|7.0e-09| 4.484042e-02  4.484042e-02| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 15
 primal objective value =  4.48404239e-02
 dual   objective value =  4.48404172e-02
 gap := trace(XZ)       = 7.00e-09
 relative gap           = 6.43e-09
 actual relative gap    = 6.14e-09
 rel. primal infeas     = 5.04e-15
 rel. dual   infeas     = 3.77e-09
 norm(X), norm(y), norm(Z) = 7.4e-01, 4.4e-01, 9.7e-01
 norm(A), norm(b), norm(C) = 1.4e+01, 2.0e+00, 2.4e+00
 Total CPU time (secs)  = 0.16  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 5.0e-15  0.0e+00  4.6e-09  0.0e+00  6.1e-09  6.4e-09
-------------------------------------------------------------------
------------------------------------------------------------
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!