Adding a new variable to an existing variable array

question

#1

Hi. I’m teaching Column Generation on Jump. Hence, given a set of columns x[1:N] I’ll like add a new variable x[N+1] to the previous variable set.

If I try @defVar(master, x[N+1]>=0, objective=1) JUMP assumes that I’m adding a new set of variables (Error: Can only create one variable at a time when adding to existing constraints.)

I know that you can add the extra variable with a new name (z for example) or an anonymous variable, and iterate adding new variables at each step (and it works, even if all new variables have the same name), but I’ll like to add the new variable to the previous set x, or at least, to give a new name to each new variable. Is is possible?


#2

Hi @borelian,

It looks like you’re using a very old version of JuMP! The current syntax is @variable.

x is just a vector, so you can push! new elements onto it. You can also set the “name” of the variable using the basename keyword. See below:

model = Model()
N = 5
@variable(model, x[1:N] >= 0)
@constraint(model, con, sum(x) <= 1)
@objective(model, Max, sum(i* x[i] for i in 1:N))
push!(x, @variable(model, 
    lowerbound=0, 
    objective = N+1, 
    basename = "x[$(N+1)]", 
    inconstraints=[con], 
    coefficients=[1.0]
))