-
Notifications
You must be signed in to change notification settings - Fork 21
Add counter validation against CUTEst native reporting #463
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
Open
arnavk23
wants to merge
14
commits into
JuliaSmoothOptimizers:main
Choose a base branch
from
arnavk23:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2b7f930
Potential fix for code scanning alert no. 8: Workflow does not contai…
arnavk23 a596c0d
Merge pull request #1 from arnavk23/alert-autofix-8
arnavk23 a6ac553
Create test_counter.jl
arnavk23 fa594d2
Update runtests.jl
arnavk23 6afd410
Update Breakage.yml
arnavk23 00ef295
Update runtests.jl
arnavk23 a12d546
Update test/test_counter.jl
arnavk23 b324c4d
Update test/test_counter.jl
arnavk23 b1c0f9a
Update test/test_counter.jl
arnavk23 8c1d171
newer_fields
arnavk23 0c9dd63
rest 3 out of 20
arnavk23 2ca4364
Update test/test_counters.jl
arnavk23 58e9848
only cutest counters
arnavk23 9d6056d
docstring """
arnavk23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using Test | ||
using CUTEst | ||
using NLPModels | ||
|
||
""" | ||
get_cutest_counters(nlp::CUTEstModel{T}) where T | ||
|
||
Get the evaluation counters from CUTEst's native reporting functions. | ||
""" | ||
function get_cutest_counters(nlp::CUTEstModel{T}) where T | ||
arnavk23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
status = [Cint(0)] | ||
if nlp.meta.ncon > 0 | ||
# Constrained problem - use creport | ||
calls = Vector{T}(undef, 7) | ||
time = Vector{T}(undef, 4) | ||
CUTEst.creport(T, nlp.libsif, status, calls, time) | ||
|
||
return ( | ||
neval_obj = Int(calls[1]), | ||
neval_grad = Int(calls[2]), | ||
neval_hess = Int(calls[3]), | ||
neval_hprod = Int(calls[4]), | ||
neval_cons = Int(calls[5]), | ||
neval_jac = Int(calls[6]), | ||
neval_jprod = Int(calls[7]) | ||
) | ||
else | ||
# Unconstrained problem - use ureport | ||
calls = Vector{T}(undef, 4) | ||
time = Vector{T}(undef, 4) | ||
CUTEst.ureport(T, nlp.libsif, status, calls, time) | ||
|
||
return ( | ||
neval_obj = Int(calls[1]), | ||
neval_grad = Int(calls[2]), | ||
neval_hess = Int(calls[3]), | ||
neval_hprod = Int(calls[4]), | ||
neval_cons = 0, | ||
neval_jac = 0, | ||
neval_jprod = 0 | ||
) | ||
end | ||
end | ||
|
||
""" | ||
Test validates eval counters against CUTEst's native reporting. | ||
""" | ||
function test_counter_validation() | ||
arnavk23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@testset "Counter validation against CUTEst native reporting (Issue #278)" begin | ||
arnavk23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@testset "Unconstrained problem counter validation" begin | ||
arnavk23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nlp = CUTEstModel{Float64}("ROSENBR") | ||
|
||
# Get baseline CUTEst counters (initialization overhead) | ||
baseline_cutest = get_cutest_counters(nlp) | ||
baseline_julia = ( | ||
neval_obj = nlp.counters.neval_obj, | ||
neval_grad = nlp.counters.neval_grad, | ||
neval_hess = nlp.counters.neval_hess, | ||
neval_hprod = nlp.counters.neval_hprod | ||
) | ||
|
||
x0 = nlp.meta.x0 | ||
f0 = obj(nlp, x0) | ||
g0 = grad(nlp, x0) | ||
H0 = hess(nlp, x0) | ||
hv = hprod(nlp, x0, ones(length(x0))) | ||
|
||
julia_counters = nlp.counters | ||
cutest_counters = get_cutest_counters(nlp) | ||
|
||
# Calculate increments from baseline | ||
julia_increments = ( | ||
neval_obj = julia_counters.neval_obj - baseline_julia.neval_obj, | ||
neval_grad = julia_counters.neval_grad - baseline_julia.neval_grad, | ||
neval_hess = julia_counters.neval_hess - baseline_julia.neval_hess, | ||
neval_hprod = julia_counters.neval_hprod - baseline_julia.neval_hprod | ||
) | ||
|
||
cutest_increments = ( | ||
neval_obj = cutest_counters.neval_obj - baseline_cutest.neval_obj, | ||
neval_grad = cutest_counters.neval_grad - baseline_cutest.neval_grad, | ||
neval_hess = cutest_counters.neval_hess - baseline_cutest.neval_hess, | ||
neval_hprod = cutest_counters.neval_hprod - baseline_cutest.neval_hprod | ||
) | ||
|
||
# Test that increments match (accounting for CUTEst's internal behavior) | ||
@test julia_increments.neval_obj == cutest_increments.neval_obj | ||
@test julia_increments.neval_grad == cutest_increments.neval_grad | ||
@test julia_increments.neval_hprod == cutest_increments.neval_hprod | ||
|
||
# CUTEst's hess implementation may call internal functions, so expect difference | ||
@test julia_increments.neval_hess >= 1 | ||
@test cutest_increments.neval_hess >= julia_increments.neval_hess | ||
|
||
finalize(nlp) | ||
end | ||
|
||
@testset "Constrained problem counter validation" begin | ||
arnavk23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nlp = CUTEstModel{Float64}("BT1") | ||
|
||
# Get baseline counters | ||
baseline_cutest = get_cutest_counters(nlp) | ||
baseline_julia = ( | ||
neval_obj = nlp.counters.neval_obj, | ||
neval_grad = nlp.counters.neval_grad, | ||
neval_hess = nlp.counters.neval_hess, | ||
neval_hprod = nlp.counters.neval_hprod, | ||
neval_cons = nlp.counters.neval_cons, | ||
neval_jac = nlp.counters.neval_jac, | ||
neval_jprod = nlp.counters.neval_jprod | ||
) | ||
|
||
x0 = nlp.meta.x0 | ||
f0 = obj(nlp, x0) | ||
g0 = grad(nlp, x0) | ||
c0 = cons(nlp, x0) | ||
J0 = jac(nlp, x0) | ||
H0 = hess(nlp, x0) | ||
hv = hprod(nlp, x0, ones(length(x0))) | ||
jv = jprod(nlp, x0, ones(length(x0))) | ||
|
||
julia_counters = nlp.counters | ||
cutest_counters = get_cutest_counters(nlp) | ||
|
||
# Calculate increments from baseline | ||
julia_increments = ( | ||
neval_obj = julia_counters.neval_obj - baseline_julia.neval_obj, | ||
neval_grad = julia_counters.neval_grad - baseline_julia.neval_grad, | ||
neval_hess = julia_counters.neval_hess - baseline_julia.neval_hess, | ||
neval_hprod = julia_counters.neval_hprod - baseline_julia.neval_hprod, | ||
neval_cons = julia_counters.neval_cons - baseline_julia.neval_cons, | ||
neval_jac = julia_counters.neval_jac - baseline_julia.neval_jac, | ||
neval_jprod = julia_counters.neval_jprod - baseline_julia.neval_jprod | ||
) | ||
|
||
cutest_increments = ( | ||
neval_obj = cutest_counters.neval_obj - baseline_cutest.neval_obj, | ||
neval_grad = cutest_counters.neval_grad - baseline_cutest.neval_grad, | ||
neval_hess = cutest_counters.neval_hess - baseline_cutest.neval_hess, | ||
neval_hprod = cutest_counters.neval_hprod - baseline_cutest.neval_hprod, | ||
neval_cons = cutest_counters.neval_cons - baseline_cutest.neval_cons, | ||
neval_jac = cutest_counters.neval_jac - baseline_cutest.neval_jac, | ||
neval_jprod = cutest_counters.neval_jprod - baseline_cutest.neval_jprod | ||
) | ||
|
||
# Test basic relationships that should hold | ||
arnavk23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@test julia_increments.neval_grad >= 1 | ||
@test julia_increments.neval_cons >= 1 | ||
@test julia_increments.neval_jac >= 1 | ||
@test julia_increments.neval_hess >= 1 | ||
@test julia_increments.neval_hprod >= 1 | ||
@test julia_increments.neval_jprod >= 1 | ||
|
||
# CUTEst may count internal calls differently, but should be consistent with usage | ||
@test cutest_increments.neval_grad >= julia_increments.neval_grad | ||
@test cutest_increments.neval_hprod == julia_increments.neval_hprod | ||
|
||
finalize(nlp) | ||
end | ||
end | ||
end | ||
|
||
# Run the tests when included in the main test suite | ||
test_counter_validation() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.