Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a median check #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Stopping = "0.3, 0.4"

[extras]
ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ADNLPModels", "Test"]
test = ["ADNLPModels", "BenchmarkTools", "Test"]
20 changes: 19 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@ using ADNLPModels, NLPModels, NLPModelsIpopt
# , NLPModelsKnitro
#This package
using Stopping, StoppingInterface
using BenchmarkTools

@test true
include("utils.jl")

for solver in (ipopt, lbfgs, trunk, tron)
@testset "Compute overhead of using Stopping with $(string(solver))" begin
T = Float64
n = 100
function f(x)
n = length(x)
return 100 * sum((x[i + 1] - x[i]^2)^2 for i = 1:(n - 1)) + sum((x[i] - 1)^2 for i = 1:(n - 1))
end
x0 = T.([i / (n + 1) for i = 1:n])
nlp = ADNLPModel(f, x0, name = "genrose")
tstp, tnlp = stopping_overhead(nlp, solver)
median_overhead = (median(tstp).time - median(tnlp).time) # result in nanoseconds
@show median_overhead / 1e9 # in seconds
@test median_overhead < 0.05 * median(tnlp).time
end
end

@testset "Rosenbrock with ∑x = 1" begin
nlp = ADNLPModel(
Expand Down
41 changes: 41 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# https://juliaci.github.io/BenchmarkTools.jl/dev/manual/
function stopping_overhead(nlp::AbstractNLPModel, solver::Function)
stp = NLPStopping(nlp)
max_ev = if :neval_obj in keys(stp.meta.max_cntrs)
stp.meta.max_cntrs[:neval_obj]
else
typemax(Int)
end
fun(nlp) = if solver == ipopt
ipopt(
nlp,
print_level = 0,
tol = stp.meta.rtol,
x0 = stp.current_state.x,
max_iter = stp.meta.max_iter,
max_cpu_time = stp.meta.max_time,
dual_inf_tol = stp.meta.atol,
constr_viol_tol = stp.meta.atol,
compl_inf_tol = stp.meta.atol,
)
else
solver(
nlp,
verbose = 0,
atol = stp.meta.atol,
rtol = stp.meta.rtol,
# max_iter = stp.meta.max_iter,
max_time = stp.meta.max_time,
max_eval = max_ev,
)
end
fun(nlp) # for pre-compilation
let nlp = nlp, stp = stp, solver = solver, fun = fun
global tnlp = @benchmark $fun($nlp)
@show tnlp
global tstp = @benchmark $solver($stp)
@show tstp
end
return tstp, tnlp
end