Skip to content

Commit

Permalink
Modifications done during the flight
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Jun 4, 2024
1 parent 4616cdd commit c832143
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
25 changes: 20 additions & 5 deletions src/sparsity_pattern.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
"""
compute_jacobian_sparsity(c, x0)
compute_jacobian_sparsity(c!, cx, x0)
Return a sparse matrix.
"""
function compute_jacobian_sparsity end

function compute_jacobian_sparsity(c, x0)
detector = SparseConnectivityTracer.TracerSparsityDetector() # replaceable
S = SparseConnectivityTracer.jacobian_pattern(c, x0, detector)
return S
end

function compute_jacobian_sparsity(c!, cx, x0)
detector = SparseConnectivityTracer.TracerSparsityDetector() # replaceable
S = ADTypes.jacobian_sparsity(c!, cx, x0, detector)
Expand All @@ -15,12 +24,18 @@ end
Return a sparse matrix.
"""
function compute_hessian_sparsity(f, nvar, c!, ncon)
detector = SparseConnectivityTracer.TracerSparsityDetector() # replaceable
function lagrangian(x)
cx = zeros(eltype(x), ncon)
c!(cx, x)
return f(x) + dot(rand(ncon), cx)
if ncon == 0
return f(x)
else
cx = zeros(eltype(x), ncon)
y0 = rand(ncon)
return f(x) + dot(c!(cx, x), y0)
end
end
S = ADTypes.hessian_sparsity(lagrangian, rand(nvar), detector)

detector = SparseConnectivityTracer.TracerSparsityDetector() # replaceable
x0 = rand(nvar)
S = ADTypes.hessian_sparsity(lagrangian, x0, detector)
return S
end
11 changes: 4 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ using ADNLPModels:

@testset "Test sparsity pattern of Jacobian and Hessian" begin
f(x) = sum(x)
c!(cx, x) = begin
cx .= 1
return x
end
nvar, ncon = 2, 1
c(x) = x
c!(cx, x) = copyto!(cx, x)
nvar, ncon = 2, 2
x0 = ones(nvar)
cx = rand(ncon)
S = ADNLPModels.compute_jacobian_sparsity(c, x0)
S = ADNLPModels.compute_jacobian_sparsity(c!, cx, x0)
S = ADNLPModels.compute_hessian_sparsity(f, nvar, c!, ncon)
end
Expand Down Expand Up @@ -41,13 +40,11 @@ push!(
ADNLPModels.predefined_backend,
:zygote_backend => Dict(
:gradient_backend => ADNLPModels.ZygoteADGradient,
:hprod_backend => ADNLPModels.SDTForwardDiffADHvprod,
:jprod_backend => ADNLPModels.ZygoteADJprod,
:jtprod_backend => ADNLPModels.ZygoteADJtprod,
:jacobian_backend => ADNLPModels.ZygoteADJacobian,
:hessian_backend => ADNLPModels.ZygoteADHessian,
:ghjvprod_backend => ADNLPModels.ForwardDiffADGHjvprod,
:hprod_residual_backend => ADNLPModels.SDTForwardDiffADHvprod,
:jprod_residual_backend => ADNLPModels.ZygoteADJprod,
:jtprod_residual_backend => ADNLPModels.ZygoteADJtprod,
:jacobian_residual_backend => ADNLPModels.ZygoteADJacobian,
Expand Down

0 comments on commit c832143

Please sign in to comment.