From 35e5d5cad7f7148faf37d2331965cd5e8f2dd9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 24 Mar 2026 10:20:10 +0100 Subject: [PATCH] Simplify instantiate with new lazy cache option --- src/ParametricOptInterface.jl | 40 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/ParametricOptInterface.jl b/src/ParametricOptInterface.jl index de7f087..f4ba021 100644 --- a/src/ParametricOptInterface.jl +++ b/src/ParametricOptInterface.jl @@ -76,16 +76,17 @@ include("parametric_cubic_function.jl") evaluate_duals::Bool = true, save_original_objective_and_constraints::Bool = true, with_bridge_type = nothing, - with_cache_type = nothing, + with_cache_type = missing, ) Create an `Optimizer`, which allows the handling of parameters in an optimization model. +If `ismissing(with_cache_type)`, the optimizer is instantiated with +`MOI.instantiate(optimizer; with_bridge_type)`. If `optimizer` is not a `MOI.ModelLike,` the inner optimizer is constructed -using `MOI.instantiate(optimizer; with_cache_type)`. +using `MOI.instantiate(optimizer; with_bridge_type, with_cache_type)`. -If `with_bridge_type !== nothing`, a `MOI.Bridges.full_bridge_optimizer` is applied as an outer layer. The `{T}` type parameter is optional; it defaults to `Float64`. @@ -296,25 +297,26 @@ Optimizer(arg; kwargs...) = Optimizer{Float64}(arg; kwargs...) function Optimizer{T}( optimizer_fn; - with_bridge_type = nothing, - with_cache_type = nothing, + with_bridge_type::Union{Nothing,Type} = nothing, + with_cache_type::Union{Nothing,Type} = nothing, kwargs..., ) where {T} - inner = MOI.instantiate(optimizer_fn; with_cache_type) - if !MOI.supports_incremental_interface(inner) - # Don't use `default_cache` for the cache because, for example, SCS's - # default cache doesn't support modifying coefficients of the constraint - # matrix. JuMP uses the default cache with SCS because it has an outer - # layer of caching; we don't have that here, so we can't use the - # default. - # - # We could revert to using the default cache if we fix this in MOI. - cache = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()) - inner = MOI.Utilities.CachingOptimizer(cache, inner) - end - if with_bridge_type !== nothing - inner = MOI.Bridges.full_bridge_optimizer(inner, with_bridge_type) + # Dualization needs an incremental interface. + # In `MOI.instantiate`, there a mechanism in place to enforce a cache + # to be used if `with_bridge_type` is `true` because bridges also need the + # incremental interface. + # So we just hook into this mechanism by setting the following to `true` + # if the user didn't explictly ask for a cache. + cache_only_if_incremental_interface_not_supported = isnothing(with_cache_type) + if isnothing(with_cache_type) + with_cache_type = T end + inner = MOI.instantiate( + optimizer_fn; + with_bridge_type, + with_cache_type, + cache_only_if_incremental_interface_not_supported, + ) return Optimizer{T}(inner; kwargs...) end