% Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/09/05
% (a figure is generated)
%
% If the two polyhedra C = {x | A1*x <= b1} and D = {y | A2*y <= b2} can be
% separated by a hyperplane, it will be of the  form
%           z'*x - z'*y >= -lambda'*b1 - mu'*b2 > 0
% where z, lambda and mu are the optimal variables of the problem:
%           maximize    -b1'*lambda - b2'*mu
%               s.t.    A1'*lambda + z = 0
%                       A2'*mu  - z = 0
%                       norm*(z) <= 1
%                       lambda >=0 , mu >= 0
% Note: here x is in R^2

% Input data
randn('seed',0);
n  = 2;
m = 2*n;
A1 = [1 1; 1 -1; -1 1; -1 -1];
A2 = [1 0; -1 0; 0 1; 0 -1];
b1 = 2*ones(m,1);
b2 = [5; -3; 4; -2];

% Solving with CVX
fprintf(1,'Finding a separating hyperplane between the 2 polyhedra...');

cvx_begin
    variables lam(m) muu(m) z(n)
    maximize ( -b1'*lam - b2'*muu)
    A1'*lam + z == 0;
    A2'*muu - z == 0;
    norm(z) <= 1;
    -lam <=0;
    -muu <=0;
cvx_end

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

% Displaying results
disp('------------------------------------------------------------------');
disp('The distance between the 2 polyhedra C and D is: ' );
disp(['dist(C,D) = ' num2str(cvx_optval)]);

% Plotting
t = linspace(-3,6,100);
p = -z(1)*t/z(2) + (muu'*b2 - lam'*b1)/(2*z(2));
figure;
fill([-2; 0; 2; 0],[0;2;0;-2],'b', [3;5;5;3],[2;2;4;4],'r')
axis([-3 6 -3 6])
axis square
hold on;
plot(t,p)
title('Separating 2 polyhedra by a hyperplane');
Finding a separating hyperplane between the 2 polyhedra... 
Calling SDPT3 4.0: 12 variables, 5 equality constraints
------------------------------------------------------------

 num. of constraints =  5
 dim. of socp   var  =  3,   num. of socp blk  =  1
 dim. of linear var  =  9
*******************************************************************
   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|5.4e+00|3.1e+00|9.1e+02| 1.200000e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|0.874|1.000|6.8e-01|3.5e-02|1.3e+02| 1.008522e+02 -1.099735e+01| 0:0:00| chol  1  1 
 2|0.967|1.000|2.2e-02|3.5e-03|6.6e+00| 8.670507e-01 -5.467940e+00| 0:0:00| chol  1  1 
 3|1.000|0.621|1.0e-06|6.0e-03|3.9e+00| 9.835878e-01 -2.862666e+00| 0:0:00| chol  1  1 
 4|0.993|0.923|4.2e-08|4.9e-04|2.4e-01|-1.944540e+00 -2.183296e+00| 0:0:00| chol  1  1 
 5|0.935|0.986|3.3e-09|1.1e-05|1.3e-02|-2.110100e+00 -2.122803e+00| 0:0:00| chol  1  1 
 6|0.907|1.000|2.2e-09|3.5e-07|1.5e-03|-2.119984e+00 -2.121525e+00| 0:0:00| chol  1  1 
 7|0.983|1.000|1.2e-09|3.5e-08|4.8e-05|-2.121281e+00 -2.121329e+00| 0:0:00| chol  1  1 
 8|0.979|0.987|1.4e-10|6.9e-10|9.3e-07|-2.121320e+00 -2.121320e+00| 0:0:00| chol  1  1 
 9|1.000|1.000|3.6e-11|2.9e-11|3.2e-08|-2.121320e+00 -2.121320e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value = -2.12132032e+00
 dual   objective value = -2.12132035e+00
 gap := trace(XZ)       = 3.19e-08
 relative gap           = 6.09e-09
 actual relative gap    = 6.02e-09
 rel. primal infeas (scaled problem)   = 3.58e-11
 rel. dual     "        "       "      = 2.90e-11
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 1.9e+00, 3.9e+00, 6.9e+00
 norm(A), norm(b), norm(C) = 5.2e+00, 2.0e+00, 9.1e+00
 Total CPU time (secs)  = 0.11  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 3.6e-11  0.0e+00  4.4e-11  0.0e+00  6.0e-09  6.1e-09
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
 
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213