-
Notifications
You must be signed in to change notification settings - Fork 34
/
morethuente.jl
664 lines (600 loc) · 20.4 KB
/
morethuente.jl
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# Translation of Matlab version by John Myles White
# Translation of minpack subroutine cvsrch
# Dianne O'Leary July 1991
#
# **********
#
# Subroutine cvsrch
#
# The purpose of cvsrch is to find a step which satisfies
# a sufficient decrease condition and a curvature condition.
# The user must provide a subroutine which calculates the
# function and the gradient.
#
# At each stage the subroutine updates an interval of
# uncertainty with endpoints stx and sty. The interval of
# uncertainty is initially chosen so that it contains a
# minimizer of the modified function
#
# f(x + stp * s) - f(x) - f_tol * stp * (gradf(x)' * s).
#
# If a step is obtained for which the modified function
# has a nonpositive function value and nonnegative derivative,
# then the interval of uncertainty is chosen so that it
# contains a minimizer of f(x + stp * s).
#
# The algorithm is designed to find a step which satisfies
# the sufficient decrease condition
#
# f(x + stp * s) <= f(x) + f_tol * stp * (gradf(x)' * s),
#
# and the curvature condition
#
# abs(gradf(x + stp * s)' * s)) <= gtol * abs(gradf(x)' * s).
#
# If f_tol is less than gtol and if, for example, the function
# is bounded below, then there is always a step which satisfies
# both conditions. If no step can be found which satisfies both
# conditions, then the algorithm usually stops when rounding
# errors prevent further progress. In this case stp only
# satisfies the sufficient decrease condition.
#
# The subroutine statement is
#
# subroutine cvsrch(df,n,x,f,s,stp,f_tol,gtol,x_tol,
# alphamin,alphamax,maxfev,info,nfev,wa)
#
# where
#
# df is the name of the user-supplied subroutine which
# calculates the function and the gradient. df must
# be declared in an external statement in the user
# calling program, and should be written as follows.
#
# function [f,g] = df(n,x) (Matlab)
# (10/2010 change in documentation)
# (derived from Fortran subroutine df(n,x,f,g))
# integer n
# f
# x(n),g(n)
#
# Calculate the function at x and
# return this value in the variable f.
# Calculate the gradient at x and
# return this vector in g.
#
# n is a positive integer input variable set to the number
# of variables.
#
# x is an Abstractarray of length n. On input it must contain the
# base point for the line search. On output it contains
# x + stp * s.
#
# f is a variable. On input it must contain the value of f
# at x. On output it contains the value of f at x + stp * s.
#
# g is an Abstractarray of length n. On input it must contain the
# gradient of f at x. On output it contains the gradient
# of f at x + stp * s.
#
# s is an input Abstractarray of length n which specifies the
# search direction.
#
# stp is a nonnegative variable. On input stp contains an
# initial estimate of a satisfactory step. On output
# stp contains the final estimate.
#
# f_tol and gtol are nonnegative input variables. Termination
# occurs when the sufficient decrease condition and the
# directional derivative condition are satisfied.
#
# x_tol is a nonnegative input variable. Termination occurs
# when the relative width of the interval of uncertainty
# is at most x_tol.
#
# alphamin and alphamax are nonnegative input variables which
# specify lower and upper bounds for the step.
#
# maxfev is a positive integer input variable. Termination
# occurs when the number of calls to df is at least
# maxfev by the end of an iteration.
#
# info is an integer output variable set as follows:
#
# info = 0 Improper input parameters.
#
# info = 1 The sufficient decrease condition and the
# directional derivative condition hold.
#
# info = 2 Relative width of the interval of uncertainty
# is at most x_tol.
#
# info = 3 Number of calls to df has reached maxfev.
#
# info = 4 The step is at the lower bound alphamin.
#
# info = 5 The step is at the upper bound alphamax.
#
# info = 6 Rounding errors prevent further progress.
# There may not be a step which satisfies the
# sufficient decrease and curvature conditions.
# Tolerances may be too small.
#
# nfev is an integer output variable set to the number of
# calls to df.
#
# Argonne National Laboratory. MINPACK Project. June 1983
# Jorge J. More', David J. Thuente
#
# **********
# Returns x, f, stp, info, nfev
# TODO: Decide whether to update x, f, g and info
# or just return step and nfev and let existing code do its job
"""
The line search implementation from:
Moré, Jorge J., and David J. Thuente
Line search algorithms with guaranteed sufficient decrease.
ACM Transactions on Mathematical Software (TOMS) 20.3 (1994): 286-307.
"""
@with_kw struct MoreThuente{T} <: AbstractLineSearch
f_tol::T = 1e-4 # c_1 Wolfe sufficient decrease condition
gtol::T = 0.9 # c_2 Wolfe curvature condition (Recommend 0.1 for GradientDescent)
x_tol::T = 1e-8
alphamin::T = 1e-16
alphamax::T = 65536.0
maxfev::Int = 100
cache::Union{Nothing,LineSearchCache{T}} = nothing
end
function (ls::MoreThuente)(df::AbstractObjective, x::AbstractArray{T},
s::AbstractArray{T}, alpha::Real, x_new::AbstractArray{T},
ϕ_0, dϕ_0) where T
ϕdϕ = make_ϕdϕ(df, x_new, x, s)
ls(ϕdϕ, alpha, ϕ_0, dϕ_0)
end
(ls::MoreThuente)(ϕ, dϕ, ϕdϕ, alpha, ϕ_0, dϕ_0) = ls(ϕdϕ, alpha, ϕ_0, dϕ_0)
# TODO: Should we deprecate the interface that only uses the ϕdϕ argument?
function (ls::MoreThuente)(ϕdϕ,
alpha::T,
ϕ_0,
dϕ_0) where T
@unpack f_tol, gtol, x_tol, alphamin, alphamax, maxfev, cache = ls
emptycache!(cache)
iterfinitemax = -log2(eps(T))
info = 0
info_cstep = 1 # Info from step
zeroT = convert(T, 0)
pushcache!(cache, zeroT, ϕ_0, dϕ_0)
#
# Check the input parameters for errors.
#
if alpha <= zeroT || f_tol < zeroT || gtol < zeroT ||
x_tol < zeroT || alphamin < zeroT || alphamax < alphamin || maxfev <= zeroT
throw(LineSearchException("Invalid parameters to MoreThuente.", 0))
end
if dϕ_0 >= zeroT
throw(LineSearchException("Search direction is not a direction of descent.", 0))
end
#
# Initialize local variables.
#
bracketed = false
stage1 = true
nfev = 0
finit = ϕ_0
dgtest = f_tol * dϕ_0
width = alphamax - alphamin
width1 = 2 * width
# Keep this across calls
#
# The variables stx, fx, dgx contain the values of the step,
# function, and directional derivative at the best step.
# The variables sty, fy, dgy contain the value of the step,
# function, and derivative at the other endpoint of
# the interval of uncertainty.
# The variables alpha, f, dg contain the values of the step,
# function, and derivative at the current step.
#
stx = zeroT
fx = finit
dgx = dϕ_0
sty = zeroT
fy = finit
dgy = dϕ_0
# START: Ensure that the initial step provides finite function values
# This is not part of the original FORTRAN code
if !isfinite(alpha)
alpha = one(T)
end
stmin = stx
stmax = alpha + 4 * (alpha - stx) # Why 4?
alpha = max(alpha, alphamin)
alpha = min(alpha, alphamax)
f, dg = ϕdϕ(alpha)
nfev += 1 # This includes calls to f() and g!()
iterfinite = 0
while (!isfinite(f) || !isfinite(dg)) && iterfinite < iterfinitemax
iterfinite += 1
alpha = alpha/2
f, dg = ϕdϕ(alpha)
nfev += 1 # This includes calls to f() and g!()
# Make stmax = (3/2)*alpha < 2alpha in the first iteration below
stx = (convert(T, 7)/8)*alpha
end
pushcache!(cache, alpha, f, dg)
# END: Ensure that the initial step provides finite function values
# TODO: check if value is finite (maybe iterfinite > iterfinitemax)
while true
#
# Set the minimum and maximum steps to correspond
# to the present interval of uncertainty.
#
if bracketed
stmin = min(stx, sty)
stmax = max(stx, sty)
else
stmin = stx
stmax = alpha + 4 * (alpha - stx) # Why 4?
end
#
# Ensure stmin and stmax (used in cstep) don't violate alphamin and alphamax
# Not part of original FORTRAN translation
#
stmin = max(alphamin,stmin)
stmax = min(alphamax,stmax)
#
# Force the step to be within the bounds alphamax and alphamin
#
alpha = max(alpha, alphamin)
alpha = min(alpha, alphamax)
#
# If an unusual termination is to occur then let
# alpha be the lowest point obtained so far.
#
if (bracketed && (alpha <= stmin || alpha >= stmax)) ||
nfev >= maxfev-1 || info_cstep == 0 ||
(bracketed && stmax - stmin <= x_tol * stmax)
alpha = stx
end
#
# Evaluate the function and gradient at alpha
# and compute the directional derivative.
#
f, dg = ϕdϕ(alpha)
pushcache!(cache, alpha, f, dg)
nfev += 1 # This includes calls to f() and g!()
if isapprox(dg, 0, atol=eps(T)) # Should add atol value to MoreThuente
return alpha, f
end
ftest1 = finit + alpha * dgtest
#
# Test for convergence.
#
# What does info_cstep stand for?
if (bracketed && (alpha <= stmin || alpha >= stmax)) || info_cstep == 0
info = 6
end
if alpha == alphamax && f <= ftest1 && dg <= dgtest
info = 5
end
if alpha == alphamin && (f > ftest1 || dg >= dgtest)
info = 4
end
if nfev >= maxfev
info = 3
end
if bracketed && stmax - stmin <= x_tol * stmax
info = 2
end
if f <= ftest1 && abs(dg) <= -gtol * dϕ_0
info = 1
end
#
# Check for termination.
#
if info != 0
return alpha, f
end
#
# In the first stage we seek a step for which the modified
# function has a nonpositive value and nonnegative derivative.
#
if stage1 && f <= ftest1 && dg >= min(f_tol, gtol) * dϕ_0
stage1 = false
end
#
# A modified function is used to predict the step only if
# we have not obtained a step for which the modified
# function has a nonpositive function value and nonnegative
# derivative, and if a lower function value has been
# obtained but the decrease is not sufficient.
#
if stage1 && f <= fx && f > ftest1
#
# Define the modified function and derivative values.
#
fm = f - alpha * dgtest
fxm = fx - stx * dgtest
fym = fy - sty * dgtest
dgm = dg - dgtest
dgxm = dgx - dgtest
dgym = dgy - dgtest
#
# Call cstep to update the interval of uncertainty
# and to compute the new step.
#
stx, fxm, dgxm,
sty, fym, dgym,
alpha, fm, dgm,
bracketed, info_cstep =
cstep(stx, fxm, dgxm, sty, fym, dgym,
alpha, fm, dgm, bracketed, stmin, stmax)
#
# Reset the function and gradient values for f.
#
fx = fxm + stx * dgtest
fy = fym + sty * dgtest
dgx = dgxm + dgtest
dgy = dgym + dgtest
else
#
# Call cstep to update the interval of uncertainty
# and to compute the new step.
#
stx, fx, dgx,
sty, fy, dgy,
alpha, f, dg,
bracketed, info_cstep =
cstep(stx, fx, dgx, sty, fy, dgy,
alpha, f, dg, bracketed, stmin, stmax)
end
#
# Force a sufficient decrease in the size of the
# interval of uncertainty.
#
if bracketed
if abs(sty - stx) >= (convert(T, 2)/3) * width1
alpha = stx + (sty - stx)/2
end
width1 = width
width = abs(sty - stx)
end
end # while
end # function
# 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 bracketed 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, dgx,
# sty, fy, dgy,
# stp, f, dg,
# bracketed, alphamin, alphamax, info)
#
# where
#
# stx, fx, and dgx 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, dgx and stp-stx must have opposite
# signs. On output these parameters are updated appropriately
#
# sty, fy, and dgy 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, f, and dg are variables which specify the step,
# the function, and the derivative at the current step.
# If bracketed is set true then on input stp must be
# between stx and sty. On output stp is set to the new step
#
# bracketed is a logical variable which specifies if a minimizer
# has been bracketed. If the minimizer has not been bracketed
# then on input bracketed must be set false. If the minimizer
# is bracketed then on output bracketed is set true
#
# alphamin and alphamax 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
#
# Argonne National Laboratory. MINPACK Project. June 1983
# Jorge J. More', David J. Thuente
function cstep(stx::Real, fx::Real, dgx::Real,
sty::Real, fy::Real, dgy::Real,
alpha::Real, f::Real, dg::Real,
bracketed::Bool, alphamin::Real, alphamax::Real)
T = promote_type(typeof(stx), typeof(fx), typeof(dgx), typeof(sty), typeof(fy), typeof(dgy), typeof(alpha), typeof(f), typeof(dg), typeof(alphamin), typeof(alphamax))
zeroT = convert(T, 0)
info = 0
#
# Check the input parameters for error
#
if (bracketed && (alpha <= min(stx, sty) || alpha >= max(stx, sty))) ||
dgx * (alpha - stx) >= zeroT || alphamax < alphamin
throw(ArgumentError("Minimizer not bracketed"))
end
#
# Determine if the derivatives have opposite sign
#
sgnd = dg * (dgx / abs(dgx))
#
# 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 f > fx
info = 1
bound = true
theta = 3 * (fx - f) / (alpha - stx) + dgx + dg
# Use s to prevent overflow/underflow of theta^2 and dgx * dg
s = max(abs(theta), abs(dgx), abs(dg))
gamma = s * sqrt((theta / s)^2 - (dgx / s) * (dg / s))
if alpha < stx
gamma = -gamma
end
p = gamma - dgx + theta
q = gamma - dgx + gamma + dg
r = p / q
alphac = stx + r * (alpha - stx)
alphaq = stx + (dgx / ((fx - f) / (alpha - stx) + dgx)) / 2 * (alpha - stx)
if abs(alphac - stx) < abs(alphaq - stx)
alphaf = alphac
else
alphaf = (alphac + alphaq) / 2
end
bracketed = true
#
# 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 < zeroT
info = 2
bound = false
theta = 3 * (fx - f) / (alpha - stx) + dgx + dg
# Use s to prevent overflow/underflow of theta^2 and dgx * dg
s = max(abs(theta), abs(dgx), abs(dg))
gamma = s * sqrt((theta / s)^2 - (dgx / s) * (dg / s))
if alpha > stx
gamma = -gamma
end
p = gamma - dg + theta
q = gamma - dg + gamma + dgx
r = p / q
alphac = alpha + r * (stx - alpha)
alphaq = alpha + (dg / (dg - dgx)) * (stx - alpha)
if abs(alphac - alpha) > abs(alphaq - alpha)
alphaf = alphac
else
alphaf = alphaq
end
bracketed = true
#
# 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 alpha. Otherwise the cubic step is defined to be
# either alphamin or alphamax. 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(dg) < abs(dgx)
info = 3
bound = true
theta = 3 * (fx - f) / (alpha - stx) + dgx + dg
# Use s to prevent overflow/underflow of theta^2 and dgx * dg
s = max(abs(theta), abs(dgx), abs(dg))
#
# The case gamma = 0 only arises if the cubic does not tend
# to infinity in the direction of the step
#
# # Use NaNMath in case s == zero(s)
gamma = s * sqrt(NaNMath.max(zero(s), (theta / s)^2 - (dgx / s) * (dg / s)))
if alpha > stx
gamma = -gamma
end
p = gamma - dg + theta
q = gamma + dgx - dg + gamma
r = p / q
if r < zeroT && gamma != zeroT
alphac = alpha + r * (stx - alpha)
elseif alpha > stx
alphac = alphamax
else
alphac = alphamin
end
alphaq = alpha + (dg / (dg - dgx)) * (stx - alpha)
if bracketed
if abs(alpha - alphac) < abs(alpha - alphaq)
alphaf = alphac
else
alphaf = alphaq
end
else
if abs(alpha - alphac) > abs(alpha - alphaq)
alphaf = alphac
else
alphaf = alphaq
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 alphamin or alphamax, else the cubic step is taken
#
else
info = 4
bound = false
if bracketed
theta = 3 * (f - fy) / (sty - alpha) + dgy + dg
# Use s to prevent overflow/underflow of theta^2 and dgy * dg
s = max(abs(theta), abs(dgy), abs(dg))
gamma = s * sqrt((theta / s)^2 - (dgy / s) * (dg / s))
if alpha > sty
gamma = -gamma
end
p = gamma - dg + theta
q = gamma - dg + gamma + dgy
r = p / q
alphac = alpha + r * (sty - alpha)
alphaf = alphac
elseif alpha > stx
alphaf = alphamax
else
alphaf = alphamin
end
end
#
# Update the interval of uncertainty. This update does not
# depend on the new step or the case analysis above
#
if f > fx
sty = alpha
fy = f
dgy = dg
else
if sgnd < zeroT
sty = stx
fy = fx
dgy = dgx
end
stx = alpha
fx = f
dgx = dg
end
#
# Compute the new step and safeguard it
#
alphaf = min(alphamax, alphaf)
alphaf = max(alphamin, alphaf)
alpha = alphaf
if bracketed && bound
if sty > stx
alpha = min(stx + (convert(T, 2)/3) * (sty - stx), alpha)
else
alpha = max(stx + (convert(T, 2)/3) * (sty - stx), alpha)
end
end
return stx, fx, dgx, sty, fy, dgy, alpha, f, dg, bracketed, info
end