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
42 changes: 42 additions & 0 deletions test/jacobian_residual_check.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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_broken length(jacobian_residual_check(nls, x = x)) == 0
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ using BundleAdjustmentModels, DataFrames, LinearAlgebra, NLPModels, Pkg, Test

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