Hi,
I hope my question is not already answered somewhere else, I couldn’t find it.
Here is the beginning of a MWE (I actually have many more variables and bigger sets):
using JuMP, Gurobi
model = Model(Gurobi.Optimizer)
set_silent(model)
I = 1:5
J = 1:10
K = 8:20
@variables(model, begin
a[i=I, j=J]
b[j=J]
c[i=I, k=K]
end)
At the end, I have a solution with results that I wish to process. How can I loop over the variables without calling them explicitly ? Is it possible to call for some results vector/container/…, where results[1] contains my variable a ?
Currently, I store the results in dictionaries or dataframes this way:
Btw, is there a way to distinguish variables from constraints or expressions ?
It’s always useful to remember that JuMP variables and constraints are normal Julia types. So you can use non-JuMP functions to manipulate them. For example, you might do:
dict_output = Dict{Symbol,Any}()
for (k, v) in object_dictionary(model)
if v isa VariableRef
dict_output[k] = value(v)
elseif v isa AbstractArray{VariableRef}
dict_output[k] = value.(v)
else
# skip. probably a constraint or an expressionn
end
end
or perhaps even
function add_output(dict_output, k, v::VariableRef)
dict_output[k] = value(v)
end
function add_output(dict_output, k, v::AbstractArray{VariableRef})
dict_output[k] = value.(v)
end
function add_output(dict_output, k, v::Any)
@info "Skipping key = $k of type $(typeof(v))"
end
dict_output = Dict{Symbol,Any}()
for (k, v) in object_dictionary(model)
add_output(dict_output, k, v)
end