# Add variables in a container

**URL:** https://discourse.julialang.org/t/add-variables-in-a-container/35798
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [March 10, 2020, 2:24pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798 "2020-03-10T14:24:13Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 10, 2020, 2:24pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/1 "2020-03-10T14:24:13Z")

</div>

Hello,  
I’ve found a workaround to my problem but I wonder if there is a better way to solve it.  
This is what I do:

```nohighlight
using JuMP
model = JuMP.Model()
n = 10
@variable(model, -5 <= x0 <= 5, Int)
@variable(model, -5 <= x[1:n] <= 5, Int)
...
n += 1
push!(model.obj_dict[:x], @variable(model, base_name="x["*string(n)*"]", lower_bound=-5, upper_bound=5, integer=true))

```

What I would like is having `@variable(model, -5 <= x[0:n] <= 5, Int)` instead of `x0` and `x[1:n]` but I didn’t find a way to `push!` into `x` with this writing.

Thank you  
Rémi Garcia

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [March 10, 2020, 2:39pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/2 "2020-03-10T14:39:47Z")

</div>

You shouldn’t touch internal fields of the JuMP model like `obj_dict`, but otherwise this is correct. You can push to `x` if it remains in scope, or to `model[:x]` otherwise.

---

<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 10, 2020, 3:15pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/3 "2020-03-10T15:15:33Z")

</div>

You can only push if the container is an `Array` (i.e., it has indices `1:n`). If it’s a `DenseAxisArray` (e.g., with indices `0:n`) or a `SparseAxisArray`, then this won’t work.

---

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 11, 2020, 6:30am UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/4 "2020-03-11T06:30:35Z")

</div>

Thanks. I didn’t find this usage (`model[:x]`) in docs. I looked in Variables and Constraints sections. It might be useful to add this there. I found `obj_dict` with auto-completion.

---

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 11, 2020, 6:38am UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/5 "2020-03-11T06:38:35Z")

</div>

Thanks for the precision. That’s what I though but I hoped there was a better workaround than my separated 0. For a given variable, if the indices are not overlapping is it an option to add _auto-push_ in JuMP? In order to make this kind of code works:

```julia
using JuMP
model = JuMP.Model()
@variable(model, -5 <= x[0:5] <= 5, Int)
@variable(model, -5 <= x[6:12] <= 5, Int)

```

or

```julia
for i in 1:7
    @variable(model, x[i], lower_bound=foo(i))
end

```

Maybe with a parameter like `force_same_name=true`?

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [March 11, 2020, 1:33pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/6 "2020-03-11T13:33:41Z")

</div>

The `model[:x]` syntax is discussed in this section of the docs: [Variables · JuMP](http://www.juliaopt.org/JuMP.jl/v0.21.1/variables/#What-is-a-JuMP-variable?-1).

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [March 11, 2020, 1:34pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/7 "2020-03-11T13:34:11Z")

</div>

We might be able to do something like this. Could you open an issue with a feature request?

---

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 11, 2020, 1:42pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/8 "2020-03-11T13:42:14Z")

</div>

I read that and… missed it. Should have double checked. Sorry.

---

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 11, 2020, 1:43pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/9 "2020-03-11T13:43:11Z")

</div>

Ok, I’ll open an issue for it. Thanks!

---

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 11, 2020, 4:50pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/10 "2020-03-11T16:50:42Z")

</div>

I found a second workaround, more viable for me:

```nohighlight
using JuMP
model = JuMP.Model()
n = 10
@variable(model, [0:n], base_name="x", lower_bound=-5, upper_bound=5, integer=true))
@constraint(model, [i in 0:n], variable_by_name(model, "x["*string(i)*"]") <= foo(i))
...
n += 1
@variable(model, [n], base_name="x", lower_bound=-5, upper_bound=5, integer=true))
@constraint(model, variable_by_name(model, "x["*string(n)*"]") <= foo(n))

```

There is a drawback: the code becomes unreadable… This can be improved with `x = [i => variable_by_name(model, "x["*string(i)*"]") for i in 0:n]` before `@constraint` calls. Yet I’m not totally satisfied. I will open the issue tomorrow morning (in France).

---

<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 11, 2020, 4:55pm UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/11 "2020-03-11T16:55:54Z")

</div>

You don’t have to use JuMP types as containers. See:

```julia
using JuMP
model = Model()
n = 10
x = Dict(
    i => @variable(model, base_name = "x_$(i)")
    for i = 0:n
)

n += 1
x[n] = @variable(model, base_name = "x_$I")
@constraint(model, x[n] <= foo(n))

```

---

<div class="post-metadata">

### Author: ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)
#### Post date: [March 12, 2020, 7:06am UTC](https://discourse.julialang.org/t/add-variables-in-a-container/35798/12 "2020-03-12T07:06:10Z")

</div>

This is great! I opened an issue ([JuMP.jl#2200](https://github.com/JuliaOpt/JuMP.jl/issues/2200)) but this does exactly what I needed. Thanks a lot.
