# How to loop over the variables of a JuMP model?

**URL:** <https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 22, 2023, 5:08pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463 "2023-03-22T17:08:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![geo1093](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@geo1093](https://discourse.julialang.org/u/geo1093)\
**Post date:** [March 22, 2023, 5:08pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463/1 "2023-03-22T17:08:01Z")

</div>

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

```julia
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:

```julia
dict_output= OrderedDict{String, Any}() 
dict_output["a"]=value.(model[:a])
dict_output["b"]=value.(model[:b])
dict_output["c"]=value.(model[:c])

```

My main goal is to process the results differently, depending on their dimensions.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 22, 2023, 7:04pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463/2 "2023-03-22T19:04:26Z")

</div>

It depends what you want.

`all_variables(model)` gives you a vector of every variable, so perhaps:

```julia
dict_output = Dict(x => value(x) for x in all_variables(model))

```

Or you might want:

```julia
dict_output = Dict(
    k => value.(v) for (k, v) in object_dictionary(model)
)

```

You’re also free to create your own data structure:

```julia
@variables(model, begin
    a[i=I, j=J]
    b[j=J]
    c[i=I, k=K]
end)
results = Dict("a" => a, "b" => b, "c" => c)
dict_output = Dict(k => value.(v) for (k, v) in results)

```

You’re not limited to using only the functions defined by JuMP.

---

<div class="post-metadata">

**Author:** ![geo1093](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@geo1093](https://discourse.julialang.org/u/geo1093)\
**Post date:** [March 23, 2023, 8:48am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463/3 "2023-03-23T08:48:52Z")

</div>

I can deal with `object_dictionary`. Many thanks !  
Btw, is there a way to distinguish variables from constraints or expressions ?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 23, 2023, 7:18pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463/4 "2023-03-23T19:18:09Z")

</div>

> 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:

```julia
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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![geo1093](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@geo1093](https://discourse.julialang.org/u/geo1093)\
**Post date:** [March 23, 2023, 7:22pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-variables-of-a-jump-model/96463/5 "2023-03-23T19:22:37Z")

</div>

Yes, indeed. Thank you for the examples !
