MathOptInterface Documentation

I get an error when I try the sample code
model = Utilities.Model{Float64}()
from this documentation: Standard form · MathOptInterface
Should the documentation instead say model = MOI.Utilities.Model{Float64}() ?

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

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

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

Why does the code below break from Standard form · MathOptInterface ?
julia> VectorOfVariables(vcat(n, x))
ERROR: UndefVarError: VectorOfVariables not defined
[1] top-level scope
@ REPL[17]:1

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

[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

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.

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

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

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
[1] top-level scope
@ REPL[37]:1

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

You probably want to read:

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).

For example,

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}:

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

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])