Getting JuMP.Variable from variable name "x[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))?

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
1 Like

No you didn’t :slight_smile:

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

1 Like