How to specify variable with different indexes/bounds in JuMP

Hello, I would like to declare in JuMP a variable s whose bounds depends form its index, with some indexes fixed.
The problems is that if I declare it twice I get an error that the variable already attached to the model, e.g.:

idxSet1 = [1,3,5]
idxSet0 = [2,4]
m = Model(Ipopt.Optimizer)
@variable(m, 0 <= s[idx in idxSet1] <= 1)
@variable(m, s[idx in idxSet0 ] == 0) # ERROR , s already exists

Of course a workaround is to make the fixation a constrain:

idxSet1 = [1,3,5]
idxSet0 = [2,4]
m = Model(Ipopt.Optimizer)
@variable(m, 0 <= s[idx in union(idxSet1,idxSet0)] <= 1)
@constraint(m,sfix[idx in idxSet0 ], s[idx] == 0)

However I am worry that I have more variables than needed here, unless JuMP and the solver engine are smart enough to remove them before the actual optimisation…

I think I found it:

@variable(m, 0 <= s[idx in union(idxSet1,idxSet0)] <= 1)
for idx0 in idxSet0 
     fix(s[idx0], 0.0; force=true);
end
1 Like

Or even just:

idxSet0, idxSet1 = [2, 4], [1, 3, 5]
model = Model()
@variable(model, 0 <= s[union(idxSet1, idxSet0)] <= 1)
set_upper_bound.(s[idxSet0], 0)
1 Like