# Automatically use getvalue() for all variables in model - JuMP

**URL:** <https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480>\
**Category:** Optimization (Mathematical)\
**Created:** [March 4, 2018, 12:48am UTC](https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480 "2018-03-04T00:48:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://discourse.julialang.org/u/holger)\
**Post date:** [March 4, 2018, 12:48am UTC](https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480/1 "2018-03-04T00:48:40Z")

</div>

Hi,  
After solving my JuMP model, I would like to use getvalue() on all variables in my model in an automated way. Say I have the following example problem:

```julia
using JuMP
using Clp
m=Model(solver=ClpSolver())
@variable(m,x[1:3]>=0)
@variable(m,y[1:3]>=0)
@constraint(m,[i=1:3],x[i]<=3)
@constraint(m,[i=1:3],y[i]<=4)
@objective(m,Max,sum(x[i] + 2*y[i] for i=1:3))
status=solve(m)

```

I could then do the following in order to obtain the optimal values of the variables:

```julia
x_res = getvalue(x)
y_res = getvalue(y)

```

How could I do this in an automated fashion, looping through all variables that the JuMP model contains and saving them similarly to the example above?  
Is there a list/dictionary associated with a JuMP model that contains all the variables, that I could then loop through?  
Thank you already in advance

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 4, 2018, 9:51am UTC](https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480/2 "2018-03-04T09:51:53Z")

</div>

As of JuMP v0.18 (the current version), this trick won’t apply to the future JuMP v0.19 version, you can have the list of variables of a model `m` using

```julia
Variable.(m, 1:m.numCols)

```

---

<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 4, 2018, 8:10pm UTC](https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480/3 "2018-03-04T20:10:24Z")

</div>

JuMP stores a dictionary of the named objects, so you can go

```julia
x_res = getvalue(m[:x])

```

However, this won’t work if you have anonymous variables, or two variables with the same name.

Is it sufficient to go

```julia
variables = [:x, :y]
variablevalues = Dict()
for v in variables
    variablevalues[v] = getvalue(m[v])
end

variablevalues[:x][2] # 3

```

or

```julia
variables = [x, y]
variablevalues = Dict()
for v in variables
    variablevalues[v] = getvalue(m, v)
end

variablevalues[x][2] # 3

```

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://discourse.julialang.org/u/holger)\
**Post date:** [March 4, 2018, 11:32pm UTC](https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480/4 "2018-03-04T23:32:49Z")

</div>

Thank you. For future reference, how would this look like in v0.19?

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 5, 2018, 7:17pm UTC](https://discourse.julialang.org/t/automatically-use-getvalue-for-all-variables-in-model-jump/9480/5 "2018-03-05T19:17:51Z")

</div>

In JuMP master, you can currently do

```julia
using MathOptInterface
const MOI = MathOptInterface
vars = JuMP.Variable(m, MOI.get(m.moibackend, MOI.ListOfVariableIndices()))
JuMP.resultvalue.(vars)

```

but we might have a nicer syntax on JuMP v0.19 like `MOI.get(m, MOI.ListOfVariableIndices())` to get the list of variables.
