-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPNKH.m
383 lines (348 loc) · 14.1 KB
/
PNKH.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
function [xc,his,xAll] = PNKH(fun,xc,varargin)
% [xc,his,xAll] = PNKH(fun,xc,varargin)
%
% Lanzcos-based projected Newton method for bound constrained problem
%
% min_x f(x) s.t. low <= x <= up
%
% Required Inputs:
%
% fun - objective funciton which takes one argument, the current variable,
% and it outputs [f, df, H, MRwholeinv, MLwholeinv, MRwhole, ...
% MLwhole, Err],
% where f is objective function value, df is gradient, H is Hessian
% MRwholeinv, MLwholeinv, MRwhole, MLwhole are preconditioners and
% their inverses,
% Err is a row vector containing the training and validation error
% xc - initial guess
%
% Optional Inputs (configured via varargin, see code for defaults)
%
% xTol - Relative change (||x_{k+1}-x_k||_2/||x_k||_2) tolerance
% gTol - Projected gradient tolerance
% cgTol - CG tolerance
% qpTol - Quadratic programming tolerance
% qpIter - Quadratic programm MaxIter
% maxIterCG - Numbers of CG iteration as a vector
% out - Print option, 0-> don't print, 1-> print each iteration
% alpha - Line search parameter
% epsilon - Width of the boundary
% maxStep - Max step size
% factor_c - Factor for Hessian shift in the orthogonal complement
% (if specified, we will shift H by factor_c*largest eigenvalue of T)
% (if not specified, we will shift H by c)
% c - Constant for Hessian shift in the orthogonal complement
% (The shift is implemented if factor_c is not specified)
% low - Lower bound, put a large negative number if no bound
% up - Upper bound, put a large positive number if no bound
% indexing - Indexing method
% quadProgSolver - QP solver
%
% Outputs
%
% xc - Optimal variable
% his - Structure containing the history of iterations
% his.str: corresponding value in each column
% his.obj: values of history of iterations,
% each row is one iteration and is
% [objtive function value, norm of restricted gradient, ...
% percentage of active variables, CG relative error, ...
% training error, validation error, mu, number of function evaluations];
% xAll - Collection of all x's at all iterations
if nargin==0
E = @(x,varargin) Rosenbrock(x);
W = [2;2];
[xc,his,xAll] = feval(mfilename,E,W,...
'low',1.3*ones(2,1),'up',2.2*ones(2,1),'cgTol',1e-16,...
'indexing', 'Augmented');
fprintf('numerical solution: x = [%1.4f, %1.4f]\n',xc);
figure(1); clf;
subplot(1,2,1)
x1= linspace(1.3,2.2,128);
x2= linspace(1.3,2.2,128);
[X1,X2] = meshgrid(x1,x2);
F = reshape(Rosenbrock([X1(:) X2(:)]'),128,128);
[fmin,idmin] = min(F(:));
xmin = [X1(idmin); X2(idmin)];
contour(X1,X2,F,100,'LineWidth',3)
hold on;
plot(xmin(1),xmin(2),'sb','MarkerSize',50);
plot(xc(1),xc(2),'.r','MarkerSize',30);
subplot(1,2,2)
semilogy(his.obj(:,2),'LineWidth',3);
set(gca,'FontSize',20);
title('optimality condition');
return
end
size_prob = size(xc,1);
xTol = 1e-6; %Relative change tolerance
gTol = 1e-8; %Projected gradient tolerance
cgTol = 1e-16; %CG tolerance
qpTol = 1e-12; %Quadratic programming tolerance
qpIter = 200; %Quadratic programm MaxIter
maxIterCG = ones(1,10)*9; %number of CG iterations
out = 1; %print option
alpha = 0; %line search parameter
epsilon = 1e-6; %width of the boundary
maxStep = inf; %max step size
c = 1e-3; %constant for Hessian shift
low = -1e16*ones(size_prob,1); %lower bound
up = 1e16*ones(size_prob,1); %upper bound
indexing = 'Augmented'; %indexing method
quadProgSolver = 'interiorQP'; %QP solver
for k=1:2:length(varargin) % overwrites default parameter
eval([varargin{k},'=varargin{',int2str(k+1),'};']);
end
xAll = [];
[fc,df,H,MRwholeinv,MLwholeinv,MRwhole,MLwhole,Err] = fun(xc);
maxIter = numel(maxIterCG);
mu = 1;
if isempty(Err)
his.str = 'iter\t obj func\tnorm(grad)\tActive \t CGrelerr \n';
his.val = '%3d.0\t%3.2e\t%3.2e\t%3.2e\t%3.2e\n';
else
his.str = 'iter\t obj func\tnorm(grad)\tActive \t CGrelerr \t\tTrain Error \tVali Error \n';
his.val = '%3d.0\t%3.2e\t%3.2e\t%3.2e\t%3.2e\t%8.2f\t%10.2f \n';
end
his.obj = zeros(maxIter+1,6+2*(~isempty(Err)));
if out==1
fprintf('=== %s (maxIter: %d, maxIterCG(1)=%d, cgTol=%1.2e, qpTol=%1.2e) ===\n',mfilename,maxIter,maxIterCG(1),cgTol,qpTol);
fprintf(his.str);
end
for j=1:maxIter
r = maxIterCG(j);
maxCG = r + 1; %Lanczos counts one less iteration
%when compared to CG
s = zeros(size_prob, 1);
switch indexing
case 'Boundary'
Jk = (xc <= low + epsilon) | (xc >= up - epsilon);
Fk = not(Jk);
case 'Augmented'
Jk = ((xc <= low + epsilon) & (df > 0)) | ...
((xc>= up - epsilon) & (df < 0));
Fk = not(Jk);
otherwise % no indexing
Jk = zeros(size_prob, 1);
Fk = ones(size_prob, 1);
end
Jk = find(Jk==1); Fk = find(Fk==1);
if norm(df(Fk)) && ~isempty(Fk)
MRinv = @(v) MRinv_temp(v, size_prob, MRwholeinv, Fk);
MLinv = @(v) MLinv_temp(v, size_prob, MLwholeinv, Fk);
MR = @(v) MR_temp(v, size_prob, MRwhole, Fk);
ML = @(v) ML_temp(v, size_prob, MLwhole, Fk);
PCH = @(v) rest_Hess(v,H,Fk,size_prob,MRinv,MLinv);
if nargout>2; xAll = [xAll xc]; end;
[Activedf, Activeidx] = projectGradient(df,xc,low,up);
[T,V] = lanczosTridiag(PCH,MLinv(df(Fk)),maxCG,cgTol,1);
s(Fk) = MRinv(V*(T\(V'*MLinv(-df(Fk)))));
[V, QR_R] = qr(ML(V), 0);
T = QR_R*T*QR_R';
[eig_V, eig_D] = eig(T);
V = V*eig_V;
if exist('factor_c', 'var')
c = eig_D(end,end)*factor_c;
end
T = eig_D-c*eye(size(eig_D,1));
LowRankH = @(v,s) V*(T*(V'*v))+s.*v;
LowRankHinv = @(v,s) LowRankHinv_temp(v, s, V, T);
CGrelerr = norm(PCH(MR(s(Fk)))+MLinv(df(Fk)))/norm(MLinv(df(Fk)));
his.obj(j,1:end-2) = [fc,norm(Activedf),nnz(Activeidx)/numel(xc),CGrelerr,Err];
% Compute Lanczos relative error (consistent with Matlab pcg)
if out==1; fprintf(his.val,j,his.obj(j,1:end-2)); end
if his.obj(j,2) < gTol %if projected gradient is too small, stop
fprintf('Iteration stops early because norm(ProjGrad)<gTol');
fprintf('\n')
break
end
if j>1 && xchange < xTol
fprintf('Iteration stops early because ||x_{k+1}-x_k||_2/||x_k||_2<xTol')
fprintf('\n')
break
end
%------------------Rescale Inactive step-------------------------------
if max(abs(s(Fk))) > maxStep
fprintf('maxstep Reached...\n');
s(Fk) = s(Fk)/max(abs(s(Fk)))*maxStep;
end
%------------------Rescale Active step---------------------------------
s_Jk = -df(Jk);
switch indexing
case 'Boundary' %Rescale gradient based on Newton step
ga = -df(xc <= low + epsilon);
if max(abs(ga)) > max(abs(s(Fk))), ga = ga/max(abs(ga))*max(abs(s(Fk))); end
s(xc <= low + epsilon) = ga;
ga = -df(xc >= up - epsilon);
if max(abs(ga)) > max(abs(s(Fk))), ga = ga/max(abs(ga))*max(abs(s(Fk))); end
s(xc >= up - epsilon) = ga;
case 'Augmented' %Rescale gradient based on epsilon of augmented index
ga = s_Jk(s_Jk<0);
if max(abs(ga)) > epsilon, ga = ga/max(abs(ga))*epsilon; end
s_Jk(s_Jk<0) = ga;
ga = s_Jk(s_Jk>0);
if max(abs(ga)) > epsilon, ga = ga/max(abs(ga))*epsilon; end
s_Jk(s_Jk>0) = ga;
end
s(Jk) = s_Jk;
else
%if norm(dk(Fk))=0 or Fk is empty,
%then we only use gradient descent
s = -df/norm(df);
end
% Armijo line search
cnt = 1;
while 1
xtry = xc(Fk) + mu*s(Fk);
% projection
% QP solver
if norm(df(Fk)) && ~isempty(Fk)
%if norm(dk(Fk))=0 or Fk is empty,
%then we only use gradient descent
%and we skip QP
if strcmp(quadProgSolver, 'matlabQuadProg')
options = optimset('Display','off');
Hv = V*T*V'+c*speye(size(V,1));
Hv = (Hv+Hv')/2;
xtry = quadprog(Hv,-Hv*xtry,[],[],[],[],low(Fk),up(Fk),[],options);
elseif strcmp(quadProgSolver, 'interiorQP')
[xtry, error_xtry] = interiorQPB(LowRankH,LowRankHinv,xtry,low(Fk),up(Fk),...
'out',0, 'tol', qpTol, 'c', c, 'maxIter', qpIter);
end
end
temp_xtry = xtry;
xtry = zeros(size_prob,1);
xtry(Fk) = temp_xtry;
temp_xtry2 = xc(Jk) + mu * s(Jk);
xtry(Jk) = min(max(temp_xtry2,low(Jk)),up(Jk));
objtry = fun(xtry);
if out==1; fprintf('%3d.%d\t%3.2e\t mu=%3.2e\n',j,cnt,objtry,mu); end
if objtry < fc + alpha * dot(df, xtry-xc)
break
end
mu = mu/2;
cnt = cnt+1;
if cnt > 10
warning('Line search break');
return;
end
end
if cnt == 1
mu = min(mu*1.5,1);
end
his.obj(j,end-1:end) = [mu, cnt];
xchange = norm(xtry-xc)/norm(xc);
xc = xtry;
[fc,df,H,MRwholeinv,MLwholeinv,MRwhole,MLwhole,Err] = fun(xc);
end
if his.obj(maxIter,end) ~= 0 %If it does not stop early, get information of the last iter
if nargout>2; xAll = [xAll xc]; end;
[Activedf, Activeidx] = projectGradient(df,xc,low,up);
his.obj(maxIter+1,1:end-2) = [fc,norm(Activedf),nnz(Activeidx)/numel(xc),0,Err];
else % If it stops early
his.obj = his.obj(1:j,:);
end
end
function x = rest_Hess(v,H,Fk,size_prob,MRinv,MLinv)
% preconditioned restricted Hessian (function handle)
% Required inputs:
% v - vector with length equals to Fk
% H - Original Hessian (function handle)
% Fk - index of inactive sets
% size_prob - size of the problem
% MRinv - right preconditioner s.t. MLinv*H*MRinv*(MR*x) = MLinv*(-\nabla f)
% MLinv - left preconditioner s.t. MLinv*H*MRinv*(MR*x) = MLinv*(-\nabla f)
%
% Output:
% x - H|_{Fk} * v|_{Fk}, where |_{Fk} means restricted onto Fk
temp = zeros(size_prob,1);
temp(Fk) = MRinv(v);
x_with_zero = H(temp);
x = MLinv(x_with_zero(Fk));
end
% function x = rest_Hess_nopre(v,H,Fk,size_prob) %restricted Hessian, input: restricted vector
% temp = zeros(size_prob,1);
% temp(Fk) = (v);
% x_with_zero = H(temp);
% x = x_with_zero(Fk);
% end
function x = MR_temp(v, size_prob, MRwhole, Fk)
% restricted right preconditioner (function handle)
% Required inputs:
% v - vector with length equals to Fk
% size_prob - size of the problem
% MRwhole - Orginal right precondtioner
% Fk - index of inactive sets
%
% Output:
% x - MRwhole|_{Fk} * v|_{Fk}, where |_{Fk} means restricted onto Fk
x = zeros(size_prob,size(v,2));
x(Fk,:) = v;
x = MRwhole(x);
x = x(Fk,:);
end
function x = ML_temp(v, size_prob, MLwhole, Fk)
% restricted left preconditioner (function handle)
% Required inputs:
% v - vector with length equals to Fk
% size_prob - size of the problem
% MLwhole - Orginal left precondtioner
% Fk - index of inactive sets
%
% Output:
% x - MLwhole|_{Fk} * v|_{Fk}, where |_{Fk} means restricted onto Fk
x = zeros(size_prob,size(v,2));
x(Fk,:) = v;
x = MLwhole(x);
x = x(Fk,:);
end
function x = MRinv_temp(v, size_prob, MRwholeinv, Fk)
% restricted inverse right preconditioner (function handle)
% Required inputs:
% v - vector with length equals to Fk
% size_prob - size of the problem
% MRwholeinv - Orginal inverse right precondtioner
% Fk - index of inactive sets
%
% Output:
% x - MRwholeinv|_{Fk} * v|_{Fk}, where |_{Fk} means restricted onto Fk
x = zeros(size_prob,size(v,2));
x(Fk,:) = v;
x = MRwholeinv(x);
x = x(Fk,:);
end
function x = MLinv_temp(v, size_prob, MLwholeinv, Fk)
% restricted inverse right preconditioner (function handle)
% Required inputs:
% v - vector with length equals to Fk
% size_prob - size of the problem
% MLwholeinv - Orginal inverse left precondtioner
% Fk - index of inactive sets
%
% Output:
% x - MLwholeinv|_{Fk} * v|_{Fk}, where |_{Fk} means restricted onto Fk
x = zeros(size_prob,size(v,2));
x(Fk,:) = v;
x = MLwholeinv(x);
x = x(Fk,:);
end
function x = LowRankHinv_temp(v, s, V, T)
% The inverse of the low-rank approximated Hessian (function handle)
% Required inputs:
% v - vector with length equals to Fk
% s - vector that shifts the Hessian to ensure symmetric
% positive definiteness
% V, T - output of Lanczos tridiagonalization
% MRinv, MLinv - inverse preconditioner restricted onto Fk
% VRT - ML*V*R', where T=R'*R, cholesky factorization of T
%
% Output:
% x - (V*T*V'+diag(s))^(-1)*v
if norm(s) == 0
x = V*(T\(V'*v));
else
v1 = v./s;
v2 = (inv(T)+V'*diag(sparse(1./s))*V)\(V'*v1);
x = v1 - (V*v2)./s;
end
end