-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcstep.m
298 lines (291 loc) · 9.88 KB
/
cstep.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
function [stx,fx,dx,sty,fy,dy,stp,fp,dp,brackt,info] ...
= cstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,brackt,stpmin,stpmax)
%CSTEP More-Thuente line search step from MINPACK.
%
% Translation of minpack subroutine cstep
% Dianne O'Leary July 1991
% **********
%
% Subroutine cstep
%
% The purpose of cstep is to compute a safeguarded step for
% a linesearch and to update an interval of uncertainty for
% a minimizer of the function.
%
% The parameter stx contains the step with the least function
% value. The parameter stp contains the current step. It is
% assumed that the derivative at stx is negative in the
% direction of the step. If brackt is set true then a
% minimizer has been bracketed in an interval of uncertainty
% with endpoints stx and sty.
%
% The subroutine statement is
%
% subroutine cstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,brackt,
% stpmin,stpmax,info)
%
% where
%
% stx, fx, and dx are variables which specify the step,
% the function, and the derivative at the best step obtained
% so far. The derivative must be negative in the direction
% of the step, that is, dx and stp-stx must have opposite
% signs. On output these parameters are updated appropriately.
%
% sty, fy, and dy are variables which specify the step,
% the function, and the derivative at the other endpoint of
% the interval of uncertainty. On output these parameters are
% updated appropriately.
%
% stp, fp, and dp are variables which specify the step,
% the function, and the derivative at the current step.
% If brackt is set true then on input stp must be
% between stx and sty. On output stp is set to the new step.
%
% brackt is a logical variable which specifies if a minimizer
% has been bracketed. If the minimizer has not been bracketed
% then on input brackt must be set false. If the minimizer
% is bracketed then on output brackt is set true.
%
% stpmin and stpmax are input variables which specify lower
% and upper bounds for the step.
%
% info is an integer output variable set as follows:
% If info = 1,2,3,4,5, then the step has been computed
% according to one of the five cases below. Otherwise
% info = 0, and this indicates improper input parameters.
%
% Subprograms called
%
% FORTRAN-supplied ... abs,max,min,sqrt
% ... dble
%
% Argonne National Laboratory. MINPACK Project. June 1983
% Jorge J. More', David J. Thuente
%
% **********
% Redistribution and use in source and binary forms, with or
% without modification, are permitted provided that the
% following conditions are met:
%
% 1. Redistributions of source code must retain the above
% copyright notice, this list of conditions and the following
% disclaimer.
%
% 2. Redistributions in binary form must reproduce the above
% copyright notice, this list of conditions and the following
% disclaimer in the documentation and/or other materials
% provided with the distribution.
%
% 3. The end-user documentation included with the
% redistribution, if any, must include the following
% acknowledgment:
%
% "This product includes software developed by the
% University of Chicago, as Operator of Argonne National
% Laboratory.
%
% Alternately, this acknowledgment may appear in the software
% itself, if and wherever such third-party acknowledgments
% normally appear.
%
% 4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
% WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
% UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
% THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
% OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
% OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
% USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
% THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
% DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
% UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
% BE CORRECTED.
%
% 5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
% HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
% ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
% INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
% ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
% PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
% SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
% (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
% EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
% POSSIBILITY OF SUCH LOSS OR DAMAGES.
%
p66 = 0.66;
info = 0;
%
% Check the input parameters for errors.
%
if ((brackt & (stp <= min(stx,sty) | ...
stp >= max(stx,sty))) | ...
dx*(stp-stx) >= 0.0 | stpmax < stpmin)
return
end
%
% Determine if the derivatives have opposite sign.
%
sgnd = dp*(dx/abs(dx));
%
% First case. A higher function value.
% The minimum is bracketed. If the cubic step is closer
% to stx than the quadratic step, the cubic step is taken,
% else the average of the cubic and quadratic steps is taken.
%
if (fp > fx)
info = 1;
bound = 1;
theta = 3*(fx - fp)/(stp - stx) + dx + dp;
s = norm([theta,dx,dp],inf);
gamma = s*sqrt((theta/s)^2 - (dx/s)*(dp/s));
if (stp < stx)
gamma = -gamma;
end
p = (gamma - dx) + theta;
q = ((gamma - dx) + gamma) + dp;
r = p/q;
stpc = stx + r*(stp - stx);
stpq = stx + ((dx/((fx-fp)/(stp-stx)+dx))/2)*(stp - stx);
if (abs(stpc-stx) < abs(stpq-stx))
stpf = stpc;
else
stpf = stpc + (stpq - stpc)/2;
end
brackt = 1;
%
% Second case. A lower function value and derivatives of
% opposite sign. The minimum is bracketed. If the cubic
% step is closer to stx than the quadratic (secant) step,
% the cubic step is taken, else the quadratic step is taken.
%
elseif (sgnd < 0.0)
info = 2;
bound = 0;
theta = 3*(fx - fp)/(stp - stx) + dx + dp;
s = norm([theta,dx,dp],inf);
gamma = s*sqrt((theta/s)^2 - (dx/s)*(dp/s));
if (stp > stx)
gamma = -gamma;
end
p = (gamma - dp) + theta;
q = ((gamma - dp) + gamma) + dx;
r = p/q;
stpc = stp + r*(stx - stp);
stpq = stp + (dp/(dp-dx))*(stx - stp);
if (abs(stpc-stp) > abs(stpq-stp))
stpf = stpc;
else
stpf = stpq;
end
brackt = 1;
%
% Third case. A lower function value, derivatives of the
% same sign, and the magnitude of the derivative decreases.
% The cubic step is only used if the cubic tends to infinity
% in the direction of the step or if the minimum of the cubic
% is beyond stp. Otherwise the cubic step is defined to be
% either stpmin or stpmax. The quadratic (secant) step is also
% computed and if the minimum is bracketed then the the step
% closest to stx is taken, else the step farthest away is taken.
%
elseif (abs(dp) < abs(dx))
info = 3;
bound = 1;
theta = 3*(fx - fp)/(stp - stx) + dx + dp;
s = norm([theta,dx,dp],inf);
%
% The case gamma = 0 only arises if the cubic does not tend
% to infinity in the direction of the step.
%
gamma = s*sqrt(max(0.,(theta/s)^2 - (dx/s)*(dp/s)));
if (stp > stx)
gamma = -gamma;
end
p = (gamma - dp) + theta;
q = (gamma + (dx - dp)) + gamma;
r = p/q;
if (r < 0.0 & gamma ~= 0.0)
stpc = stp + r*(stx - stp);
elseif (stp > stx)
stpc = stpmax;
else
stpc = stpmin;
end
stpq = stp + (dp/(dp-dx))*(stx - stp);
if (brackt)
if (abs(stp-stpc) < abs(stp-stpq))
stpf = stpc;
else
stpf = stpq;
end
else
if (abs(stp-stpc) > abs(stp-stpq))
stpf = stpc;
else
stpf = stpq;
end
end
%
% Fourth case. A lower function value, derivatives of the
% same sign, and the magnitude of the derivative does
% not decrease. If the minimum is not bracketed, the step
% is either stpmin or stpmax, else the cubic step is taken.
%
else
info = 4;
bound = 0;
if (brackt)
theta = 3*(fp - fy)/(sty - stp) + dy + dp;
s = norm([theta,dy,dp],inf);
gamma = s*sqrt((theta/s)^2 - (dy/s)*(dp/s));
if (stp > sty)
gamma = -gamma;
end
p = (gamma - dp) + theta;
q = ((gamma - dp) + gamma) + dy;
r = p/q;
stpc = stp + r*(sty - stp);
stpf = stpc;
elseif (stp > stx)
stpf = stpmax;
else
stpf = stpmin;
end
end
%
% Update the interval of uncertainty. This update does not
% depend on the new step or the case analysis above.
%
if (fp > fx)
sty = stp;
fy = fp;
dy = dp;
else
if (sgnd < 0.0)
sty = stx;
fy = fx;
dy = dx;
end
stx = stp;
fx = fp;
dx = dp;
end
%
% Compute the new step and safeguard it.
%
stpf = min(stpmax,stpf);
stpf = max(stpmin,stpf);
stp = stpf;
if (brackt & bound)
if (sty > stx)
stp = min(stx+p66*(sty-stx),stp);
else
stp = max(stx+p66*(sty-stx),stp);
end
end
return
%
% Last card of subroutine cstep.
%