# Rewriting JuMP code in MathOptInterface

**URL:** <https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061>\
**Category:** Optimization (Mathematical)\
**Created:** [June 25, 2020, 7:09pm UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061 "2020-06-25T19:09:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeneralPoxter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/generalpoxter/32/17695_2.png) [@GeneralPoxter](https://discourse.julialang.org/u/GeneralPoxter)\
**Post date:** [June 25, 2020, 7:09pm UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061/1 "2020-06-25T19:09:36Z")

</div>

Say I wanted to add a variable _x_ which represents an _m x n_ matrix whose elements are constrianed.  
In JuMP, the code would simply be:

```julia
@variable(model, 0 <= x[i=1:m, j=1:n] <= 1)

```

However, I am not sure how to translate this into MathOptInterface, which contains features that I need that were not carried into JuMP.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [June 25, 2020, 8:49pm UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061/2 "2020-06-25T20:49:39Z")

</div>

```julia
x = reshape(MOI.add_variables(model, m * n), m, n)
cx = MOI.add_constraint(model, MOI.SingleVariable.(x), MOI.Interval(0.0, 1.0))

```

Or

```julia
x = [MOI.add_constrained_variables(model, MOI.Interval(0.0, 1.0))(1) for i in 1:m, j in 1:n]

```

---

<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:** [June 25, 2020, 8:50pm UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061/3 "2020-06-25T20:50:48Z")

</div>

Which features of MOI were not carried into JuMP?

---

<div class="post-metadata">

**Author:** ![GeneralPoxter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/generalpoxter/32/17695_2.png) [@GeneralPoxter](https://discourse.julialang.org/u/GeneralPoxter)\
**Post date:** [June 25, 2020, 8:59pm UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061/4 "2020-06-25T20:59:17Z")

</div>

I was working off of a suggestion from my previous question [Rational{Int64} variables in JuMP?](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048) to use the CDDLib.Optimizer, which according to [JuMP Documentation](https://jump.dev/JuMP.jl/stable/installation/), requires directly implementing from MOI.

I may have misread the instructions though and could use some help clearing things up regarding whether rational fraction arithmetic from CDDLib can be used in JuMP.

---

<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:** [June 26, 2020, 2:44am UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061/5 "2020-06-26T02:44:28Z")

</div>

No, you will need to use MOI directly.

From the [tests](https://github.com/JuliaPolyhedra/CDDLib.jl/blob/master/test/MOI_wrapper.jl), it looks like you need to use:

```julia
model = CDDLib.Optimizer{Rational{BigInt}}()

```

And make sure all your coefficients are also `Rational{BigInt}`s.

---

<div class="post-metadata">

**Author:** ![GeneralPoxter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/generalpoxter/32/17695_2.png) [@GeneralPoxter](https://discourse.julialang.org/u/GeneralPoxter)\
**Post date:** [June 27, 2020, 4:59pm UTC](https://discourse.julialang.org/t/rewriting-jump-code-in-mathoptinterface/42061/6 "2020-06-27T16:59:32Z")

</div>

Thanks for the help!
