Repeated indext at optimization problem in JuMP

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!

1 Like

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:

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

1 Like

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!

1 Like

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:

  "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:

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.

1 Like

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!

1 Like