How to define a subset of some variables integers?

I need to define some variables as integer based on input. The following code doesn’t work as the name x is already used, and at the same time I don’t know the syntax to add it as a constraint:

for a in 1:nActivities
    if activities.integer[a] == 1
        @variable(profitModel, 0 <= x[a], Int)
    else
        @variable(profitModel, 0 <= x[a])
    end
end

The possibility of extension of a variable set already defined was considered and discarded here. (But there is a workaround if you need one, you just need to create anonymous variables, call set_name over them, and append! them to the already existing model[:var_name] vector.)

However, if you can define all variables first, then you can just change part of them to integer after, see here.

2 Likes

Indeed. I solved by using:

@variables profitModel begin
    x[1:nA] >= 0
end

for a in 1:nA
    if activities.integer[a] == 1
        set_integer(x[a])
    end
end

Thanks

2 Likes