How can I clear solver attributes in JuMP?

I need to solve a JuMP model sequentially using different solvers. Solver 1 has one set of attributes but I’m fine with the defaults for solver 2, at least for now. Unfortunately solver 2 complains about the options I set for solver 1, and I can’t figure out how to clear them.

Here’s a silly example:

julia> using JuMP, Gurobi, Ipopt

julia> function resolve()
    m = Model()
    @variable(m, x >= 0)

    set_optimizer(m, Gurobi.Optimizer)
    set_optimizer_attributes(m, "Method" => 2)
    optimize!(m)

    set_optimizer(m, Ipopt.Optimizer)
    # Here I want something like clear_optimizer_attributes(m)
    optimize!(m)
end

julia> resolve()
Academic license - for non-commercial use only - expires 2021-04-16
Gurobi Optimizer version 9.1.1 build v9.1.1rc0 (win64)
Thread count: 6 physical cores, 12 logical processors, using up to 12 threads
Optimize a model with 0 rows, 1 columns and 0 nonzeros
Model fingerprint: 0xa187a6b9
Coefficient statistics:
  Matrix range     [0e+00, 0e+00]
  Objective range  [0e+00, 0e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [0e+00, 0e+00]
Presolve removed 0 rows and 1 columns
Presolve time: 0.01s
Presolve: All rows and columns removed
Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    0.0000000e+00   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.01 seconds
Optimal objective  0.000000000e+00

User-callback calls 20, time in user-callback 0.00 sec
Tried to set Option: Method. It is not a valid option. Please check the list of available options.
ERROR: IPOPT: Couldn't set option 'Method' to value '2'.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] addOption(prob::IpoptProblem, keyword::String, value::Int64)
   @ Ipopt ~\.julia\packages\Ipopt\P1XLY\src\Ipopt.jl:445
 [3] optimize!(model::Ipopt.Optimizer)
   @ Ipopt ~\.julia\packages\Ipopt\P1XLY\src\MOI_wrapper.jl:1436
 [4] optimize!(b::MathOptInterface.Bridges.LazyBridgeOptimizer{Ipopt.Optimizer})
   @ MathOptInterface.Bridges ~\.julia\packages\MathOptInterface\5WwpK\src\Bridges\bridge_optimizer.jl:293
 [5] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer, MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}})
   @ MathOptInterface.Utilities ~\.julia\packages\MathOptInterface\5WwpK\src\Utilities\cachingoptimizer.jl:237
 [6] optimize!(model::Model, optimizer_factory::Nothing; bridge_constraints::Bool, ignore_optimize_hook::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ JuMP ~\.julia\packages\JuMP\e0Uc2\src\optimizer_interface.jl:130
 [7] optimize!
   @ ~\.julia\packages\JuMP\e0Uc2\src\optimizer_interface.jl:106 [inlined]
 [8] resolve()
   @ Main .\REPL[2]:10
 [9] top-level scope
   @ REPL[3]:1

Is there a good way to clear_optimizer_attributes(m)?

2 Likes

This is a known bug. The best work-around we have is something like

set_optimizer(m, optimizer_with_attributes(Gurobi.Optimizer, "Method" => 2))

# ... 

set_optimizer(m, Ipopt.Optimizer)
2 Likes

Thanks, it works now! I never suspected that was a bug. Both solvers were working and I could modify their attributes just fine. I tried finding the relevant issue just now but failed, mind pointing me to it?

Oh, and for anyone who ends up here after searching, use optimizer_with_attributes() with an s at the end.

1 Like

Here’s the open issue: reset_optimizer attempts to pass RawParameters between models · Issue #1220 · jump-dev/MathOptInterface.jl · GitHub

(Edit: and I updated the post. Thanks for catching the typo)

2 Likes