# How to define a JuMP variable in a specific discrete set?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-jump-variable-in-a-specific-discrete-set/42754>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, discretization\
**Created:** [July 8, 2020, 8:31pm UTC](https://discourse.julialang.org/t/how-to-define-a-jump-variable-in-a-specific-discrete-set/42754 "2020-07-08T20:31:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZdM87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdm87/32/15353_2.png) [@ZdM87](https://discourse.julialang.org/u/ZdM87)\
**Post date:** [July 8, 2020, 8:31pm UTC](https://discourse.julialang.org/t/how-to-define-a-jump-variable-in-a-specific-discrete-set/42754/1 "2020-07-08T20:31:26Z")

</div>

I have an integer variable that takes values in `{2, 5, 6, 8, 10}`. How do I define this in JuMP?

For an integer variable, I do

```julia
@variable(model, x, Int)

```

but this will take all integer variables. I can set the upper and lower bounds limit but I would like the variable to take only few specific values as given above.

---

<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:** [July 8, 2020, 8:55pm UTC](https://discourse.julialang.org/t/how-to-define-a-jump-variable-in-a-specific-discrete-set/42754/2 "2020-07-08T20:55:04Z")

</div>

You cannot do this directly. You need to reformulate the problem.

```nohighlight
model = Model()
S = [2, 5, 6, 8, 10]
@variable(model, x, Int)
@variable(model, z[1:length(S)], Bin)
@constraint(model, sum(z) == 1)
@constraint(model, x == sum(S[i] * z[i] for i = 1:length(S))

```

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [July 8, 2020, 11:00pm UTC](https://discourse.julialang.org/t/how-to-define-a-jump-variable-in-a-specific-discrete-set/42754/3 "2020-07-08T23:00:55Z")

</div>

If that “5” is actually a typo for “4” you can do even simpler:

```julia
model = Model()
@variable(model, 1 <= y <= 5, Int)
@variable(model, x)
@constraint(model, x == 2*y) # or simply use 2*y where you would use x

```
