Understanding variable orders in ``MOI.VariableIndex`` in JuMP

I am trying to understand the variable orders in MOI.VariableIndex. For example, in the following model

m = Model()
@variable(m, 0 <= x[i=1:2] <= 2 )
@variable(m, z)
@variable(m, 0 <= y[i=1:3] <= 1)

The outcome of

for i = 1:JuMP.num_variables(m)
println("x[$i] = ", VariableRef(m, MOI.VariableIndex(i)))
end

is

x[1] = x[1]
x[2] = x[2]
x[3] = z
x[4] = y[1]
x[5] = y[2]
x[6] = y[3]

which implies that the VariableIndex is set based on the order of defining the variables in the model. But my question is whether this is always true? Are there any particular situations where the variable orders in ``VariableIndex" would be different from those entered in the original model?

You should not assume anything about the mapping between JuMP variables and their MOI index.

The only thing you may use is index(x) to query the index of a variable, and most users should never need this.

1 Like