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 derivative check for Jacobian of residual #85

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,46 @@ end

include("testBundleAdjustmentModels.jl")
include("testBundleAdjustmentAllocations.jl")

# https://github.com/JuliaSmoothOptimizers/NLPModelsTest.jl/blob/src/dercheck.jl#L43
function jacobian_residual_check(
nlp::AbstractNLSModel;
x::AbstractVector = nlp.meta.x0,
atol::Float64 = 1.0e-6,
rtol::Float64 = 1.0e-4,
)

# Fast exit if there are no constraints.
J_errs = Dict{Tuple{Int, Int}, Float64}()
nlp.nls_meta.nequ > 0 || return J_errs

# Optimal-ish step for second-order centered finite differences.
step = (eps(Float64) / 3)^(1 / 3)

# Check constraints Jacobian.
J = jac_residual(nlp, x)
h = zeros(nlp.meta.nvar)
cxph = zeros(nlp.nls_meta.nequ)
cxmh = zeros(nlp.nls_meta.nequ)
# Differentiate all constraints with respect to each variable in turn.
for i = 1:(nlp.meta.nvar)
h[i] = step
residual!(nlp, x + h, cxph)
residual!(nlp, x - h, cxmh)
dcdxi = (cxph - cxmh) / 2 / step
for j = 1:(nlp.nls_meta.nequ)
err = abs(dcdxi[j] - J[j, i])
if err > atol + rtol * abs(dcdxi[j])
J_errs[(j, i)] = err
end
end
h[i] = 0
end
return J_errs
end

@testset "Test derivative Jacobian of residual" begin
nls = BundleAdjustmentModel("problem-49-7776-pre")
x = 10 * [-(-1.0)^i for i = 1:nls.meta.nvar]
@test length(jacobian_residual_check(nls, x = x)) == 0
end
Loading