-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathloopPcg.m
170 lines (168 loc) · 5.94 KB
/
loopPcg.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
function [y,k, DAy] = loopPcg(L,Lden,At,dense,d, DAt,K, b,p,ssqrNew,cgpars, restol)
% [y,k, DAy] = loopPcg(L,Lden,At,dense,d, DAt,K, b,p,ssqrNew,cgpars, restol)
%
% LOOPPCG Solve y from AP(d)A' * y = b
% using PCG-method and Cholesky L as conditioner.
% If L is sufficiently accurate, then only 1 CG-step is needed.
% It assumes that the previous step was p, with
% ssqrNew = bOld'*inv(L*THETA*L')*bOld, and bOld the residual before
% the step p was taken. If p = [], then the PCG is started from scratch.
%
% k = #(CG-iterations).
% DAy = D*A'*y with D'*D = P(d).
%
% Warning: if the scaling operation P(d) gets ill-conditioned, the
% precision in y may be insufficient to compute DAy satisfactory.
% In this case, one should allow refinement, see wrapPcg.
%
% ********** INTERNAL FUNCTION OF SEDUMI **********
%
% See also sedumi, wrapPcg
% This file is part of SeDuMi 1.1 by Imre Polik and Oleksandr Romanko
% Copyright (C) 2005 McMaster University, Hamilton, CANADA (since 1.1)
%
% Copyright (C) 2001 Jos F. Sturm (up to 1.05R5)
% Dept. Econometrics & O.R., Tilburg University, the Netherlands.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% Affiliation SeDuMi 1.03 and 1.04Beta (2000):
% Dept. Quantitative Economics, Maastricht University, the Netherlands.
%
% Affiliations up to SeDuMi 1.02 (AUG1998):
% CRL, McMaster University, Canada.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
% 02110-1301, USA
%
% --------------------------------------------------
% INITIALIZE:
% y=0 with r = b.
% --------------------------------------------------
k = 0;
STOP = 0;
r = b;
finew = 0;
y = []; % means all-0
normrmin = norm(r,inf);
ymin = [];
% --------------------------------------------------
% Conjugate Gradient until convergence
% --------------------------------------------------
while STOP == 0
% --------------------------------------------------
% P r e - C o n d i t i o n i n g:
% Solve L*Lr = r, L*THETA*tmp = r.
% p=[] ==> initialize p to solve L*THETA*L'*p = r.
% --------------------------------------------------
Lr = fwdpr1(Lden,sparfwslv(L, r));
tmp = Lr ./ L.d;
if isempty(p)
ssqrNew = Lr'*tmp;
p = sparbwslv(L, bwdpr1(Lden,tmp));
else
% --------------------------------------------------
% General iterate: make p conjugate to previous iterate(s):
% --------------------------------------------------
ssqrOld = ssqrNew;
ssqrNew = Lr'*tmp;
p = (ssqrNew/ssqrOld) * p;
p = p + sparbwslv(L, bwdpr1(Lden,tmp));
end
% --------------------------------------------------
% SCALING OPERATION AND MATRIX*VECTOR.
% Let DDAp = P(d)*A'*p and ssqrDAp = ||P(d)^{1/2}*A'*p||^2.
% Set alpha = ssqrNew / ssqrDAp
% --------------------------------------------------
Ap = vecsym(Amul(At,dense,p,1), K);
[DDAp, DApq, DAps, ssqrDAp] = PopK(d,Ap,K);
if ssqrDAp > 0.0
k = k + 1;
% --------------------------------------------------
% Take step: y := y + alpha*p
%--------------------------------------------------
alpha = ssqrNew / ssqrDAp;
if ~isempty(y)
if isstruct(y)
[y.hi,y.lo] = quadadd(y.hi,y.lo,alpha*p);
else
y = y + alpha * p;
end
elseif cgpars.qprec > 0
y.hi = alpha * p; y.lo = zeros(length(p),1);
else
y = alpha * p;
end
% --------------------------------------------------
% Update residual r := r - alpha * A*[P(d)*Ap]. MATRIX*VECTOR.
% --------------------------------------------------
tmp = Amul(At,dense,DDAp)+ DAt.q'*DApq(:,1) +...
DAt.denq*DApq(dense.q,1);
r = r - alpha * tmp;
% --------------------------------------------------
% Convergence check (HEURISTIC)
% --------------------------------------------------
fiprev = finew;
if isstruct(y)
finew = (b+r)'*y.hi + (b+r)'*y.lo;
else
finew = (b+r)'*y;
end
normr = norm(r,inf);
if normr < normrmin
ymin = y;
normrmin = normr;
end
if normr < restol
STOP = 1;
elseif (finew-fiprev < cgpars.stagtol * fiprev)
STOP = 2;
elseif k >= cgpars.maxiter
STOP = 2;
end
else
% my_fprintf('Warning: DAp = 0 in PCG\n');
STOP = 1; % If DAp == 0 then can't go on.
end
end
% --------------------------------------------------
% OUTPUT: return projection D * At*y on request.
% --------------------------------------------------
if STOP == 2
y = ymin; % Take best so far
end
if isempty(y)
DAy = [];
return
end
if nargout >= 3
if k == 1
DAy = alpha*[sqrt(d.l).*Ap(1:K.l); asmDxq(d,Ap,K,DApq); DAps];
else
if isstruct(y)
Ap = vecsym(Amul(At,dense,y.hi,1), K);
else
Ap = vecsym(Amul(At,dense,y,1), K);
end
DAy = [sqrt(d.l).*Ap(1:K.l); asmDxq(d,Ap,K); psdscale(d,Ap,K)];
if isstruct(y)
Ap = vecsym(Amul(At,dense,y.lo,1), K);
DAy = DAy + [sqrt(d.l).*Ap(1:K.l); asmDxq(d,Ap,K); psdscale(d,Ap,K)];
end
end
end
if isstruct(y)
y = y.hi;
end