Section 8.2.2: Separating polyhedra in 2D

% 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: 11 variables, 5 equality constraints
------------------------------------------------------------

 num. of constraints =  5
 dim. of socp   var  =  3,   num. of socp blk  =  1
 dim. of linear var  =  8
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      mean(obj)   cputime
-------------------------------------------------------------------
 0|0.000|0.000|3.7e-01|2.0e+00|2.7e+02| 3.394113e+01| 0:0:00| chol  1  1 
 1|0.660|1.000|1.2e-01|4.2e-02|6.0e+01| 2.006953e+01| 0:0:00| chol  1  1 
 2|0.936|1.000|8.0e-03|4.2e-03|3.8e+00|-2.107672e+00| 0:0:00| chol  1  1 
 3|0.917|0.954|6.6e-04|2.2e-03|4.7e-01|-2.010964e+00| 0:0:00| chol  1  1 
 4|1.000|1.000|6.4e-08|1.7e-04|6.7e-02|-2.111658e+00| 0:0:00| chol  1  1 
 5|0.982|0.982|2.1e-09|7.2e-06|1.2e-03|-2.121127e+00| 0:0:00| chol  1  1 
 6|0.985|0.988|8.5e-10|5.0e-07|1.7e-05|-2.121315e+00| 0:0:00| chol  1  1 
 7|0.954|0.984|3.4e-10|8.3e-09|6.1e-07|-2.121320e+00| 0:0:00| chol  2  2 
 8|1.000|1.000|5.4e-11|6.7e-11|6.6e-08|-2.121320e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  8
 primal objective value = -2.12132030e+00
 dual   objective value = -2.12132037e+00
 gap := trace(XZ)       = 6.56e-08
 relative gap           = 1.25e-08
 actual relative gap    = 1.24e-08
 rel. primal infeas     = 5.43e-11
 rel. dual   infeas     = 6.72e-11
 norm(X), norm(y), norm(Z) = 1.9e+00, 2.6e+00, 6.6e+00
 norm(A), norm(b), norm(C) = 5.1e+00, 2.0e+00, 7.1e+00
 Total CPU time (secs)  = 0.1  
 CPU time per iteration = 0.0  
 termination code       =  0
 DIMACS: 5.4e-11  0.0e+00  1.2e-10  0.0e+00  1.2e-08  1.3e-08
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213