Newbie mathopt question: JuMP declare or restrain a variable to a few integers, not in a row

I am looking to be able to declare a vector of integer variables and restrain them to 1, 2, 3 … some finite number of non sequential integers. Should I create some roundabout lookup table to interpret sequential integers? (this is very similar to Bin, except with a configurable set)

To restrict x to take one of the values in y, a naive approach is as follows:

using JuMP
model = Model()
@variable(model, x)
y  = [2, 3, 5, 7, 11]
N = length(y)
@variable(model, z[1:N], Bin)
@constraint(model, x == sum(y[i] * z[i] for i in 1:N))
@constraint(model, sum(z) == 1)
1 Like

Yeah, this is sorta what I was referring to with the “roundabout lookup table” type idea, with the additional benefit that the values of x are easily interpretable.

I tried it out and I think this makes sense, thanks!