From f514ff11997fb1dd830a777f196bc5d8b8621419 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 13 Feb 2025 09:52:14 +1300 Subject: [PATCH] Fix invalidating inner when AutomaticDifferentiationBackend is set (#458) --- src/MOI_wrapper.jl | 4 ++++ test/MOI_wrapper.jl | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index e5a5c95..ee94c9e 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -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 diff --git a/test/MOI_wrapper.jl b/test/MOI_wrapper.jl index 453a56a..3ba5773 100644 --- a/test/MOI_wrapper.jl +++ b/test/MOI_wrapper.jl @@ -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)