Reuse a JuMP model with different solvers

Is there an efficient way to change the solver associated with a JuMP model? For example, I want to solve a model that takes a nontrivial amount of time to setup with a few different solvers. I am wondering if it is possible to reuse some (intermediate?) objects that JuMP creates and pass them to different solvers. From the docs, it seems that JuMP.copy_model is one approach?

Use set_optimizer: Models · JuMP

model = Model(HiGHS.Optimizer)
# stuff
set_optimizer(model, Ipopt.Optimizer)
1 Like

Would I need to MathOptInterface.Utilities.reset_optimizer(model) the model each time I change optimizer? What I was using is:

M = Model()
# stuff ...

M_HiGHS, refmap_HiGHS = JuMP.copy_model(M)
JuMP.set_optimizer(M_HiGHS, HiGHS.Optimizer)

M_Gurobi, refmap_Gurobi = JuMP.copy_model(M)
JuMP.set_optimizer(M_Gurobi, Gurobi.Optimizer)

Just do:

M = Model()
# stuff ...

JuMP.set_optimizer(M, HiGHS.Optimizer)

JuMP.set_optimizer(M, Gurobi.Optimizer)

JuMP takes care of the rest.