Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LineSearches"
uuid = "d3d80556-e9d4-5f37-9878-2ab0fcc64255"
version = "7.5.0"
version = "7.5.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
42 changes: 6 additions & 36 deletions src/LineSearches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ function make_ϕdϕ(df, x_new, x, s)
x_new .= x .+ α.*s

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

# Calculate ϕ(a_i), ϕ'(a_i)
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
return f_x_new, real(dot(g_x_new, s))
end
ϕdϕ
end
Expand All @@ -42,48 +42,18 @@ function make_ϕ_dϕ(df, x_new, x, s)
x_new .= x .+ α.*s

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

# Calculate ϕ'(a_i)
real(dot(NLSolversBase.gradient(df), s))
return real(dot(g_x_new, s))
end
make_ϕ(df, x_new, x, s), dϕ
end
function make_ϕ_dϕ_ϕdϕ(df, x_new, x, s)
function dϕ(α)
# Move a distance of alpha in the direction of s
x_new .= x .+ α.*s

# Evaluate f(x+α*s) and ∇f(x+α*s)
NLSolversBase.gradient!(df, x_new)

# Calculate ϕ'(a_i)
real(dot(NLSolversBase.gradient(df), s))
end
function ϕdϕ(α)
# Move a distance of alpha in the direction of s
x_new .= x .+ α.*s

# Evaluate ∇f(x+α*s)
NLSolversBase.value_gradient!(df, x_new)

# Calculate ϕ'(a_i)
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
end
make_ϕ(df, x_new, x, s), dϕ, ϕdϕ
make_ϕ_dϕ(df, x_new, x, s)..., make_ϕdϕ(df, x_new, x, s)
end
function make_ϕ_ϕdϕ(df, x_new, x, s)
function ϕdϕ(α)
# Move a distance of alpha in the direction of s
x_new .= x .+ α.*s

# Evaluate ∇f(x+α*s)
NLSolversBase.value_gradient!(df, x_new)

# Calculate ϕ'(a_i)
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
end
make_ϕ(df, x_new, x, s), ϕdϕ
make_ϕ(df, x_new, x, s), make_ϕdϕ(df, x_new, x, s)
end

include("types.jl")
Expand Down
8 changes: 5 additions & 3 deletions src/initialguess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function (is::InitialQuadratic{T})(ls, state, phi_0, dphi_0, df) where T
# If we're at the first iteration
αguess = is.α0
else
αguess = 2 * (NLSolversBase.value(df) - state.f_x_previous) / dphi_0
αguess = 2 * (phi_0 - state.f_x_previous) / dphi_0
αguess = NaNMath.max(is.αmin, state.alpha*is.ρ, αguess)
αguess = NaNMath.min(is.αmax, αguess)
# if αguess ≈ 1, then make it 1 (Newton-type behaviour)
Expand Down Expand Up @@ -180,8 +180,10 @@ function (is::InitialHagerZhang)(ls::Tls, state, phi_0, dphi_0, df) where Tls
# and the user has not provided an initial step size (is.α0 is NaN),
# then we
# pick the initial step size according to HZ #I0
state.alpha = _hzI0(state.x, NLSolversBase.gradient(df),
NLSolversBase.value(df),
# phi_0 is (or should be) equal to NLSolversBase.value(df, state.x)
# TODO: Make the gradient available as part of the state?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, generally only g_previous is available with the logic being that we can use the cache in the objective, but as we have discussed it makes sense to add an additional vector/array to the algorithm state because it's so much easier to ensure correctness that way. One more array won't mean anything in the full scheme

g_x = NLSolversBase.gradient!(df, state.x)
state.alpha = _hzI0(state.x, g_x, phi_0,
is.αmax,
convert(eltype(state.x), is.ψ0)) # Hack to deal with type instability between is{T} and state.x
if Tls <: HagerZhang
Expand Down
4 changes: 2 additions & 2 deletions test/initial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@
state.f_x_previous = 2*phi_0
is = InitialQuadratic(snap2one=(0.9,Inf))
is(ls, state, phi_0, dphi_0, df)
@test state.alpha == 1.0
@test state.alpha == 0.5
@test ls.mayterminate[] == false

# Test Quadratic snap2one
ls = HagerZhang()
state = getstate()
state.f_x_previous = 2*phi_0
is = InitialQuadratic(snap2one=(0.75,Inf))
is = InitialQuadratic(snap2one=(0.5,Inf))
is(ls, state, phi_0, dphi_0, df)
@test state.alpha == 1.0
@test ls.mayterminate[] == false
Expand Down