% 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 Euclidean
% lengths of the links, i.e.
%           minimize    sum_{i<j) h(||x_i - x_j||)
% where h(z) = z.

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(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('Linear placement problem');
% print -deps placement-lin.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 = 2*xx;
plot(xx,yy,'--');
axis([0 2 0 4.5]);
hold on
plot([0 2], [0 0 ], 'k-');
title('Distribution of the 27 link lengths');
% print -deps placement-lin-hist.eps
Computing the optimal locations of the 6 free points... 
Calling sedumi: 81 variables, 39 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 = 39, order n = 55, dim = 82, blocks = 28
nnz(A) = 101 + 0, nnz(ADA) = 279, nnz(L) = 167
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            3.44E-01 0.000
  1 :  -1.40E+01 8.21E-02 0.000 0.2387 0.9000 0.9000   3.01  1  1  6.5E-01
  2 :  -2.01E+01 1.82E-02 0.000 0.2217 0.9000 0.9000   1.13  1  1  1.4E-01
  3 :  -2.16E+01 3.56E-03 0.000 0.1955 0.9000 0.9000   1.02  1  1  2.7E-02
  4 :  -2.18E+01 7.01E-04 0.000 0.1969 0.9000 0.9000   1.00  1  1  5.2E-03
  5 :  -2.19E+01 6.81E-05 0.165 0.0971 0.9900 0.9900   1.00  1  1  5.1E-04
  6 :  -2.19E+01 1.20E-06 0.198 0.0177 0.9900 0.9236   1.00  1  1  4.6E-05
  7 :  -2.19E+01 6.13E-08 0.136 0.0509 0.9900 0.9900   1.00  1  1  2.4E-06
  8 :  -2.19E+01 1.48E-08 0.000 0.2415 0.9009 0.9000   1.00  1  1  5.7E-07
  9 :  -2.19E+01 1.32E-09 0.401 0.0893 0.9900 0.9699   1.00  1  1  5.1E-08
 10 :  -2.19E+01 4.18E-10 0.000 0.3166 0.9275 0.9000   1.00  2  2  1.5E-08

iter seconds digits       c*x               b*y
 10      0.1   Inf -2.1908263691e+01 -2.1908263520e+01
|Ax-b| =   1.1e-08, [Ay-c]_+ =   1.3E-08, |x|=  7.3e+00, |y|=  5.2e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    6.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 2,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +21.9083
Done!