% 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 sedumi: 55 variables, 10 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 = 10, order n = 11, dim = 101, blocks = 2
nnz(A) = 10 + 0, nnz(ADA) = 100, nnz(L) = 55
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.36E+00 0.000
  1 :  -1.28E+01 4.80E-01 0.000 0.3538 0.9000 0.9000   0.88  1  1  1.1E+00
  2 :  -2.51E+01 1.08E-01 0.000 0.2254 0.9000 0.9000   0.68  1  1  2.8E-01
  3 :  -2.80E+01 2.00E-02 0.000 0.1851 0.9000 0.9000   0.95  1  1  5.4E-02
  4 :  -2.88E+01 1.25E-03 0.000 0.0624 0.9900 0.9900   0.98  1  1  3.4E-03
  5 :  -2.88E+01 2.71E-04 0.000 0.2167 0.9000 0.9000   1.00  1  1  7.3E-04
  6 :  -2.88E+01 1.25E-06 0.212 0.0046 0.9900 0.9784   1.00  1  1  4.0E-05
  7 :  -2.88E+01 1.14E-07 0.000 0.0914 0.9900 0.9900   1.00  1  1  3.7E-06
  8 :  -2.88E+01 1.02E-08 0.457 0.0887 0.9900 0.9900   1.00  1  1  3.3E-07
  9 :  -2.88E+01 9.45E-10 0.311 0.0930 0.9900 0.9900   1.00  1  1  3.1E-08
 10 :  -2.88E+01 2.84E-10 0.059 0.3002 0.9000 0.9000   1.00  2  2  9.2E-09

iter seconds digits       c*x               b*y
 10      0.1   Inf -2.8825674846e+01 -2.8825674817e+01
|Ax-b| =   8.6e-09, [Ay-c]_+ =   2.1E-08, |x|=  8.6e+00, |y|=  1.0e+01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    5.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 2.873183e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 604.228.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -26.6924
Done! 
Solving the SDP relaxation of the two-way partitioning problem... 
Calling sedumi: 55 variables, 10 equality constraints
------------------------------------------------------------
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 = 10, order n = 11, dim = 101, blocks = 2
nnz(A) = 10 + 0, nnz(ADA) = 100, nnz(L) = 55
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.36E+00 0.000
  1 :  -1.28E+01 4.80E-01 0.000 0.3538 0.9000 0.9000   0.88  1  1  1.1E+00
  2 :  -2.51E+01 1.08E-01 0.000 0.2254 0.9000 0.9000   0.68  1  1  2.8E-01
  3 :  -2.80E+01 2.00E-02 0.000 0.1851 0.9000 0.9000   0.95  1  1  5.4E-02
  4 :  -2.88E+01 1.25E-03 0.000 0.0624 0.9900 0.9900   0.98  1  1  3.4E-03
  5 :  -2.88E+01 2.71E-04 0.000 0.2167 0.9000 0.9000   1.00  1  1  7.3E-04
  6 :  -2.88E+01 1.25E-06 0.212 0.0046 0.9900 0.9784   1.00  1  1  4.0E-05
  7 :  -2.88E+01 1.14E-07 0.000 0.0914 0.9900 0.9900   1.00  1  1  3.7E-06
  8 :  -2.88E+01 1.02E-08 0.457 0.0887 0.9900 0.9900   1.00  1  1  3.3E-07
  9 :  -2.88E+01 9.45E-10 0.311 0.0930 0.9900 0.9900   1.00  1  1  3.1E-08
 10 :  -2.88E+01 2.84E-10 0.059 0.3002 0.9000 0.9000   1.00  2  2  9.2E-09

iter seconds digits       c*x               b*y
 10      0.0   Inf -2.8825674846e+01 -2.8825674817e+01
|Ax-b| =   8.6e-09, [Ay-c]_+ =   2.1E-08, |x|=  8.6e+00, |y|=  1.0e+01

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