# Define a variable in two ranges

**URL:** <https://discourse.julialang.org/t/define-a-variable-in-two-ranges/22959>\
**Category:** Optimization (Mathematical)\
**Created:** [April 9, 2019, 2:16pm UTC](https://discourse.julialang.org/t/define-a-variable-in-two-ranges/22959 "2019-04-09T14:16:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![muriloasouza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muriloasouza/32/4650_2.png) [@muriloasouza](https://discourse.julialang.org/u/muriloasouza)\
**Post date:** [April 9, 2019, 2:16pm UTC](https://discourse.julialang.org/t/define-a-variable-in-two-ranges/22959/1 "2019-04-09T14:16:47Z")

</div>

Is there anyway i can define a JuMP variable in two different ranges?

Say the first range is between 3:6 and the second one between 9:12.

---

<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:** [April 9, 2019, 3:00pm UTC](https://discourse.julialang.org/t/define-a-variable-in-two-ranges/22959/2 "2019-04-09T15:00:50Z")

</div>

No. But you can write a reformulation like:

```julia
@variable(model, range_variable)
x = @variable(model, [1:2])
z = @variable(model, [1:2], Bin)
@constraints(model, begin
    3z[1] <= x[1]
             x[1] <= 6z[1]
    9z[2] <= x[2]
             x[2] <= 12z[2]
    z[1] + z[2] == 1
    range_variable == x[1] + x[2]
end)

```

---

<div class="post-metadata">

**Author:** ![muriloasouza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muriloasouza/32/4650_2.png) [@muriloasouza](https://discourse.julialang.org/u/muriloasouza)\
**Post date:** [April 10, 2019, 12:00pm UTC](https://discourse.julialang.org/t/define-a-variable-in-two-ranges/22959/3 "2019-04-10T12:00:24Z")

</div>

Thank you for the tip! I’m going to try that!
