Getting JuMP.Variable from variable name "x[1]"

jump

#1

Is there a straightforward way to get a JuMP.Variable object from the model using the variable name or column index that does not involve the relatively inefficient eval(parse(vname))?


#2

Maybe I misunderstand your question, but you can get variables (and named constraints, I believe) by indexing into the model object:

julia> m = Model(); @variable(m, y)
y

julia> m[:y]
y

#3

No you didn’t :slight_smile:


#4

This also falls under relatively inefficient because it involves a dictionary lookup and the type of the result cannot be inferred.

http://www.juliaopt.org/JuMP.jl/0.17/refvariable.html#constructors

The constructor Variable(m::Model,idx::Int) may be used to create a variable object corresponding to an existing variable in the model (the constructor does not add a new variable to the model).