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

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

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

1 Like

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

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))
3 Likes

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

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
3 Likes