# Querying optimizer to set for another JuMP model

**URL:** <https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672>\
**Category:** Optimization (Mathematical)\
**Tags:** question, package, jump\
**Created:** [April 19, 2022, 3:43am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672 "2022-04-19T03:43:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 3:43am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/1 "2022-04-19T03:43:05Z")

</div>

I have a question (probably very naive though) on how to query an optimizer used for a particular model, so that I can use it in another JuMP model. For example, suppose user decides to use GLPK for the first model as shown here:

```julia
using JuMP

model = Model(GLPK.Optimizer)
@variable(...)
...
optimize!(model)

```

Now, I want to run another model that needs to use the exact same solver as above and run my `new_model` as:

```julia
using JuMP

new_model = Model(query_solver(model)) #Madeup function query_solver(...)
@variable(...)
...
optimize!(new_model)

```

As far as I could look into the JuMP documentation, I could not find a function to retrieve the solver object. Is there something in MOI that I can probably use here? Or is there some other method I can use?  
One possible way I can think is let user store the solver as `solver = GLPK.Optimizer` before the first model is run, and then simply use `solver` in my second model. However, I don’t want this solution. You can think of first model as an input to a package and second model as part of a package that solves a modification of `model`, i.e., `new_model`.

I believe there is some very standard way to do so in packages with dependency on `JuMP`; however, I am new to understanding the machinery behind it, so any help is appreciated. Thanks!

---

<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:** [April 19, 2022, 4:04am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/2 "2022-04-19T04:04:57Z")

</div>

> One possible way I can think is let user store the solver as `solver = GLPK.Optimizer` before the first model is run, and then simply use `solver` in my second model.

This is the recommended way, just ask the user for an optimizer.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 4:19am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/3 "2022-04-19T04:19:40Z")

</div>

OK. The reason I was looking for a different solution was because I thought the JuMP extension packages do it in a different (possibly a more programmatically right) way.

Can you give me an example (or even better refer to one) of how to query for the solver. I tried looking through packages such as [StochasticPrograms.jl](https://github.com/martinbiel/StochasticPrograms.jl)/SDDP.jl but could not find the exact location in the code where packages query the solver from the user.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 19, 2022, 4:22am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/4 "2022-04-19T04:22:58Z")

</div>

You can recover the name of the optimizer as a string using `JuMP.solver_name(model)`.

---

<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:** [April 19, 2022, 4:23am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/5 "2022-04-19T04:23:57Z")

</div>

Gets passed here:  
[https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user\_interface.jl#L620](https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user_interface.jl#L620)  
Used here:  
[https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user\_interface.jl#L653](https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user_interface.jl#L653)  
[https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user\_interface.jl#L505-L524](https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user_interface.jl#L505-L524)  
Stored here:  
[https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user\_interface.jl#L409-L410](https://github.com/odow/SDDP.jl/blob/26690765450df69d1b1f95886530ddbb8d37e43c/src/user_interface.jl#L409-L410)

In general, you should write your extension to be agnostic of the solver. Allow the user to choose, don’t force them into something.

---

<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:** [April 19, 2022, 4:24am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/6 "2022-04-19T04:24:39Z")

</div>

What type of extension are you trying to write?

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 4:44am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/7 "2022-04-19T04:44:45Z")

</div>

This is amazing. Thank you so much for this.

> [@odow](#):
>
> In general, you should write your extension to be agnostic of the solver. Allow the user to choose, don’t force them into something.

That totally makes sense. I will keep this in mind.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 4:47am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/8 "2022-04-19T04:47:39Z")

</div>

I am trying to write a small solver that can possibly solve a two-stage stochastic program using decomposition techniques like Benders and/or column generation (I know packages like [SDDP.jl](https://github.com/odow/SDDP.jl) already exist for this purpose). I am not aiming at writing a full extension right now, but mostly trying to automate my stuff and learn how these packages are build so that I can hopefully help write full fledged extensions (or contribute to the existing ones) in the future.

I am relatively new to Julia and building packages in particular and I sincerely appreciate the quick help and solution you guys provide here and on Julia slack. Surely want to leverage your experience. Thanks again!

---

<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:** [April 19, 2022, 4:50am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/9 "2022-04-19T04:50:06Z")

</div>

> solve a two-stage stochastic program using decomposition techniques like Benders and/or column generation

See also [GitHub - atoptima/Coluna.jl: Branch-and-Price-and-Cut in Julia](https://github.com/atoptima/Coluna.jl) and related packages.

---

<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:** [April 19, 2022, 4:50am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/10 "2022-04-19T04:50:59Z")

</div>

Another reference is  
[https://github.com/odow/StochOptFormat/blob/master/examples/lang-julia/TwoStageBenders.jl](https://github.com/odow/StochOptFormat/blob/master/examples/lang-julia/TwoStageBenders.jl)

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 4:53am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/11 "2022-04-19T04:53:49Z")

</div>

I was not aware of this one. I will have a look at it. Thank you!

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 4:59am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/12 "2022-04-19T04:59:02Z")

</div>

Is there a way I can directly convert this `string` object to the object I can feed to `set_optimizer(model, solver)`, where for example, `solver = GLPK.Optimizer`.

I assume there are ways but probably it is not recommended to convert `string` to other data types? Correct me if I am wrong.

---

<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:** [April 19, 2022, 5:02am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/13 "2022-04-19T05:02:21Z")

</div>

> Is there a way I can directly convert this `string` object to the object I can feed to

No. Please don’t use the `String` approach. It’s led to problems when it has been tried before: [Alpine checks solver name - Why? · Issue #178 · lanl-ansi/Alpine.jl · GitHub](https://github.com/lanl-ansi/Alpine.jl/issues/178).

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 19, 2022, 5:03am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/14 "2022-04-19T05:03:32Z")

</div>

Yes, don’t rely on strings to match actual types or objects.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 5:05am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/15 "2022-04-19T05:05:34Z")

</div>

Makes sense. 👍

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 19, 2022, 5:07am UTC](https://discourse.julialang.org/t/querying-optimizer-to-set-for-another-jump-model/79672/16 "2022-04-19T05:07:09Z")

</div>

Thanks for confirming 👍
