How to extract the indices of a certain variable from a `Vector{VariableRef}`?

using JuMP
m = Model()
@variable(m, x[1:3])
@variable(m, y[1:5])
v = all_variables(m)

v is an 8-element Vector{VariableRef}. Now I want to extract all the indices with respect to variable y from the vector v (e.g., it can be a Vector{Int64}: [4:8;]). How should I do?

Thanks!

Do something like:

julia> using JuMP

julia> model = Model();

julia> @variable(model, x[1:3]);

julia> @variable(model, y[1:5]);

julia> var_to_column = Dict(v => i for (i, v) in enumerate(all_variables(model)))
Dict{VariableRef, Int64} with 8 entries:
  y[1] => 4
  y[5] => 8
  y[2] => 5
  x[2] => 2
  x[1] => 1
  y[4] => 7
  x[3] => 3
  y[3] => 6

julia> [var_to_column[yi] for yi in y]
5-element Vector{Int64}:
 4
 5
 6
 7
 8
2 Likes

Thanks, @odow ! :handshake: :handshake:

1 Like