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

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!

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

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:

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)

it suffices with a general integer variable

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