# Creating a new variable in JuMP with other variables already existing

**URL:** <https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 11, 2022, 10:31pm UTC](https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138 "2022-11-11T22:31:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![afazevedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afazevedo/32/16394_2.png) [@afazevedo](https://discourse.julialang.org/u/afazevedo)\
**Post date:** [November 11, 2022, 10:31pm UTC](https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138/1 "2022-11-11T22:31:20Z")

</div>

Hi there. Can anyone help me?

I’m doing sort of implementation of a column generation model and after finding a new pattern I want to add a new variable from a existing set of existing variables. Example:

This is my main problem

main = Model(Gurobi.Optimizer)  
@variable(main, 0 \<= x[i in 1:length(I)] \<= 1)  
@variable(main, 0 \<= y[i in 1:length(J)] \<= 1)  
@variable(main, 0 \<= z[Edges] \<= 1)  
…

Found a new pattern and I want to add the number of variables so

push!(x, @variable(main, lower\_bound = 0, upper\_bound = 0))

But it creates something like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/b/db76e268e081054b122e3af5604399191f92b118.png)

And I want to be refereced as a new “x” variable, not “\_”. Additionally, I want to add the variable x[121], not with indices 286. I think this happens because I have y variables.

How can I create this type of variable to add in my set of existing variables named x?

---

<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:** [November 12, 2022, 4:14am UTC](https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138/2 "2022-11-12T04:14:21Z")

</div>

See this part of the documentation:

[https://jump.dev/JuMP.jl/stable/manual/variables/#variable\_names\_and\_bindings](https://jump.dev/JuMP.jl/stable/manual/variables/#variable_names_and_bindings)

```julia
julia> using JuMP

julia> model = Model();

julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
 x[1]
 x[2]

julia> for i in (length(x)+1):4
           push!(x, @variable(model, base_name = "x[$i]"))
       end

julia> x
4-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]
 x[4]

```

---

<div class="post-metadata">

**Author:** ![hellemo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hellemo/32/324_2.png) [@hellemo](https://discourse.julialang.org/u/hellemo)\
**Post date:** [November 12, 2022, 9:35am UTC](https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138/3 "2022-11-12T09:35:53Z")

</div>

You didn’t mention what your patterns look like, but I think [SparseVariables](https://github.com/hellemo/SparseVariables.jl) might be a good fit for this application:

```julia
julia> using JuMP, SparseVariables, PrettyTables

julia> main = Model();

julia> @variable(main, x[i=1:10,j=1:10,k=1:10] >=0; container=IndexedVarArray)
IndexedVarArray{VariableRef, 3, Tuple{Int64, Int64, Int64}} with 0 entries

julia> for p in ((1,2,3), (3,1,1), (9,2,3))
           insertvar!(x, p...)
       end

julia> pretty_table(JuMP.Containers.rowtable(name, x))
┌───────┬───────┬───────┬──────────┐
│ i │ j │ k │ name │
│ Int64 │ Int64 │ Int64 │ String │
├───────┼───────┼───────┼──────────┤
│ 1 │ 2 │ 3 │ x[1,2,3] │
│ 3 │ 1 │ 1 │ x[3,1,1] │
│ 9 │ 2 │ 3 │ x[9,2,3] │
└───────┴───────┴───────┴──────────┘

```

---

<div class="post-metadata">

**Author:** ![afazevedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afazevedo/32/16394_2.png) [@afazevedo](https://discourse.julialang.org/u/afazevedo)\
**Post date:** [November 12, 2022, 10:01pm UTC](https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138/4 "2022-11-12T22:01:42Z")

</div>

Thanks odow!!! \<3

---

<div class="post-metadata">

**Author:** ![afazevedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afazevedo/32/16394_2.png) [@afazevedo](https://discourse.julialang.org/u/afazevedo)\
**Post date:** [November 12, 2022, 10:02pm UTC](https://discourse.julialang.org/t/creating-a-new-variable-in-jump-with-other-variables-already-existing/90138/5 "2022-11-12T22:02:09Z")

</div>

This will be very helpful. Thank you very much!
