# How to define a variable with specific values from a set using JuMP

**URL:** <https://discourse.julialang.org/t/how-to-define-a-variable-with-specific-values-from-a-set-using-jump/130808>\
**Category:** Optimization (Mathematical)\
**Created:** [July 17, 2025, 1:33pm UTC](https://discourse.julialang.org/t/how-to-define-a-variable-with-specific-values-from-a-set-using-jump/130808 "2025-07-17T13:33:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luiz\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Luiz\_Monteiro](https://discourse.julialang.org/u/Luiz_Monteiro)\
**Post date:** [July 17, 2025, 1:33pm UTC](https://discourse.julialang.org/t/how-to-define-a-variable-with-specific-values-from-a-set-using-jump/130808/1 "2025-07-17T13:33:45Z")

</div>

Hi,  
I’m using JuMP and I would like to define a variable that can only assume specific values from a predefined set, for example:

allowed\_values = [10.0, 10.5, 11.0, 11.5, 12.0]

Is there a way to define a variable in JuMP that is restricted to take values only from this set? If so, how can I implement this?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![kziliask](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kziliask](https://discourse.julialang.org/u/kziliask)\
**Post date:** [July 17, 2025, 6:42pm UTC](https://discourse.julialang.org/t/how-to-define-a-variable-with-specific-values-from-a-set-using-jump/130808/2 "2025-07-17T18:42:42Z")

</div>

You could add binary variables for each discrete value times the value and have your variable equal the sum of those, while also having the sum of the binary variables equal 1 as a second constraint to make sure exactly one is selected.

Like x = 10z1 + 10.5z2 + …  
& z1 + z2 + … = 1

---

<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 17, 2025, 10:59pm UTC](https://discourse.julialang.org/t/how-to-define-a-variable-with-specific-values-from-a-set-using-jump/130808/3 "2025-07-17T22:59:06Z")

</div>

Hi @Luiz_Monteiro,

This is not something that is implemented in JuMP directly, but you can achieve it with a MIP reformulation, just as @kziliask describes:

```julia
using JuMP
allowed_values = [10.0, 10.5, 11.0, 11.5, 12.0]
model = Model()
@variable(model, x)
@variable(model, z[1:length(allowed_values)], Bin)
@constraint(model, sum(z) == 1)
@constraint(model, x == allowed_values' * z)

```

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [July 18, 2025, 1:21am UTC](https://discourse.julialang.org/t/how-to-define-a-variable-with-specific-values-from-a-set-using-jump/130808/4 "2025-07-18T01:21:56Z")

</div>

it suffices with a general integer variable

```julia
@variable(model, 0 ≤ n ≤ 4, Int)
@expression(model, x, 10 + n/2)

```
