# Reuse a JuMP model with different solvers

**URL:** <https://discourse.julialang.org/t/reuse-a-jump-model-with-different-solvers/95629>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 6, 2023, 6:29pm UTC](https://discourse.julialang.org/t/reuse-a-jump-model-with-different-solvers/95629 "2023-03-06T18:29:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 6, 2023, 6:29pm UTC](https://discourse.julialang.org/t/reuse-a-jump-model-with-different-solvers/95629/1 "2023-03-06T18:29:17Z")

</div>

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](https://jump.dev/JuMP.jl/stable/reference/models/#JuMP.copy_model), it seems that `JuMP.copy_model` is one approach?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 6, 2023, 7:23pm UTC](https://discourse.julialang.org/t/reuse-a-jump-model-with-different-solvers/95629/2 "2023-03-06T19:23:50Z")

</div>

Use `set_optimizer`: [Models · JuMP](https://jump.dev/JuMP.jl/stable/reference/models/#JuMP.set_optimizer)

```julia
model = Model(HiGHS.Optimizer)
# stuff
set_optimizer(model, Ipopt.Optimizer)

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 6, 2023, 7:33pm UTC](https://discourse.julialang.org/t/reuse-a-jump-model-with-different-solvers/95629/3 "2023-03-06T19:33:15Z")

</div>

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

```julia
M = Model()
# stuff ...

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

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

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 6, 2023, 8:09pm UTC](https://discourse.julialang.org/t/reuse-a-jump-model-with-different-solvers/95629/4 "2023-03-06T20:09:56Z")

</div>

Just do:

```julia
M = Model()
# stuff ...

JuMP.set_optimizer(M, HiGHS.Optimizer)
optimize!(M)

JuMP.set_optimizer(M, Gurobi.Optimizer)
optimize!(M)

```

JuMP takes care of the rest.
