annotate matlab/BP/l1qc_logbarrier.m @ 68:cab8a215f9a1 tip

Minor
author Nic Cleju <nikcleju@gmail.com>
date Tue, 09 Jul 2013 14:50:09 +0300
parents 735a0e24575c
children
rev   line source
nikcleju@2 1 % l1qc_logbarrier.m
nikcleju@2 2 %
nikcleju@2 3 % Solve quadratically constrained l1 minimization:
nikcleju@2 4 % min ||x||_1 s.t. ||Ax - b||_2 <= \epsilon
nikcleju@2 5 %
nikcleju@2 6 % Reformulate as the second-order cone program
nikcleju@2 7 % min_{x,u} sum(u) s.t. x - u <= 0,
nikcleju@2 8 % -x - u <= 0,
nikcleju@2 9 % 1/2(||Ax-b||^2 - \epsilon^2) <= 0
nikcleju@2 10 % and use a log barrier algorithm.
nikcleju@2 11 %
nikcleju@2 12 % Usage: xp = l1qc_logbarrier(x0, A, At, b, epsilon, lbtol, mu, cgtol, cgmaxiter)
nikcleju@2 13 %
nikcleju@2 14 % x0 - Nx1 vector, initial point.
nikcleju@2 15 %
nikcleju@2 16 % A - Either a handle to a function that takes a N vector and returns a K
nikcleju@2 17 % vector , or a KxN matrix. If A is a function handle, the algorithm
nikcleju@2 18 % operates in "largescale" mode, solving the Newton systems via the
nikcleju@2 19 % Conjugate Gradients algorithm.
nikcleju@2 20 %
nikcleju@2 21 % At - Handle to a function that takes a K vector and returns an N vector.
nikcleju@2 22 % If A is a KxN matrix, At is ignored.
nikcleju@2 23 %
nikcleju@2 24 % b - Kx1 vector of observations.
nikcleju@2 25 %
nikcleju@2 26 % epsilon - scalar, constraint relaxation parameter
nikcleju@2 27 %
nikcleju@2 28 % lbtol - The log barrier algorithm terminates when the duality gap <= lbtol.
nikcleju@2 29 % Also, the number of log barrier iterations is completely
nikcleju@2 30 % determined by lbtol.
nikcleju@2 31 % Default = 1e-3.
nikcleju@2 32 %
nikcleju@2 33 % mu - Factor by which to increase the barrier constant at each iteration.
nikcleju@2 34 % Default = 10.
nikcleju@2 35 %
nikcleju@2 36 % cgtol - Tolerance for Conjugate Gradients; ignored if A is a matrix.
nikcleju@2 37 % Default = 1e-8.
nikcleju@2 38 %
nikcleju@2 39 % cgmaxiter - Maximum number of iterations for Conjugate Gradients; ignored
nikcleju@2 40 % if A is a matrix.
nikcleju@2 41 % Default = 200.
nikcleju@2 42 %
nikcleju@2 43 % Written by: Justin Romberg, Caltech
nikcleju@2 44 % Email: jrom@acm.caltech.edu
nikcleju@2 45 % Created: October 2005
nikcleju@2 46 %
nikcleju@2 47
nikcleju@2 48 function xp = l1qc_logbarrier(x0, A, At, b, epsilon, lbtol, mu, cgtol, cgmaxiter)
nikcleju@2 49
nikcleju@2 50 largescale = isa(A,'function_handle');
nikcleju@2 51
nikcleju@2 52 if (nargin < 6), lbtol = 1e-3; end
nikcleju@2 53 if (nargin < 7), mu = 10; end
nikcleju@2 54 if (nargin < 8), cgtol = 1e-8; end
nikcleju@2 55 if (nargin < 9), cgmaxiter = 200; end
nikcleju@2 56
nikcleju@2 57 newtontol = lbtol;
nikcleju@2 58 newtonmaxiter = 50;
nikcleju@2 59
nikcleju@2 60 N = length(x0);
nikcleju@2 61
nikcleju@2 62 % starting point --- make sure that it is feasible
nikcleju@2 63 if (largescale)
nikcleju@2 64 if (norm(A(x0)-b) > epsilon)
nikcleju@2 65 disp('Starting point infeasible; using x0 = At*inv(AAt)*y.');
nikcleju@2 66 AAt = @(z) A(At(z));
nikcleju@2 67 w = cgsolve(AAt, b, cgtol, cgmaxiter, 0);
nikcleju@2 68 if (cgres > 1/2)
nikcleju@2 69 disp('A*At is ill-conditioned: cannot find starting point');
nikcleju@2 70 xp = x0;
nikcleju@2 71 return;
nikcleju@2 72 end
nikcleju@2 73 x0 = At(w);
nikcleju@2 74 end
nikcleju@2 75 else
nikcleju@2 76 if (norm(A*x0-b) > epsilon)
nikcleju@2 77 disp('Starting point infeasible; using x0 = At*inv(AAt)*y.');
nikcleju@2 78 opts.POSDEF = true; opts.SYM = true;
nikcleju@2 79 [w, hcond] = linsolve(A*A', b, opts);
nikcleju@2 80 if (hcond < 1e-14)
nikcleju@2 81 disp('A*At is ill-conditioned: cannot find starting point');
nikcleju@2 82 xp = x0;
nikcleju@2 83 return;
nikcleju@2 84 end
nikcleju@2 85 x0 = A'*w;
nikcleju@2 86 end
nikcleju@2 87 end
nikcleju@2 88 x = x0;
nikcleju@2 89 u = (0.95)*abs(x0) + (0.10)*max(abs(x0));
nikcleju@2 90
nikcleju@2 91 disp(sprintf('Original l1 norm = %.3f, original functional = %.3f', sum(abs(x0)), sum(u)));
nikcleju@2 92
nikcleju@2 93 % choose initial value of tau so that the duality gap after the first
nikcleju@2 94 % step will be about the origial norm
nikcleju@2 95 tau = max((2*N+1)/sum(abs(x0)), 1);
nikcleju@2 96
nikcleju@2 97 lbiter = ceil((log(2*N+1)-log(lbtol)-log(tau))/log(mu));
nikcleju@2 98 disp(sprintf('Number of log barrier iterations = %d\n', lbiter));
nikcleju@2 99
nikcleju@2 100 totaliter = 0;
nikcleju@2 101
nikcleju@2 102 % Added by Nic
nikcleju@2 103 if lbiter == 0
nikcleju@2 104 xp = zeros(size(x0));
nikcleju@2 105 end
nikcleju@2 106
nikcleju@2 107 for ii = 1:lbiter
nikcleju@2 108
nikcleju@2 109 [xp, up, ntiter] = l1qc_newton(x, u, A, At, b, epsilon, tau, newtontol, newtonmaxiter, cgtol, cgmaxiter);
nikcleju@2 110 totaliter = totaliter + ntiter;
nikcleju@2 111
nikcleju@2 112 disp(sprintf('\nLog barrier iter = %d, l1 = %.3f, functional = %8.3f, tau = %8.3e, total newton iter = %d\n', ...
nikcleju@2 113 ii, sum(abs(xp)), sum(up), tau, totaliter));
nikcleju@2 114
nikcleju@2 115 x = xp;
nikcleju@2 116 u = up;
nikcleju@2 117
nikcleju@2 118 tau = mu*tau;
nikcleju@2 119
nikcleju@2 120 end
nikcleju@2 121