# MathOptInterface Documentation

**URL:** <https://discourse.julialang.org/t/mathoptinterface-documentation/93544>\
**Category:** Optimization (Mathematical)\
**Created:** [January 25, 2023, 11:57pm UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544 "2023-01-25T23:57:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)\
**Post date:** [January 25, 2023, 11:57pm UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/1 "2023-01-25T23:57:04Z")

</div>

I get an error when I try the sample code  
model = Utilities.Model{Float64}()  
from this documentation: [Standard form · MathOptInterface](https://jump.dev/MathOptInterface.jl/stable/reference/standard_form/#MathOptInterface.AllDifferent)  
Should the documentation instead say model = MOI.Utilities.Model{Float64}() ?

julia\> model = Utilities.Model{Float64}()  
ERROR: UndefVarError: Utilities not defined  
Stacktrace:  
[1] top-level scope  
@ REPL[5]:1

julia\> model = MOI.Utilities.Model{Float64}()  
MOIU.Model{Float64}

---

<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:** [January 25, 2023, 11:59pm UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/2 "2023-01-25T23:59:52Z")

</div>

Yes, everything needs the `MOI.` prefix. I should correct it.

---

<div class="post-metadata">

**Author:** ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)\
**Post date:** [January 26, 2023, 12:08am UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/3 "2023-01-26T00:08:00Z")

</div>

Why does the code below break from [Standard form · MathOptInterface](https://jump.dev/MathOptInterface.jl/stable/reference/standard_form/#MathOptInterface.CountDistinct) ?  
julia\> VectorOfVariables(vcat(n, x))  
ERROR: UndefVarError: VectorOfVariables not defined  
Stacktrace:  
[1] top-level scope  
@ REPL[17]:1

---

<div class="post-metadata">

**Author:** ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)\
**Post date:** [January 26, 2023, 12:26am UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/4 "2023-01-26T00:26:26Z")

</div>

The code below also breaks.

julia\> MOI.VectorOfVariables(vcat(n, x))  
ERROR: MethodError: Cannot `convert` an object of type Tuple{MathOptInterface.VariableIndex, MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex, MathOptInterface.Integer}} to an object of type MathOptInterface.VariableIndex  
Closest candidates are:  
convert(::Type{MathOptInterface.VariableIndex}, ::MathOptInterface.ScalarQuadraticFunction{T}) where T at ~/.julia/packages/MathOptInterface/57fsF/src/functions.jl:497  
convert(::Type{MathOptInterface.VariableIndex}, ::MathOptInterface.ScalarAffineFunction) at ~/.julia/packages/MathOptInterface/57fsF/src/functions.jl:486  
convert(::Type{T}, ::T) where T at ~/julia-1.7.3/share/julia/base/essentials.jl:218  
…  
Stacktrace:  
[1] setindex!(A::Vector{MathOptInterface.VariableIndex}, x::Tuple{MathOptInterface.VariableIndex, MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex, MathOptInterface.Integer}}, i1::Int64)  
@ Base ./array.jl:903  
[2] \_unsafe\_copyto!(dest::Vector{MathOptInterface.VariableIndex}, doffs::Int64, src::Vector{Any}, soffs::Int64, n::Int64)  
@ Base ./array.jl:253  
[3] unsafe\_copyto!  
@ ./array.jl:307 [inlined]  
[4] \_copyto\_impl!  
@ ./array.jl:331 [inlined]  
[5] copyto!  
@ ./array.jl:317 [inlined]  
[6] copyto!  
@ ./array.jl:343 [inlined]  
[7] copyto\_axcheck!  
@ ./abstractarray.jl:1104 [inlined]  
[8] Vector{MathOptInterface.VariableIndex}(x::Vector{Any})  
@ Base ./array.jl:563  
[9] convert  
@ ./array.jl:554 [inlined]  
[10] MathOptInterface.VectorOfVariables(variables::Vector{Any})  
@ MathOptInterface ~/.julia/packages/MathOptInterface/57fsF/src/functions.jl:28  
[11] top-level scope  
@ REPL[20]:1

---

<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:** [January 26, 2023, 12:29am UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/5 "2023-01-26T00:29:46Z")

</div>

I’ve made a PR to fix all of these docstrings: [[docs] fix correctness of docstrings in src/sets.jl by odow · Pull Request #2087 · jump-dev/MathOptInterface.jl · GitHub](https://github.com/jump-dev/MathOptInterface.jl/pull/2087).

They’ll now get tested for correctness during the build of the documentation.

> Why does the code below break

It also needs `MOI.`

> The code below also breaks

This was a typo. It needed to be

```julia
n, _ = MOI.add_constrained_variable(model, MOI.Integer())
# instead of 
n = add_constrained_variable(model, Integer())

```

---

<div class="post-metadata">

**Author:** ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)\
**Post date:** [January 26, 2023, 12:36am UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/6 "2023-01-26T00:36:32Z")

</div>

How do we actually get the result of the MOI documentation code snippets? For example, the code below does not work.

julia\> optimize!(model)  
ERROR: MethodError: no method matching optimize!(::MathOptInterface.Utilities.Model{Float64})  
Closest candidates are:  
optimize!(::Model; ignore\_optimize\_hook, \_differentiation\_backend, kwargs…) at ~/.julia/packages/JuMP/vuP7I/src/optimizer\_interface.jl:155  
Stacktrace:  
[1] top-level scope  
@ REPL[37]:1

---

<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:** [January 26, 2023, 12:40am UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/7 "2023-01-26T00:40:58Z")

</div>

What are you trying to do? Use the constraint programming sets with JuMP?

You probably want to read:

- [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#MathOptInterface-constraints)
- [Sudoku · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/linear/sudoku/#Constraint-programming-formulation)

If you’re trying to use MOI directly, then `MOI.Utilities.Model{Float64}()` is a model which stores information. It is not an optimizer than can solve the problem so the answer to your question is “you can’t.”

You’d need to use instead something like `model = MOI.instantiate(HiGHS.Optimizer; with_bridge_type = Float64)`. You’ll also need to use `MOI.optimize!` instead of JuMP’s `optimize!`, and you need to query solutions using MOI’s `MOI.get(model, MOI.VariablePrimal(), x)` instead of JuMP’s `value(x)`.

---

<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:** [January 26, 2023, 12:44am UTC](https://discourse.julialang.org/t/mathoptinterface-documentation/93544/8 "2023-01-26T00:44:04Z")

</div>

For example,

```julia
julia> using JuMP, HiGHS

julia> model = Model(HiGHS.Optimizer);

julia> set_silent(model)

julia> @variable(model, 0 <= x[1:3] <= 3, Int)
3-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]

julia> @variable(model, 1 <= n <= 3, Int)
n

julia> @constraint(model, [n; x] in MOI.CountDistinct(4))
[n, x[1], x[2], x[3]] ∈ MathOptInterface.CountDistinct(4)

julia> optimize!(model)

julia> value(n), value.(x)
(2.0, [1.0, 0.0, 0.0])

```
