% Boyd & Vandenberghe. "Convex Optimization"
% Joëlle Skaf - 09/07/05
% (a figure is generated)
%
% Compares the optimal values of:
% 1) the Lagrange dual of the two-way partitioning problem
%               maximize    -sum(nu)
%                   s.t.    W + diag(nu) >= 0
% 2) the SDP relaxation of the two-way partitioning problem
%               minimize    trace(WX)
%                   s.t.    X >= 0
%                           X_ii = 1

% Input data
randn('state',0);
n = 10;
W = randn(n); W = 0.5*(W + W');

% Lagrange dual
fprintf(1,'Solving the dual of the two-way partitioning problem...');

cvx_begin sdp
    variable nu(n)
    maximize ( -sum(nu) )
    W + diag(nu) >= 0;
cvx_end

fprintf(1,'Done! \n');
opt1 = cvx_optval;

% SDP relaxation
fprintf(1,'Solving the SDP relaxation of the two-way partitioning problem...');

cvx_begin sdp
    variable X(n,n) symmetric
    minimize ( trace(W*X) )
    diag(X) == 1;
    X >= 0;
cvx_end

fprintf(1,'Done! \n');
opt2 = cvx_optval;

% Displaying results
disp('------------------------------------------------------------------------');
disp('The optimal value of the Lagrange dual and the SDP relaxation fo the    ');
disp('two-way partitioning problem are, respectively, ');
disp([opt1 opt2])
disp('They are equal as expected!');
Solving the dual of the two-way partitioning problem... 
Calling SDPT3: 55 variables, 45 equality constraints
------------------------------------------------------------

 num. of constraints = 45
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|8.2e-01|6.8e+00|1.4e+03| 1.427322e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|1.000|7.9e-08|7.6e-02|1.3e+02| 1.203407e+02  5.975533e+00| 0:0:00| chol  1  1 
 2|0.824|1.000|2.0e-07|7.6e-03|2.3e+01| 3.558432e+01  1.280563e+01| 0:0:00| chol  1  1 
 3|1.000|0.886|1.4e-07|1.5e-03|7.4e+00| 3.269993e+01  2.534106e+01| 0:0:00| chol  1  1 
 4|0.866|1.000|5.9e-08|7.6e-05|8.3e-01| 2.915069e+01  2.832212e+01| 0:0:00| chol  1  1 
 5|0.912|1.000|1.9e-08|7.6e-06|3.6e-01| 2.890026e+01  2.853999e+01| 0:0:00| chol  1  1 
 6|0.965|0.954|2.9e-09|1.1e-06|2.7e-02| 2.883038e+01  2.880355e+01| 0:0:00| chol  1  1 
 7|0.950|0.949|1.4e-09|1.3e-07|1.5e-03| 2.882589e+01  2.882442e+01| 0:0:00| chol  1  1 
 8|0.955|0.940|6.6e-10|8.0e-09|1.1e-04| 2.882569e+01  2.882557e+01| 0:0:00| chol  1  1 
 9|0.988|0.979|7.1e-11|3.0e-10|5.7e-06| 2.882568e+01  2.882567e+01| 0:0:00| chol  1  1 
10|1.000|1.000|2.2e-11|1.4e-11|5.4e-07| 2.882568e+01  2.882567e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value =  2.88256751e+01
 dual   objective value =  2.88256745e+01
 gap := trace(XZ)       = 5.42e-07
 relative gap           = 9.25e-09
 actual relative gap    = 9.23e-09
 rel. primal infeas     = 2.17e-11
 rel. dual   infeas     = 1.43e-11
 norm(X), norm(y), norm(Z) = 1.2e+01, 1.1e+01, 8.6e+00
 norm(A), norm(b), norm(C) = 5.7e+00, 5.7e+00, 4.2e+00
 Total CPU time (secs)  = 0.11  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 5.0e-11  0.0e+00  3.0e-11  0.0e+00  9.2e-09  9.2e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -26.6924
 
Done! 
Solving the SDP relaxation of the two-way partitioning problem... 
Calling SDPT3: 55 variables, 10 equality constraints
------------------------------------------------------------

 num. of constraints = 10
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|6.8e+00|4.3e+00|1.0e+03| 0.000000e+00  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|1.000|2.6e-06|4.2e-02|8.6e+01|-7.238328e+00 -9.272163e+01| 0:0:00| chol  1  1 
 2|1.000|0.880|7.1e-08|8.7e-03|1.6e+01|-1.728912e+01 -3.312548e+01| 0:0:00| chol  1  1 
 3|0.884|1.000|1.6e-08|4.2e-04|4.7e+00|-2.624691e+01 -3.095297e+01| 0:0:00| chol  1  1 
 4|0.962|0.948|4.7e-09|6.1e-05|2.5e-01|-2.863288e+01 -2.888462e+01| 0:0:00| chol  1  1 
 5|0.979|0.989|4.8e-10|4.8e-06|1.9e-02|-2.881025e+01 -2.882921e+01| 0:0:00| chol  1  1 
 6|0.946|0.982|2.8e-10|5.0e-07|9.2e-04|-2.882492e+01 -2.882583e+01| 0:0:00| chol  1  1 
 7|1.000|1.000|1.9e-09|5.6e-11|8.9e-05|-2.882561e+01 -2.882569e+01| 0:0:00| chol  1  1 
 8|0.974|0.984|1.6e-10|8.6e-11|2.2e-06|-2.882567e+01 -2.882568e+01| 0:0:00| chol  1  1 
 9|1.000|1.000|3.4e-11|3.1e-11|1.4e-07|-2.882567e+01 -2.882568e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value = -2.88256749e+01
 dual   objective value = -2.88256750e+01
 gap := trace(XZ)       = 1.41e-07
 relative gap           = 2.40e-09
 actual relative gap    = 2.41e-09
 rel. primal infeas     = 3.36e-11
 rel. dual   infeas     = 3.10e-11
 norm(X), norm(y), norm(Z) = 8.6e+00, 1.0e+01, 1.2e+01
 norm(A), norm(b), norm(C) = 4.2e+00, 4.2e+00, 7.6e+00
 Total CPU time (secs)  = 0.10  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 7.0e-11  0.0e+00  9.7e-11  0.0e+00  2.4e-09  2.4e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -26.6924
 
Done! 
------------------------------------------------------------------------
The optimal value of the Lagrange dual and the SDP relaxation fo the    
two-way partitioning problem are, respectively, 
  -26.6924  -26.6924

They are equal as expected!