How to get all the registered variable names of a `JuMP` model?

Is there any method to obtaining all the registered variable names (symbols) of a JuMP model? And, if the variables have been set String names, how to get their original symbolic names?

1 Like

See object_dictionary(model), JuMP · JuMP, which maps the registered name to the object.

There is no way to go from a variable to the symbolic name. You’ll need to invert the object_dictionary for that.

2 Likes

Thanks!

2 Likes

I’d suggest directly add a keys method. This would be way more convenient.

julia> using JuMP

julia> model = Model();

julia> @variable(model, x); @variable(model, y);

julia> model
├ ...omit lines...
└ Names registered in the model
  └ :x, :y

julia> :x ∈ keys(JuMP.object_dictionary(model))
true

julia> :z ∈ keys(JuMP.object_dictionary(model))
false

julia> :x ∈ keys(model)
ERROR: MethodError: no method matching keys(::Model)

Edit: I notice there is a haskey method, which is convenient

julia> m = [JuMP.Model(); JuMP.Model()];

julia> JuMP.@variable(m[1], x[1:2]); JuMP.@variable(m[2], y[1:3]);

julia> af = nt -> haskey(nt, :y);

julia> af(m[1])
false

julia> af(m[2])
true