# How to specify variable with different indexes/bounds in JuMP

**URL:** <https://discourse.julialang.org/t/how-to-specify-variable-with-different-indexes-bounds-in-jump/96651>\
**Category:** Optimization (Mathematical)\
**Created:** [March 27, 2023, 10:58am UTC](https://discourse.julialang.org/t/how-to-specify-variable-with-different-indexes-bounds-in-jump/96651 "2023-03-27T10:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 27, 2023, 10:58am UTC](https://discourse.julialang.org/t/how-to-specify-variable-with-different-indexes-bounds-in-jump/96651/1 "2023-03-27T10:58:12Z")

</div>

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.:

```julia
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:

```julia
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…

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 27, 2023, 11:08am UTC](https://discourse.julialang.org/t/how-to-specify-variable-with-different-indexes-bounds-in-jump/96651/2 "2023-03-27T11:08:22Z")

</div>

I think I found it:

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

```

---

<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:** [March 27, 2023, 7:29pm UTC](https://discourse.julialang.org/t/how-to-specify-variable-with-different-indexes-bounds-in-jump/96651/3 "2023-03-27T19:29:55Z")

</div>

Or even just:

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

```
