Skip to content

Commit 11d039f

Browse files
authored
Remove use of implicit NLSolversBase.value and NLSolversBase.gradient (#190)
1 parent 07963b7 commit 11d039f

File tree

4 files changed

+14
-42
lines changed

4 files changed

+14
-42
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "LineSearches"
22
uuid = "d3d80556-e9d4-5f37-9878-2ab0fcc64255"
3-
version = "7.5.0"
3+
version = "7.5.1"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/LineSearches.jl

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ function make_ϕdϕ(df, x_new, x, s)
2929
x_new .= x .+ α.*s
3030

3131
# Evaluate ∇f(x+α*s)
32-
NLSolversBase.value_gradient!(df, x_new)
32+
f_x_new, g_x_new = NLSolversBase.value_gradient!(df, x_new)
3333

3434
# Calculate ϕ(a_i), ϕ'(a_i)
35-
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
35+
return f_x_new, real(dot(g_x_new, s))
3636
end
3737
ϕdϕ
3838
end
@@ -42,48 +42,18 @@ function make_ϕ_dϕ(df, x_new, x, s)
4242
x_new .= x .+ α.*s
4343

4444
# Evaluate ∇f(x+α*s)
45-
NLSolversBase.gradient!(df, x_new)
45+
g_x_new = NLSolversBase.gradient!(df, x_new)
4646

4747
# Calculate ϕ'(a_i)
48-
real(dot(NLSolversBase.gradient(df), s))
48+
return real(dot(g_x_new, s))
4949
end
5050
make_ϕ(df, x_new, x, s), dϕ
5151
end
5252
function make_ϕ_dϕ_ϕdϕ(df, x_new, x, s)
53-
function (α)
54-
# Move a distance of alpha in the direction of s
55-
x_new .= x .+ α.*s
56-
57-
# Evaluate f(x+α*s) and ∇f(x+α*s)
58-
NLSolversBase.gradient!(df, x_new)
59-
60-
# Calculate ϕ'(a_i)
61-
real(dot(NLSolversBase.gradient(df), s))
62-
end
63-
function ϕdϕ(α)
64-
# Move a distance of alpha in the direction of s
65-
x_new .= x .+ α.*s
66-
67-
# Evaluate ∇f(x+α*s)
68-
NLSolversBase.value_gradient!(df, x_new)
69-
70-
# Calculate ϕ'(a_i)
71-
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
72-
end
73-
make_ϕ(df, x_new, x, s), dϕ, ϕdϕ
53+
make_ϕ_dϕ(df, x_new, x, s)..., make_ϕdϕ(df, x_new, x, s)
7454
end
7555
function make_ϕ_ϕdϕ(df, x_new, x, s)
76-
function ϕdϕ(α)
77-
# Move a distance of alpha in the direction of s
78-
x_new .= x .+ α.*s
79-
80-
# Evaluate ∇f(x+α*s)
81-
NLSolversBase.value_gradient!(df, x_new)
82-
83-
# Calculate ϕ'(a_i)
84-
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
85-
end
86-
make_ϕ(df, x_new, x, s), ϕdϕ
56+
make_ϕ(df, x_new, x, s), make_ϕdϕ(df, x_new, x, s)
8757
end
8858

8959
include("types.jl")

src/initialguess.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function (is::InitialQuadratic{T})(ls, state, phi_0, dphi_0, df) where T
7474
# If we're at the first iteration
7575
αguess = is.α0
7676
else
77-
αguess = 2 * (NLSolversBase.value(df) - state.f_x_previous) / dphi_0
77+
αguess = 2 * (phi_0 - state.f_x_previous) / dphi_0
7878
αguess = NaNMath.max(is.αmin, state.alpha*is.ρ, αguess)
7979
αguess = NaNMath.min(is.αmax, αguess)
8080
# if αguess ≈ 1, then make it 1 (Newton-type behaviour)
@@ -180,8 +180,10 @@ function (is::InitialHagerZhang)(ls::Tls, state, phi_0, dphi_0, df) where Tls
180180
# and the user has not provided an initial step size (is.α0 is NaN),
181181
# then we
182182
# pick the initial step size according to HZ #I0
183-
state.alpha = _hzI0(state.x, NLSolversBase.gradient(df),
184-
NLSolversBase.value(df),
183+
# phi_0 is (or should be) equal to NLSolversBase.value(df, state.x)
184+
# TODO: Make the gradient available as part of the state?
185+
g_x = NLSolversBase.gradient!(df, state.x)
186+
state.alpha = _hzI0(state.x, g_x, phi_0,
185187
is.αmax,
186188
convert(eltype(state.x), is.ψ0)) # Hack to deal with type instability between is{T} and state.x
187189
if Tls <: HagerZhang

test/initial.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@
9090
state.f_x_previous = 2*phi_0
9191
is = InitialQuadratic(snap2one=(0.9,Inf))
9292
is(ls, state, phi_0, dphi_0, df)
93-
@test state.alpha == 1.0
93+
@test state.alpha == 0.5
9494
@test ls.mayterminate[] == false
9595

9696
# Test Quadratic snap2one
9797
ls = HagerZhang()
9898
state = getstate()
9999
state.f_x_previous = 2*phi_0
100-
is = InitialQuadratic(snap2one=(0.75,Inf))
100+
is = InitialQuadratic(snap2one=(0.5,Inf))
101101
is(ls, state, phi_0, dphi_0, df)
102102
@test state.alpha == 1.0
103103
@test ls.mayterminate[] == false

0 commit comments

Comments
 (0)