Creating a new variable in JuMP with other variables already existing

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

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?

See this part of the documentation:

https://jump.dev/JuMP.jl/stable/manual/variables/#variable_names_and_bindings

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]
3 Likes

You didn’t mention what your patterns look like, but I think SparseVariables might be a good fit for this application:

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] │
└───────┴───────┴───────┴──────────┘
2 Likes

Thanks odow!!! <3

1 Like

This will be very helpful. Thank you very much!

1 Like