% 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^4.

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

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(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('Fourth-order placement problem');
% print -deps placement-quartic.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 = (6/1.5^4)*xx.^4;
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-quartic-hist.eps
Computing the optimal locations of the 6 free points... 
Calling SDPT3: 351 variables, 201 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 201
 dim. of sdp    var  = 108,   num. of sdp  blk  = 54
 dim. of socp   var  = 81,   num. of socp blk  = 27
 dim. of linear var  = 108
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|2.7e+01|1.4e+01|3.5e+04| 5.400000e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|0.762|0.729|6.4e+00|3.9e+00|1.2e+04| 7.443811e+02 -3.333465e+02| 0:0:00| chol  1  1 
 2|0.998|0.833|1.6e-02|6.6e-01|3.3e+03| 9.918537e+02 -7.150741e+02| 0:0:00| chol  1  1 
 3|1.000|1.000|3.8e-06|1.0e-03|9.9e+02| 4.190922e+02 -5.717039e+02| 0:0:00| chol  1  1 
 4|1.000|0.928|1.8e-06|1.7e-04|2.1e+02| 8.389689e+01 -1.248869e+02| 0:0:00| chol  1  1 
 5|0.999|1.000|1.1e-07|1.0e-05|6.4e+01| 4.638005e+00 -5.920390e+01| 0:0:00| chol  1  1 
 6|1.000|0.983|3.0e-08|1.2e-06|1.6e+01|-1.411058e+01 -2.983966e+01| 0:0:00| chol  1  1 
 7|0.896|1.000|9.4e-09|1.1e-07|4.6e+00|-1.888389e+01 -2.345131e+01| 0:0:00| chol  1  1 
 8|1.000|0.970|4.2e-09|1.5e-08|9.8e-01|-2.034367e+01 -2.132609e+01| 0:0:00| chol  1  1 
 9|0.887|1.000|1.6e-09|1.8e-09|2.2e-01|-2.056448e+01 -2.078382e+01| 0:0:00| chol  1  1 
10|0.991|0.981|1.4e-11|4.6e-10|1.8e-02|-2.064140e+01 -2.065939e+01| 0:0:00| chol  1  1 
11|0.978|0.979|3.1e-13|2.2e-11|4.1e-04|-2.064621e+01 -2.064662e+01| 0:0:00| chol  1  1 
12|0.986|0.987|6.7e-14|1.3e-12|5.4e-06|-2.064632e+01 -2.064633e+01| 0:0:00| chol  1  1 
13|0.998|0.997|2.4e-12|1.0e-12|1.4e-07|-2.064632e+01 -2.064632e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 13
 primal objective value = -2.06463236e+01
 dual   objective value = -2.06463237e+01
 gap := trace(XZ)       = 1.39e-07
 relative gap           = 3.28e-09
 actual relative gap    = 3.27e-09
 rel. primal infeas     = 2.39e-12
 rel. dual   infeas     = 1.00e-12
 norm(X), norm(y), norm(Z) = 4.6e+01, 8.5e+00, 1.5e+01
 norm(A), norm(b), norm(C) = 2.3e+01, 6.2e+00, 1.1e+01
 Total CPU time (secs)  = 0.37  
 CPU time per iteration = 0.03  
 termination code       =  0
 DIMACS: 7.4e-12  0.0e+00  3.6e-12  0.0e+00  3.3e-09  3.3e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +20.6463
 
Done!