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
5 changes: 5 additions & 0 deletions src/strong_wolfe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Algorithms 3.5 and 3.6.

`maxiters` bounds the outer bracketing loop (Alg. 3.5). `zoom_maxiters` bounds
the inner zoom loop (Alg. 3.6) independently.
A step blocked at `α_max` that satisfies Armijo is accepted without the
curvature condition.

# Merit function

Expand Down Expand Up @@ -257,6 +259,9 @@ end
ϕ_i, dϕ_i, ϕ_prev, dϕ_prev, c1, c2, zoom_maxiters
)
α_out, ok, done = α_z, ok_z, true
elseif α_i >= α_max
# Blocked at the cap while still descending: accept on Armijo alone.
α_out, ok, done = α_i, true, true
else
α_prev = α_i
ϕ_prev = ϕ_i
Expand Down
51 changes: 50 additions & 1 deletion test/optimization_tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LineSearch, Test
using StaticArrays: SVector
using CommonSolve: init, solve!
using SciMLBase: OptimizationFunction, OptimizationProblem, ReturnCode

Expand Down Expand Up @@ -29,7 +30,8 @@ using SciMLBase: OptimizationFunction, OptimizationProblem, ReturnCode
@test sol.step_size ≈ 1.0

sol_capped = solve!(cache, u0, du; α_max = 0.25)
@test sol_capped.step_size <= 0.25 + 1.0e-12
@test sol_capped.retcode == ReturnCode.Success
@test sol_capped.step_size == 0.25
end

@testset "missing OptimizationFunction.grad is rejected" begin
Expand Down Expand Up @@ -60,4 +62,51 @@ using SciMLBase: OptimizationFunction, OptimizationProblem, ReturnCode
@test sol.retcode == ReturnCode.Success
@test sol.step_size ≈ 0.5
end

# Minimizer outside the box [0, 1]²; the ray hits the boundary at α = 0.2.
@testset "minimizer outside box: accept α_max" begin
c = SVector(3.0, 3.0)
f(u, p) = sum(abs2, u .- c) / 2
grad(u, p) = u .- c

u0 = SVector(0.5, 0.2)
du = -grad(u0, nothing)
α_box = minimum((1 .- u0) ./ du)
@test α_box ≈ 0.2

optprob = OptimizationProblem(OptimizationFunction(f; grad), u0)
@testset "α_init = $α_init" for α_init in (1.0, 0.2, 0.03)
cache = init(
optprob, StrongWolfeLineSearch(; c2 = 0.1, α_init),
f(u0, nothing), u0
)
sol = solve!(cache, u0, du; α_max = α_box)
@test sol.retcode == ReturnCode.Success
@test sol.step_size == α_box
@test f(u0 + sol.step_size * du, nothing) < f(u0, nothing)
end

g!(G, u, p) = (G .= u .- c; G)
uv, duv = Vector(u0), Vector(du)
vprob = OptimizationProblem(OptimizationFunction(f; grad = g!), uv)
vcache = init(
vprob, StrongWolfeLineSearch(; c2 = 0.1, α_init = 0.03, α_max = α_box), uv
)
vsol = solve!(vcache, uv, duv)
@test vsol.retcode == ReturnCode.Success
@test vsol.step_size == α_box
end

@testset "Armijo failure at α_max still zooms" begin
f(u, p) = 0.5 * (u - 1)^2
grad(u, p) = u - 1
optprob = OptimizationProblem(OptimizationFunction(f; grad), 0.0)
cache = init(
optprob, StrongWolfeLineSearch(; c2 = 0.1, α_init = 3.0), 0.5, 0.0
)
sol = solve!(cache, 0.0, 1.0; α_max = 3.0)
@test sol.retcode == ReturnCode.Success
@test sol.step_size < 3.0
@test abs(grad(sol.step_size, nothing)) <= 0.1
end
end
4 changes: 2 additions & 2 deletions test/root_finding_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ end
end

@testset "failure returns α_init not last trial α" begin
# Start past the root so a short α_max cannot reach it; search fails at α_max.
# Bracketing runs out of iterations before reaching the root.
u0 = 2.0
α_init = 0.05
nlp = NonlinearProblem(r, u0)
cache = init(
nlp,
StrongWolfeLineSearch(; c2 = 1.0e-8, α_init, α_max = 0.2, maxiters = 20),
StrongWolfeLineSearch(; c2 = 1.0e-8, α_init, α_max = 4.0, maxiters = 3),
r(u0, nothing), u0; grad_f
)

Expand Down
Loading