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

**URL:** <https://discourse.julialang.org/t/newbie-mathopt-question-jump-declare-or-restrain-a-variable-to-a-few-integers-not-in-a-row/27726>\
**Category:** Optimization (Mathematical)\
**Created:** [August 19, 2019, 5:08pm UTC](https://discourse.julialang.org/t/newbie-mathopt-question-jump-declare-or-restrain-a-variable-to-a-few-integers-not-in-a-row/27726 "2019-08-19T17:08:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhalonen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bhalonen/32/9848_2.png) [@bhalonen](https://discourse.julialang.org/u/bhalonen)\
**Post date:** [August 19, 2019, 5:08pm UTC](https://discourse.julialang.org/t/newbie-mathopt-question-jump-declare-or-restrain-a-variable-to-a-few-integers-not-in-a-row/27726/1 "2019-08-19T17:08:59Z")

</div>

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)

---

<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:** [August 19, 2019, 6:47pm UTC](https://discourse.julialang.org/t/newbie-mathopt-question-jump-declare-or-restrain-a-variable-to-a-few-integers-not-in-a-row/27726/2 "2019-08-19T18:47:01Z")

</div>

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

```nohighlight
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)

```

---

<div class="post-metadata">

**Author:** ![bhalonen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bhalonen/32/9848_2.png) [@bhalonen](https://discourse.julialang.org/u/bhalonen)\
**Post date:** [August 19, 2019, 7:17pm UTC](https://discourse.julialang.org/t/newbie-mathopt-question-jump-declare-or-restrain-a-variable-to-a-few-integers-not-in-a-row/27726/3 "2019-08-19T19:17:39Z")

</div>

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!
