# Repeated indext at optimization problem in JuMP

**URL:** <https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [January 9, 2022, 6:21pm UTC](https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295 "2022-01-09T18:21:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuri\_Mizusawa](https://avatars.discourse-cdn.com/v4/letter/y/e480ec/32.png) [@Yuri\_Mizusawa](https://discourse.julialang.org/u/Yuri_Mizusawa)\
**Post date:** [January 9, 2022, 6:21pm UTC](https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295/1 "2022-01-09T18:21:55Z")

</div>

Hi,  
I getting an error that says " **Repeated index 1. Index sets must have unique elements.**"  
My problem has to be the following structure:  
x(1,0,0)+x(1,0,4)+x(1,4,0)+x(1,4,4)+x(2,0,0)+x(2,4,0)+x(2,6,0) Max  
I reduced one dimesion by concatenating the two last variables (eg. 0\_0,0\_4), as it is just coordinates in a 2D field.  
So I have the following matix:  
0 0 “0\_0” 1  
0 4 “0\_4” 1  
4 0 “4\_0” 1  
4 4 “4\_4” 1  
0 0 “0\_0” 2  
4 0 “4\_0” 2  
6 0 “6\_0” 2  
I was expecting to use the following code lines to make the OF:

> @variable(model,x[i=df\_coord\_pq[:,4],p\_q=df\_coord\_pq[:,3]], binary=true)  
> @objective(model, Max, sum(x))

But I get the the error of the repeated index. For this case in particular I can work around with concatenating again (1\_0\_0, 1\_0\_4…), but if I end up having diffenrent restrictions that migth not be the case.  
Am I missing something? As I am new to JuMP and Julia that migth be the case.  
Thank you for the help!  
Cheers!

---

<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:** [January 9, 2022, 10:05pm UTC](https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295/2 "2022-01-09T22:05:26Z")

</div>

The two indices attempt to make a rectangular matrix of variables.

But it seems instead that you want one variable for each row, with the key composed of the 4th and 3rd columns?

I think you want this instead:

```julia
using JuMP
df_coord_pq = [
    0 0 "0_0" 1
    0 4 "0_4" 1
    4 0 "4_0" 1
    4 4 "4_4" 1
    0 0 "0_0" 2
    4 0 "4_0" 2
    6 0 "6_0" 2
]
model = Model()
S = [(i, p_q) for (i, p_q) in zip(df_coord_pq[:,4], df_coord_pq[:,3])]
@variable(model, x[S], Bin)
@objective(model, Max, sum(x))
x[(2, "6_0")]

```

Here is a tutorial which explains things in more detail: [Getting started with sets and indexing · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_sets_and_indexing/)

---

<div class="post-metadata">

**Author:** ![Yuri\_Mizusawa](https://avatars.discourse-cdn.com/v4/letter/y/e480ec/32.png) [@Yuri\_Mizusawa](https://discourse.julialang.org/u/Yuri_Mizusawa)\
**Post date:** [January 11, 2022, 9:59pm UTC](https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295/3 "2022-01-11T21:59:53Z")

</div>

Thank you, I think that solves it.  
I will study the indexes a bit more, I am still a bit confused on how to work with them.  
Cheers!

---

<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:** [January 11, 2022, 10:34pm UTC](https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295/4 "2022-01-11T22:34:36Z")

</div>

I don’t know if I have a good way of explaining indices in general. I’d encourage you to read through the documentation which has a range of usages.

If this helps:

Your original code tried to create a 7x7 matrix with the following names:

```julia
  "0_0" "0_4" "4_0" "4_4" "0_0" "4_0" "6_0"
1 . . . . . . .
1 . . . . . . .
1 . . . . . . .
1 . . . . . . .
2 . . . . . . .
2 . . . . . . .
2 . . . . . . .

```

What is `x[1, "0_0"]`? Is it the top-left element? Or perhaps the element on row  
4 and column 5? That’s why we threw an error about unique elements.

What you actually wanted was to construct a 7 element vector with the following  
keys:

```julia
1 "0_0" .
1 "0_4" .
1 "4_0" .
1 "4_4" .
2 "0_0" .
2 "4_0" .
2 "6_0" .

```

This only has one index dimension, and it’s formed by the `(i, p_q)` tuples.

---

<div class="post-metadata">

**Author:** ![Yuri\_Mizusawa](https://avatars.discourse-cdn.com/v4/letter/y/e480ec/32.png) [@Yuri\_Mizusawa](https://discourse.julialang.org/u/Yuri_Mizusawa)\
**Post date:** [January 18, 2022, 11:04pm UTC](https://discourse.julialang.org/t/repeated-indext-at-optimization-problem-in-jump/74295/5 "2022-01-18T23:04:56Z")

</div>

Hi @odow,  
This last explanation really helped me.  
Your second example is what I was looking for. The way I was viewing the creation of the variables was not related with a matrix but with the mathematical problem (basically sum of two indexes “i” and “X\_X”).  
If I got it right, for JuMP you create an empty matrix to be filled by the optimization so as I was creating a 7x7 matrix it wold not work. I tried a few examples and that seems to be the case.  
Just for the record I have this model in Python and was trying to replicate on Julia as training, it is a Pallet Loading problem bottom left one pallet one box maximize. So the “0\_0” are viable grid coordinates and the “i” is vertical or horizontal boxes.  
Really appreciate the help!
