# What is the difference between variable definition in JuMP like these two ways

**URL:** https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337
**Category:** Optimization (Mathematical)
**Tags:** jump, dataframes
**Created:** [February 9, 2023, 11:52am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337 "2023-02-09T11:52:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)
#### Post date: [February 9, 2023, 11:52am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337/1 "2023-02-09T11:52:15Z")

</div>

I got two difinition ways used in my JuMP model. And I encountered some trouble when I try to access their results using value function. A is a variable defined as `@variable(M, A[i = 1:3, t = 1:24])` and B is like `@variable(M, B[i in [1,2,3], t = 1:24])`. When I access their values, A yields a matrix, while B gives a DenseAxisArray. This kind of difference cause some trouble when I try to record them in dataframes using `Dataframe(value.(A/B), :auto)`. A is good to go while B throwed an error `no method matching to_shape(::Tuple{Vector{Int64}})`.

How could I avoid this without changes to model definition?

---

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [February 9, 2023, 12:51pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337/2 "2023-02-09T12:51:46Z")

</div>

The difference is that `B` defines a [variable container](https://jump.dev/JuMP.jl/stable/manual/variables/#Variable-containers) whereas `A` does not. If you do not like to change your model definition, a (probably inefficient) workaround is to create an empty DataFrame and to fill it value by value, e.g., `value(A[i,t]) / value(B[i,t])` packed into an appropriate loop.

---

<div class="post-metadata">

### Author: ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)
#### Post date: [February 9, 2023, 1:57pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337/3 "2023-02-09T13:57:40Z")

</div>

Thanks, it’s a viable solution. I checked JuMP’s documentation and found that B’s data could be accessed using `Array(value.(B))` or just `value.(B).data`.

---

<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: [February 9, 2023, 7:26pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337/4 "2023-02-09T19:26:55Z")

</div>

The trick is that JuMP cannot “see” that `[1,2,3]` is the same as `1:3`. It has to assume that the first vector could contain arbitrary integers.

As a result, it builds a different type of container. You seem on the right track with the rest.

---

<div class="post-metadata">

### Author: ![trulsf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trulsf/32/46751_2.png) [@trulsf](https://discourse.julialang.org/u/trulsf)
#### Post date: [February 9, 2023, 8:20pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-variable-definition-in-jump-like-these-two-ways/94337/5 "2023-02-09T20:20:20Z")

</div>

I would also look into the possibility of using the `rowtable()` function from JuMP.Containers which will work with the DenseAxisArray and can be used to initialize a dataframe directly:

```julia
using JuMP
using DataFrames
using HiGHS

M = Model(HiGHS.Optimizer)

@variable(M, B[i in [1,2,3], t = 1:24])
@objective(M, Min, 0)

optimize!(M)

df = DataFrame(Containers.rowtable(value, B))

```

It is also possible to specify the column names by using the header argument.
