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

**URL:** https://discourse.julialang.org/t/understanding-variable-orders-in-moi-variableindex-in-jump/112594
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [April 6, 2024, 1:13am UTC](https://discourse.julialang.org/t/understanding-variable-orders-in-moi-variableindex-in-jump/112594 "2024-04-06T01:13:48Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Maybe](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Maybe](https://discourse.julialang.org/u/Maybe)
#### Post date: [April 6, 2024, 1:13am UTC](https://discourse.julialang.org/t/understanding-variable-orders-in-moi-variableindex-in-jump/112594/1 "2024-04-06T01:13:49Z")

</div>

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?

---

<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: [April 6, 2024, 5:37am UTC](https://discourse.julialang.org/t/understanding-variable-orders-in-moi-variableindex-in-jump/112594/2 "2024-04-06T05:37:06Z")

</div>

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.
