# Collection of JuMP variables

**URL:** <https://discourse.julialang.org/t/collection-of-jump-variables/5798>\
**Category:** Optimization (Mathematical)\
**Created:** [September 9, 2017, 9:35am UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798 "2017-09-09T09:35:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leclere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leclere/32/3644_2.png) [@leclere](https://discourse.julialang.org/u/leclere)\
**Post date:** [September 9, 2017, 9:35am UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/1 "2017-09-09T09:35:30Z")

</div>

Hello,

I would like to define an array of jump variables, e.g something like

```julia
for c:1:C
    @variable(model, mu[c][1:foo(c)] >= 0)
end

```

or maybe

```julia
for c:1:C
    mu[c] = @variable(model, [1:foo(c)] >= 0)
end

```

but I was not able to find the right syntax,

My goal is later to be able to define constraints on the different mu[c].  
I cannot simply use `mu = @variable(model,[c=1:C,1:foo(c)])` as in fact the dimension of mu[c] might depends on c.

Any pointer on the right way to do this ?

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [September 9, 2017, 4:53pm UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/2 "2017-09-09T16:53:10Z")

</div>

Are you sure it won’t work the simple way? It seems to for me:

```julia
julia> f(i) = 2i -1
f (generic function with 1 method)

julia> using JuMP

julia> m = Model()
Feasibility problem with:
 * 0 linear constraints
 * 0 variables
Solver is default solver

julia> @variable(m, y[i = 1:3, j = 1:f(i)] >= 0, start = i + j)
y[i,j] >= 0 for all i in {1,2,3}, j in {..}

julia> getvalue(y)
y: 2 dimensions, 9 entries:
 [1,1] = 2.0
 [2,1] = 3.0
 [2,2] = 4.0
 [2,3] = 5.0
 [3,1] = 4.0
 [3,2] = 5.0
 [3,3] = 6.0
 [3,4] = 7.0
 [3,5] = 8.0

```

Do you get an error if you try that?

---

<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:** [September 10, 2017, 3:37am UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/3 "2017-09-10T03:37:13Z")

</div>

In the second case you’re probably looking for the syntax:

```julia
for c in 1:C
    mu[c] = @variable(model, [1:foo(c)], lowerbound=0)
end 

```

---

<div class="post-metadata">

**Author:** ![leclere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leclere/32/3644_2.png) [@leclere](https://discourse.julialang.org/u/leclere)\
**Post date:** [September 10, 2017, 9:55am UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/4 "2017-09-10T09:55:13Z")

</div>

Thanks for the answer, unfortunately this mgiht not be enough, indeed for some c I want to have mu[c] as a one dimensional variable, and for other as a multi-dimensional variable. Nevertheless your code snippet is helpful to me 🙂

One question : what is the start keyword exactly doing ?

---

<div class="post-metadata">

**Author:** ![leclere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leclere/32/3644_2.png) [@leclere](https://discourse.julialang.org/u/leclere)\
**Post date:** [September 10, 2017, 9:55am UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/5 "2017-09-10T09:55:17Z")

</div>

Thanks, actually this might work but unfortunately I don’t know how to initialize mu ?

`mu = Variable[]` require to use `push!(mu,@variable(model,[1:foo(c), lowerbound=0)` which fails.

More generically, later on, I would like to have `c` as a multi index `c=[1,3,4]`.

Best,

---

<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:** [September 10, 2017, 7:54pm UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/6 "2017-09-10T19:54:25Z")

</div>

Maybe just a dictionary then?

```julia
mu = Dict()
mu[1] = @variable(...)

```

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [September 11, 2017, 12:06pm UTC](https://discourse.julialang.org/t/collection-of-jump-variables/5798/7 "2017-09-11T12:06:51Z")

</div>

The start keyword lets you initialize a variable to have a particular value before the solving process begins. Generally this is required for nonlinear problems and ignored for linear problems, but that depends on the solver used.
