Skip to content

Commit

Permalink
Fix invalidating inner when AutomaticDifferentiationBackend is set (#458
Browse files Browse the repository at this point in the history
)
  • Loading branch information
odow authored Feb 12, 2025
1 parent 1412187 commit f514ff1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,10 @@ function MOI.set(
::MOI.AutomaticDifferentiationBackend,
backend::MOI.Nonlinear.AbstractAutomaticDifferentiation,
)
# Setting the backend will invalidate the model if it is different. But we
# don't requrire == for `::MOI.Nonlinear.AutomaticDifferentiationBackend` so
# act defensive and invalidate regardless.
model.inner = nothing
model.ad_backend = backend
return
end
Expand Down
5 changes: 4 additions & 1 deletion test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,15 @@ end
function test_ad_backend()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
attr = MOI.AutomaticDifferentiationBackend()
@test MOI.supports(model, attr)
@test MOI.get(model, attr) == MOI.Nonlinear.SparseReverseMode()
MOI.optimize!(model)
@test model.inner isa Ipopt.IpoptProblem
MOI.set(model, attr, MOI.Nonlinear.ExprGraphOnly())
@test MOI.get(model, attr) == MOI.Nonlinear.ExprGraphOnly()
x = MOI.add_variable(model)
@test model.inner === nothing
f = MOI.ScalarNonlinearFunction(:^, Any[x, 4])
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
Expand Down

0 comments on commit f514ff1

Please sign in to comment.