Getting value from unregistered variables

Sorry if this has already been asked - I tried searching and re-reading the documentation and couldn’t find anything. I have a model with unregistered variables (though we did assign base names/strings) and I’m trying to figure out if there’s a way for me to access the solution values for those variables. I can’t tell if it’s possible, or if I need to rewrite the variable assignments so that they have registered names.

Thanks!

I’m not sure what your larger goal is but usually I tend to use anonymous variables when I am storing the variables in another data structure, and you can index into a different data structure to naturally retrieve them. For example:

model = JuMP.Model()

data = DataFrame(i=[1])
data.x = @variable(model, [1:1]) # anon var

# you would optimize it before calling value()

JuMP.value.(data.x)
1 Like

Hi @nhebel, welcome to the forum :smile:

As @slwu89 alludes to, you need some sort of data structure to hold your variables if you want to access them later. You can either use the built-in model[:x] data structure if you name your variables like @variable(model, x).

If you use anonymous variables, then you must maintain some sort of external data structure.

There is variable_by_name(model, "x"), but you’re probably much better off using your own data structure.

2 Likes

thank you so much, this is super helpful!

2 Likes