Figure 8.16: Quadratic placement problem

% Section 8.7.3, Boyd & Vandenberghe "Convex Optimization"
% Original by Lieven Vandenberghe
% Adapted for CVX by Joelle Skaf - 10/24/05
% (a figure is generated)
%
% Placement problem with 6 free points, 8 fixed points and 27 links.
% The coordinates of the free points minimize the sum of the squares of
% Euclidean lengths of the links, i.e.
%           minimize    sum_{i<j) h(||x_i - x_j||)
% where h(z) = z^2.

linewidth = 1;      % in points;  width of dotted lines
markersize = 5;    % in points;  marker size

% Input data
fixed = [ 1   1  -1 -1    1   -1  -0.2  0.1; % coordinates of fixed points
          1  -1  -1  1 -0.5 -0.2    -1    1]';
M = size(fixed,1);  % number of fixed points
N = 6;              % number of free points

% first N columns of A correspond to free points,
% last M columns correspond to fixed points

A = [ 1  0  0 -1  0  0    0  0  0  0  0  0  0  0
      1  0 -1  0  0  0    0  0  0  0  0  0  0  0
      1  0  0  0 -1  0    0  0  0  0  0  0  0  0
      1  0  0  0  0  0   -1  0  0  0  0  0  0  0
      1  0  0  0  0  0    0 -1  0  0  0  0  0  0
      1  0  0  0  0  0    0  0  0  0 -1  0  0  0
      1  0  0  0  0  0    0  0  0  0  0  0  0 -1
      0  1 -1  0  0  0    0  0  0  0  0  0  0  0
      0  1  0 -1  0  0    0  0  0  0  0  0  0  0
      0  1  0  0  0 -1    0  0  0  0  0  0  0  0
      0  1  0  0  0  0    0 -1  0  0  0  0  0  0
      0  1  0  0  0  0    0  0 -1  0  0  0  0  0
      0  1  0  0  0  0    0  0  0  0  0  0 -1  0
      0  0  1 -1  0  0    0  0  0  0  0  0  0  0
      0  0  1  0  0  0    0 -1  0  0  0  0  0  0
      0  0  1  0  0  0    0  0  0  0 -1  0  0  0
      0  0  0  1 -1  0    0  0  0  0  0  0  0  0
      0  0  0  1  0  0    0  0 -1  0  0  0  0  0
      0  0  0  1  0  0    0  0  0 -1  0  0  0  0
      0  0  0  1  0  0    0  0  0  0  0 -1  0  0
      0  0  0  1  0 -1    0  0  0  0  0 -1  0  0        % error in data!!!
      0  0  0  0  1 -1    0  0  0  0  0  0  0  0
      0  0  0  0  1  0   -1  0  0  0  0  0  0  0
      0  0  0  0  1  0    0  0  0 -1  0  0  0  0
      0  0  0  0  1  0    0  0  0  0  0  0  0 -1
      0  0  0  0  0  1    0  0 -1  0  0  0  0  0
      0  0  0  0  0  1    0  0  0  0 -1  0  0  0 ];
nolinks = size(A,1);    % number of links

fprintf(1,'Computing the optimal locations of the 6 free points...');

cvx_begin
    variable x(N+M,2)
    minimize ( sum(square_pos(norms( A*x,2,2 ))))
    x(N+[1:M],:) == fixed;
cvx_end

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

% Plots
free_sum = x(1:N,:);
figure(1);
dots = plot(free_sum(:,1), free_sum(:,2), 'or', fixed(:,1), fixed(:,2), 'bs');
set(dots(1),'MarkerFaceColor','red');
hold on
legend('Free points','Fixed points','Location','Best');
for i=1:nolinks
  ind = find(A(i,:));
  line2 = plot(x(ind,1), x(ind,2), ':k');
  hold on
  set(line2,'LineWidth',linewidth);
end
axis([-1.1 1.1 -1.1 1.1]) ;
axis equal;
title('Quadratic placement problem');
% print -deps placement-quadr.eps

figure(2)
all = [free_sum; fixed];
bins = 0.05:0.1:1.95;
lengths = sqrt(sum((A*all).^2')');
[N2,hist2] = hist(lengths,bins);
bar(hist2,N2);
hold on;
xx = linspace(0,2,1000); yy = (4/1.5^2)*xx.^2;
plot(xx,yy,'--');
axis([0 1.5 0 4.5]);
hold on
plot([0 2], [0 0 ], 'k-');
title('Distribution of the 27 link lengths');
% print -deps placement-quadr-hist.eps
Computing the optimal locations of the 6 free points... 
Calling SeDuMi: 174 variables (12 free), 108 equality constraints
------------------------------------------------------------------------
SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 12 free variables
eqs m = 108, order n = 133, dim = 214, blocks = 55
nnz(A) = 335 + 0, nnz(ADA) = 1060, nnz(L) = 966
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            7.62E+000 0.000
  1 :  1.11E+001 2.07E+000 0.000 0.2722 0.9000 0.9000   1.83  1  1  1.6E+000
  2 :  1.78E+001 5.03E-001 0.000 0.2426 0.9000 0.9000   0.88  1  1  4.4E-001
  3 :  2.04E+001 2.17E-002 0.000 0.0432 0.9900 0.9900   0.96  1  1  2.0E-002
  4 :  2.05E+001 5.56E-005 0.000 0.0026 0.9990 0.9990   1.00  1  1  5.1E-005
  5 :  2.05E+001 2.16E-007 0.177 0.0039 0.9990 0.9990   1.00  1  1  2.0E-007
  6 :  2.05E+001 4.32E-008 0.000 0.1999 0.9000 0.9000   1.00  2  2  4.0E-008
  7 :  2.05E+001 8.88E-009 0.000 0.2055 0.9000 0.9000   1.00  2  2  8.1E-009

iter seconds digits       c*x               b*y
  7      0.0   8.6  2.0547313604e+001  2.0547313551e+001
|Ax-b| =  1.6e-008, [Ay-c]_+ =  1.3E-009, |x|= 1.4e+001, |y|= 1.8e+001

Detailed timing (sec)
   Pre          IPM          Post
2.003E-002    2.003E-002    1.001E-002    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.33546.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +20.5473
Done!