Skip to content

Pass cons_hess_prototype and cons_jac_prototype from user's OptimizationFunction in the created OF in AutoForwardDiff #340

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

Merged
merged 2 commits into from
Aug 16, 2022
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
4 changes: 2 additions & 2 deletions src/function/forwarddiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function instantiate_function(f::OptimizationFunction{true}, x, adtype::AutoForw
else
cons_h = f.cons_h
end

return OptimizationFunction{true}(f.f, adtype; grad=grad, hess=hess, hv=hv,
cons=cons, cons_j=cons_j, cons_h=cons_h,
hess_prototype=nothing, cons_jac_prototype=nothing, cons_hess_prototype=nothing)
hess_prototype=nothing, cons_jac_prototype=f.cons_jac_prototype, cons_hess_prototype=f.cons_hess_prototype)
end
8 changes: 8 additions & 0 deletions test/ADtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ H2 = Array{Float64}(undef, 2, 2)
optprob.hess(H2, [5.0, 3.0])
@test all(isapprox(H2, [28802.0 -2000.0; -2000.0 200.0]; rtol=1e-3))

cons_j = optprob.cons_j
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff(), cons=con2_c, cons_j=cons_j, cons_jac_prototype=cons_jac_proto)
optprob = Optimization.instantiate_function(optf, x0, Optimization.AutoForwardDiff(), nothing, 2)
@test optprob.cons_jac_prototype == sparse([1.0 1.0; 1.0 1.0]) # make sure it's still using it
J = Array{Float64}(undef, 2, 2)
optprob.cons_j(J, [5.0, 3.0])
@test all(isapprox(J, [10.0 6.0; -0.149013 -0.958924]; rtol=1e-3))

# Can we solve problems? Using AutoForwardDiff to test since we know that works
for consf in [cons, con2_c]
optf1 = OptimizationFunction(rosenbrock, Optimization.AutoFiniteDiff(); cons=consf)
Expand Down