% Section 8.7.1, Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/23/05
%
% K fixed points (u1,v1),..., (uK,vK) in R^2 are given and the goal is to place
% one additional point (u,v) such that:
% 1) the L1-norm is minimized, i.e.
%           minimize    sum_{i=1}^K ( |u - u_i| + |v - v_i| )
%    the solution in this case is any median of the fixed points
% 2) the L2-norm is minimized, i.e.
%           minimize    sum_{i=1}^K ( |u - u_i|^2 + |v - v_i|^2 )^.5
%    the solution in this case is the Weber point of the fixed points

% Data generation
n = 2;
K = 11;
randn('state',0);
P = randn(n,K);

% L1 - norm
fprintf(1,'Minimizing the L1-norm of the sum of the distances to fixed points...');

cvx_begin
    variable x1(2)
    minimize ( sum(norms(x1*ones(1,K) - P,1)) )
cvx_end

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

% L2 - norm
fprintf(1,'Minimizing the L2-norm of the sum of the distances to fixed points...');

cvx_begin
    variable x2(2)
    minimize ( sum(norms(x2*ones(1,K) - P,2)) )
cvx_end

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

% Displaying results
disp('------------------------------------------------------------------');
disp('The optimal point location for the L1-norm case is: ');
disp(x1);
disp('The optimal point location for the L2-norm case is: ');
disp(x2);
Minimizing the L1-norm of the sum of the distances to fixed points... 
Calling sedumi: 44 variables, 20 equality constraints
------------------------------------------------------------
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 = 20, order n = 45, dim = 45, blocks = 1
nnz(A) = 80 + 0, nnz(ADA) = 200, nnz(L) = 110
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.17E+02 0.000
  1 :   5.26E+00 3.27E+01 0.000 0.2803 0.9000 0.9000   2.72  1  1  9.7E-01
  2 :   1.13E+01 6.73E+00 0.000 0.2058 0.9000 0.9000   1.18  1  1  3.0E-01
  3 :   1.32E+01 1.58E+00 0.000 0.2347 0.9000 0.9000   1.02  1  1  8.0E-02
  4 :   1.37E+01 3.53E-01 0.000 0.2235 0.9000 0.9000   1.00  1  1  1.9E-02
  5 :   1.39E+01 1.98E-02 0.000 0.0561 0.9900 0.9900   1.00  1  1  1.1E-03
  6 :   1.39E+01 5.68E-06 0.000 0.0003 0.9999 0.9999   1.00  1  1  
iter seconds digits       c*x               b*y
  6      0.0   Inf  1.3868099974e+01  1.3868099974e+01
|Ax-b| =   5.7e-16, [Ay-c]_+ =   0.0E+00, |x|=  4.1e+00, |y|=  4.2e+00

Detailed timing (sec)
   Pre          IPM          Post
2.000E-02    4.000E-02    0.000E+00    
Max-norms: ||b||=3.848770e+00, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +13.8681
Done! 
Minimizing the L2-norm of the sum of the distances to fixed points... 
Calling sedumi: 33 variables, 20 equality constraints
------------------------------------------------------------
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 = 20, order n = 23, dim = 34, blocks = 12
nnz(A) = 40 + 0, nnz(ADA) = 400, nnz(L) = 210
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            3.51E+01 0.000
  1 :   7.89E+00 9.87E+00 0.000 0.2808 0.9000 0.9000   2.94  1  1  8.2E-01
  2 :   1.05E+01 2.03E+00 0.000 0.2060 0.9000 0.9000   1.12  1  1  1.8E-01
  3 :   1.14E+01 1.65E-01 0.000 0.0812 0.9900 0.9900   1.01  1  1  1.5E-02
  4 :   1.15E+01 1.04E-02 0.000 0.0631 0.9900 0.9900   1.00  1  1  9.7E-04
  5 :   1.15E+01 9.90E-04 0.318 0.0950 0.9900 0.9900   1.00  1  1  9.4E-05
  6 :   1.15E+01 1.90E-04 0.000 0.1918 0.9060 0.9000   1.00  1  1  1.8E-05
  7 :   1.15E+01 3.42E-05 0.000 0.1802 0.9125 0.9000   1.00  1  1  3.1E-06
  8 :   1.15E+01 5.45E-06 0.000 0.1592 0.9105 0.9000   1.00  1  1  5.2E-07
  9 :   1.15E+01 8.33E-07 0.000 0.1529 0.9068 0.9000   1.00  1  1  8.8E-08
 10 :   1.15E+01 5.77E-08 0.000 0.0693 0.9900 0.9900   1.00  2  2  6.9E-09

iter seconds digits       c*x               b*y
 10      0.1   8.2  1.1483929331e+01  1.1483929255e+01
|Ax-b| =   1.3e-09, [Ay-c]_+ =   1.8E-09, |x|=  5.8e+00, |y|=  3.2e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    8.000E-02    0.000E+00    
Max-norms: ||b||=3.848770e+00, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 8.90459.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +11.4839
Done! 
------------------------------------------------------------------
The optimal point location for the L1-norm case is: 
   -0.0956
    0.1139

The optimal point location for the L2-norm case is: 
    0.1251
    0.1716