From c8cebe72c2a232833fdea3031424c00b508e7718 Mon Sep 17 00:00:00 2001 From: AdityaPandeyCN Date: Mon, 28 Sep 2026 12:37:15 +0530 Subject: [PATCH] =?UTF-8?q?Accept=20blocked=20step=20at=20=CE=B1=5Fmax=20i?= =?UTF-8?q?n=20StrongWolfeLineSearch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a trial step reaches α_max with Armijo satisfied and ϕ still decreasing, accept it instead of re-evaluating α_max and zooming on an empty interval. --- src/strong_wolfe.jl | 5 ++++ test/optimization_tests.jl | 51 +++++++++++++++++++++++++++++++++++++- test/root_finding_tests.jl | 4 +-- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/strong_wolfe.jl b/src/strong_wolfe.jl index 8f27385..0d0cf88 100644 --- a/src/strong_wolfe.jl +++ b/src/strong_wolfe.jl @@ -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 @@ -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 diff --git a/test/optimization_tests.jl b/test/optimization_tests.jl index 19ca077..e77dc11 100644 --- a/test/optimization_tests.jl +++ b/test/optimization_tests.jl @@ -1,4 +1,5 @@ using LineSearch, Test +using StaticArrays: SVector using CommonSolve: init, solve! using SciMLBase: OptimizationFunction, OptimizationProblem, ReturnCode @@ -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 @@ -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 diff --git a/test/root_finding_tests.jl b/test/root_finding_tests.jl index f12a8d6..eb20230 100644 --- a/test/root_finding_tests.jl +++ b/test/root_finding_tests.jl @@ -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 )