# How to get ALL decision variables value?

**URL:** https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [May 3, 2021, 1:13am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442 "2021-05-03T01:13:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![iiitr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iiitr/32/24586_2.png) [@iiitr](https://discourse.julialang.org/u/iiitr)
#### Post date: [May 3, 2021, 1:13am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/1 "2021-05-03T01:13:38Z")

</div>

Hi all,

I am new in Julia and JuMP.jl

I have many decision variables, like, x1, x1, …x999, …

if I want to obtain the value of all decision variables

now, I may only use the codes as follows:

```julia
x1 = value.(x1)
x2 = value.(x2)
....
x999 = value.(x999)

```

this may cost me too much time

Is there a “one-click” function to get all variable values? And copy them to their original variable names

I used to be a MATLAB/Yalmip user, in matlab, I can use this m-file to achieve the target above, as follows:

```matlab
res = whos;
[nn, mm] = size(res);

for ii = 1:nn
    if strcmp(res(ii).class, 'sdpvar') || strcmp(res(ii).class, 'ndsdpvar')
        vars = res(ii).name;
        eval([vars, '=', 'value(', vars ,');']);
    end
end
clear vars

```

if JuMP.jl do not have this “one-click” function, who can tell me how to obtain the variables in the current workspace (Julia pro), like `whos` in MATLAB.

Thanks a lot!

---

<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: [May 3, 2021, 1:33am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/2 "2021-05-03T01:33:24Z")

</div>

You can use

```nohighlight
value.(all_variables(model))

```

But a better way would be to structure your code to not use `x1, x2, ..., x999`. Use a vector like

```nohighlight
@variable(model, x[1:999])

```

Then you can just go

```nohighlight
value.(x)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [May 3, 2021, 7:10am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/3 "2021-05-03T07:10:54Z")

</div>

Having a vector instead of x1, x2,… as suggested by @odow is likely better, but if for any reason you want to keep the same model structure as you have, you can (thanks to Julia metaprogramming capabilities) programmatically build the variable names and the for loop…

---

<div class="post-metadata">

### Author: ![iiitr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iiitr/32/24586_2.png) [@iiitr](https://discourse.julialang.org/u/iiitr)
#### Post date: [May 3, 2021, 7:27am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/4 "2021-05-03T07:27:22Z")

</div>

Hi odow,

Using vectors to create decision variables is a good idea, However, I may give a bad example.

In my mode, I want to obtain the value of all variables with completely different and irregular name.

```julia
value.(all_variables(model))

```

This code can indeed get the value of all variables, but it seems that it cannot replace the variable with the value of the variable, that is, the original name assigned to the variable

```julia
@variable(model, ashfjnbasdfgbkj[1:10, 1:10, 1:10], Bin)
@variable(model, jhsFADJNBGAH[1:10, 1:10, 1:10], Bin)
@variable(model, hdukfhiebfHB[1:10, 1:10, 1:10], Bin)
...
...
optimize!(model)
ashfjnbasdfgbkj = value.(ashfjnbasdfgbkj)
jhsFADJNBGAH = value.(jhsFADJNBGAH)
hdukfhiebfHB = value.(hdukfhiebfHB)

```

---

<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: [May 3, 2021, 7:51am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/5 "2021-05-03T07:51:07Z")

</div>

JuMP variables are just normal Julia bindings. You can do anything with them. You can also look them up using `model[key]`.

```julia
@variable(model, x[1:10, 1:10, 1:10], Bin)
@variable(model, y[1:10, 1:10, 1:10], Bin)
@variable(model, z[1:10, 1:10, 1:10], Bin)
optimize!(model)
Z_val = Dict(v => value.(v) for v in [x, y, z])
Z_val = Dict(v => value.(model[v]) for v in [:x, :y, :z])

```

Another option is [Models · JuMP](https://jump.dev/JuMP.jl/stable/reference/models/#JuMP.object_dictionary:)

```nohighlight
object_dictionary(model)

```

it returns a dictionary containing keys mapping to each JuMP object.

---

<div class="post-metadata">

### Author: ![iiitr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iiitr/32/24586_2.png) [@iiitr](https://discourse.julialang.org/u/iiitr)
#### Post date: [May 3, 2021, 10:04am UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/6 "2021-05-03T10:04:03Z")

</div>

Hi all,  
I may found the solutions. Thanks @odow for the tips!  
This seems to be an inefficient method, but it is indeed effective for my code.  
if anyone has a better way, please let me know, thanks! 😊  
I guess there may be a better way, but limited by my English level, it is difficult for me to search for a solution to my problem on Google. 😅

```julia
dvar_string = string(keys(object_dictionary(model)))
# OUTPUT: dvar_string = "[:x, :y, :z, :p, :q,...]"
dvar = []
for i in 1:length(dvar_string)
    if dvar_string[i] == ':'
        I = i + 1
        for j = I:length(dvar_string)
            if dvar_string[j] == ','
                J = j-1
                push!(dvar, dvar_string[I:J])
                break
            end
        end
    end
end

for k in dvar
    eval(Meta.parse(k * "=value.(" * k * ")"))
end

```

You can also write it in a separate file, like : `getAllJuMPValue.jl`  
and call this .jl-file after `optimize!(model)`, as follows:

```julia
....
optimize!(model)
include("getAllJuMPValue.jl")
...

```

The above code is equivalently executed:

```julia
x = value.(x)
y = value.(y)
z = value.(z)
p = value.(p)
q = value.(q)
...

```

This is especially useful when there are many decision variables, and the decision variables are increased or decreased during the programming process for debugging.

---

<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: [May 3, 2021, 7:18pm UTC](https://discourse.julialang.org/t/how-to-get-all-decision-variables-value/60442/7 "2021-05-03T19:18:19Z")

</div>

In general, you should almost never need to use `eval` or `Meta.parse`. They’re easy to get wrong, and they don’t always do what you think they’re doing.

Do something like this instead:

```nohighlight
solution = Dict(
    k => value.(model[k])
    for k in keys(object_dictionary(model))
)

```

and structure your code to not use the lots of lines like `x = value.(x)`.
