Skip to content

Commit 0990b55

Browse files
authored
Fix DomainError and calculate number of f/g evaluations correctly in MoreThuente (#76)
* Count nfev correctly * Fix DomainError for sqrt
1 parent deb45fb commit 0990b55

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/morethuente.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,16 @@ function _morethuente!(df,
230230

231231
f = NLSolversBase.value_gradient!(df, x_new)
232232
gdf = NLSolversBase.gradient(df)
233+
nfev += 1 # This includes calls to f() and g!()
233234
iterfinite = 0
234235
while (!isfinite(f) || any(.!isfinite.(gdf))) && iterfinite < iterfinitemax
235236
iterfinite += 1
236237
stp = 0.5*stp
237238
@. x_new = x + stp*s
238239
f = NLSolversBase.value_gradient!(df, x_new)
239240
gdf = NLSolversBase.gradient(df)
241+
nfev += 1 # This includes calls to f() and g!()
242+
240243
# Make stpmax = (3/2)*stp < 2stp in the first iteration below
241244
stx = (7/8)*stp
242245
end
@@ -289,12 +292,12 @@ function _morethuente!(df,
289292

290293
f = NLSolversBase.value_gradient!(df, x_new)
291294
gdf = NLSolversBase.gradient(df)
295+
nfev += 1 # This includes calls to f() and g!()
292296

293297
if isapprox(norm(gdf), 0.0) # TODO: this should be tested vs Optim's g_tol
294298
return stp
295299
end
296300

297-
nfev += 1 # This includes calls to f() and g!()
298301
dg = vecdot(gdf, s)
299302
push!(lsr, stp, f, dg)
300303
ftest1 = finit + stp * dgtest
@@ -564,8 +567,8 @@ function cstep(stx::Real, fx::Real, dgx::Real,
564567
# The case gamma = 0 only arises if the cubic does not tend
565568
# to infinity in the direction of the step
566569
#
567-
#
568-
gamma = (s > zero(s)) ? s * sqrt((theta / s)^2 - (dgx / s) * (dg / s)) : zero(s)
570+
# # Use NaNMath in case s == zero(s)
571+
gamma = s * sqrt(NaNMath.max(zero(s), (theta / s)^2 - (dgx / s) * (dg / s)))
569572

570573
if stp > stx
571574
gamma = -gamma

0 commit comments

Comments
 (0)