# Indexing a const array with a JuMP variable

**URL:** <https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283>\
**Category:** Optimization (Mathematical)\
**Created:** [July 12, 2019, 10:46am UTC](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283 "2019-07-12T10:46:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bocc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bocc/32/7546_2.png) [@bocc](https://discourse.julialang.org/u/bocc)\
**Post date:** [July 12, 2019, 10:46am UTC](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283/1 "2019-07-12T10:46:36Z")

</div>

Hello,

let’s assume I have multiple 2D arrays (maps, tables…), each row of those belongs to a single design in a Pareto set (objective & parameter values).

I want to optimize on the combinations of those, therefore (I assume) I want to be able to index their rows with a discrete JuMP variable, so I can refer to them in the constraints & objective.

Do you think it’s possible/a reasonable thing to do?

Thank you!

---

<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 12, 2019, 1:25pm UTC](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283/2 "2019-07-12T13:25:03Z")

</div>

Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757), and provide a minimal working example.

Note that the following will _not_ work:

```nohighlight
model = Model()
@variable(model, x)
data = [1, 2, 3]
@objective(model, data[x]) # Can't index with x.

```

Instead, you should do something like the following:

```nohighlight
model = Model()
data = [1, 2, 3]
@variable(model, x[1:length(data)], Bin)
@constraint(model, sum(x) == 1)
@objective(model, sum(x[i] * data[i] for i in 1:length(data))

```

Here, the binary variables `x` choose each element out of `data`, and there is a constraint that you can only choose one element.

---

<div class="post-metadata">

**Author:** ![bocc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bocc/32/7546_2.png) [@bocc](https://discourse.julialang.org/u/bocc)\
**Post date:** [July 12, 2019, 2:41pm UTC](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283/3 "2019-07-12T14:41:16Z")

</div>

Thank you, and sorry, will provide a working example next time!
