comparison matlab/BP/l1qc_logbarrier.m @ 2:735a0e24575c

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