# Building a solver (Getting variable types)

**URL:** <https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [October 20, 2017, 6:49pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587 "2017-10-20T18:49:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [October 20, 2017, 6:49pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/1 "2017-10-20T18:49:54Z")

</div>

I’m currently reading the documentation of MathProgBase from top to bottom and back again and having a look on some implementations of solvers.

As far as I understood it:  
JuMP divides a problem into different categories like LP,Conic and NLP and gives different parameters to MathProgBase based on that. For linear that’s a matrix A a vector b and so on.

For NLP this is kind of an expression of f(x) and g(x) and some other stuff.

Actually I’m not really understanding where the types of each variable are stored.

I get the number of variables and its upper and lower bounds but that’s all. I tried to understand the [Pajarito](https://github.com/JuliaOpt/Pajarito.jl) solver but there it seems to be somewhere between `loadproblem!` and `optimize!`. I thought there is kind of nothing in between besides from `initialize` but it doesn’t seem to happen there.

Thanks for any help and maybe you also have some tutorial or documentation or another solver where I might be able to get some information.

---

<div class="post-metadata">

**Author:** ![joehuchette](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joehuchette/32/32_2.png) [@joehuchette](https://discourse.julialang.org/u/joehuchette)\
**Post date:** [October 20, 2017, 6:58pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/2 "2017-10-20T18:58:24Z")

</div>

You can use the `getcategory(x)` function to query the type of each variable in your model. They’re all stored as symbols in the `model.colCat` field.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [October 20, 2017, 7:02pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/3 "2017-10-20T19:02:56Z")

</div>

> [@joehuchette](#):
>
> getcategory

Sorry can you clarify that a bit? I don’t have access to the x values, right? Or how can I access them inside `loadproblem!` or something similar. It feels like I’m in a kind of abstract place where the Model is in the outer world and I have just some information about it. That’s why I’m a bit confused.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [October 20, 2017, 7:21pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/4 "2017-10-20T19:21:26Z")

</div>

In Pajarito it seems like they use

```julia
MathProgBase.setvartype!(m::PajaritoNonlinearModel, v::Vector{Symbol}) = (m.vartype = v)

```

Unfortunately that function doesn’t seem to be called in my module 😕

---

<div class="post-metadata">

**Author:** ![joehuchette](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joehuchette/32/32_2.png) [@joehuchette](https://discourse.julialang.org/u/joehuchette)\
**Post date:** [October 20, 2017, 7:21pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/5 "2017-10-20T19:21:58Z")

</div>

`getcategory(x::JuMP.Variable)` returns the type of a JuMP Variable. If you’re trying to build a solver that JuMP could call through MathProgBase, you will need to implement a method for `MathProgBase.setvartype!` (see [https://github.com/JuliaOpt/JuMP.jl/blob/592d4e47e9e46404f92bfbca45a077b12a045652/src/solvers.jl#L400-L406](https://github.com/JuliaOpt/JuMP.jl/blob/592d4e47e9e46404f92bfbca45a077b12a045652/src/solvers.jl#L400-L406)) yourself.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [October 20, 2017, 7:47pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/6 "2017-10-20T19:47:38Z")

</div>

Is there a documentation about the flow inside `MathProgBase` ? Like first `loadproblem!` is called and then this and then `optimize!` and so on?

I more or less copied the `setvartype!` function from Pajarito just changed the type of `m` but it doesn’t get called. Any idea?

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [October 20, 2017, 8:26pm UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/7 "2017-10-20T20:26:32Z")

</div>

Ah I found it. I used `relaxation=true` and then the function doesn’t get called which makes sense.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [October 21, 2017, 12:18am UTC](https://discourse.julialang.org/t/building-a-solver-getting-variable-types/6587/8 "2017-10-21T00:18:40Z")

</div>

JuMP will call `setvartype!` on the MPB model after `loadproblem!` if there are any non-continuous variables.

You should be aware that MPB will soon be replaced and unsuported in Julia 0.7 and JuMP 0.19 in favor of MathOptInterface ([announcement](https://discourse.julialang.org/t/mathoptinterface-and-upcoming-breaking-changes-in-jump-0-19/4874)). We’re getting closer to having basic MOI functionality working on the JuMP side and solver interfaces, but it’s not quite usable yet.
