# Creating a tuple from combination of tuple and vectors

I’d like to combine three thing; a `Vector{Tuple{Int64, Int64}}`, `Vector{Vector{Int64}}`, and a `Vector{Int64}` under some condition to be able to create a new tuple; in other words; a `Vector{Tuple{Int64, Int64, Int64}}`. And then define a variable `x(i,j,k)` on them.

For example, if we have the following:

``````# A is the condition
A = [(1, 2), (1, 3),
(2, 1), (2, 7), (2, 8), (2, 4),
(3, 4), (3,5), (4,3) (4,2), (4,6),(4,9),
(5,6), (7,8), (7,10), (8,9), (8,10), (9,10)]

# These are thing that we want to combine them
B = [ 2,9]
C = [ [1,7,3,4], [4,10,1,2,6,7]]
D = [12,15]

``````

We are interested in a tuples like `(i,j,k)` which satisfy these conditions: ` i in B` , `j in C`, `k in D` and `(i,j) in A`

In the above case, therefore will result in the following:

``````
[   # for  k =12
[(2,1,12), (2,7,12), (2,4,12)] ,
# for k =15
[(9,10,15)]

]
``````

I’ve trired to first filter out those combination of `(i,j)` which are satisfy three condition of ` i in B && j in C && (i,j) in A` and then put them together with additional index over `k in D`. But it throw an error every time.

Do you know of any better way?

Hi @joxane7887, welcome to the community

By looking at the example that you have provided, i assumed `length(B) == length(C) == length(D)` and came up with the below impl →

``````for (idx, i) in enumerate(B)
for j in C[idx]
(i, j) in A && println((i, j, D[idx]))
end
end
``````

output →

``````(2, 1, 12)
(2, 7, 12)
(2, 4, 12)
(9, 10, 15)
``````

I’m not sure if this is what you are expecting …!!

1 Like

Hi @Karthik-d-k Thanks. I’m a beginner, and didn’t know about enumerate. So, How is it possible to use `@variable` macro and define a `x(i,j,k)` with this indicies?

Why this one is incorrect?
`@variable(model, x[ (i, j, k) for (idx, i) in enumerate(B) for j in C[idx] && (i, j) in A for k in D[idx] ] >= 0, Bin) `

Is this question related to Define variables on each elemnts of a vector of vectors? It seems pretty similar.

``````A = [
(1, 2), (1, 3), (2, 1), (2, 7), (2, 8), (2, 4), (3, 4), (3, 5), (4, 3),
(4, 2), (4, 6), (4, 9), (5, 6), (7, 8), (7, 10), (8, 9), (8, 10), (9, 10),
]
B = [2, 9]
C = [[1, 7, 3, 4], [4, 10, 1, 2, 6, 7]]
D = [12, 15]

using JuMP
model = Model()
sets = [(b, c, d) for (k, d) in enumerate(D) for b in B for c in C[k] if (b, c) in A]
@variable(model, x[sets])
x[(2, 7, 12)]
``````
2 Likes

Quite beautiful! @odow Super thanks for this and link. I didn’t know about that.
The output of @Karthik-d-k is theone I’m trying to put in your format. `sets` contains those combination that satisfys all things and in your definition `7-element Vector{Tuple{Int64, Int64, Int64}}:` However, it should be `4-element Vector{Tuple{Int64, Int64, Int64}}:` or `like the original following format

``````[   # for  k =12
[(2,1,12), (2,7,12), (2,4,12)] ,
# for k =15
[(9,10,15)]

]

``````

I was unable spot the issue in your code ( could the issue be because of `(b, c) in A`)?

1 Like

This will give you `Vector{Tuple{Int64, Int64, Int64}}`

``````[(i, j, D[idx]) for (idx, i) in enumerate(B) for j in C[idx] if (i, j) in A]
``````
2 Likes