# Good practice for subset in Julia/JuMP

Hello everyone,
I have some questions about good practice in Julia/JuMP to manipulate subset of elements in a model.
In my usage, I want to do a model using graph, so I have my graph G=(V, E).
All edges in E are a struct, containing the id of the edge and an other Tuple containing the id of the vertices who are connecting.
struct edge
id::Int64
ij:Tuple{Int64,Int64}
end

So E can look like this : [ (1,(1,3)), (2,(1,2)) , (3, (2,3)) ]

But in my model, I may have the use of a subset of E. For example all the edges connecting the destination vertex (here the edges connecting the vertex 3), so I have an Array A containing [1,3], the two edges who connect to the vertex 3, and this is the subset I want to work with.

Then I’m trying to create Variables in JuMP with the information of the edges which id is in A. So I want in this example , 2 variables x[(1,3)] and x[(2,3)].
I’m trying with something like :
@variable(model, x[ E[ e in A].ij], Bin]

But as you can expect the “e in A” doesn’t work, and I don’t know how to iterate on E with the ID stocked in the subset A to extract only the edges I want.

My question is : Is the separation I made between my Array with all the edges, and from the other side a subset containing the ID, a good way to represent subset in Julia? And how can I use my subset inside the @variable/@constraint in JuMP ?
I don’t know if I’m clear enough, feel free to ask more precision !

PS: Sorry for the fail post earlier, I delete it

Unless your identification of the edges is more than just sequential integer numbering, I would advise to you to just drop the `id` from inside and have just a single array of tuples representing edges, the `id` is given by the element position. If you do this you can:

``````@variable(model, x[E[A]], Bin]
``````

Considering that `A` is a `Vector{Int}` (i.e., a list of the edges indexes).

You can also go further and separate `E` in `tail` and `head` (two vector of node indexes), and do something like:

``````@variable(model, x[i in tail, j in head], Bin]
``````

That will be indexed by `x[i, j]` instead of `x[(i, j)]`.

I made it easier for the example but in my program, edges carry other information For the head/tail part, is it better to have x[ i , j ] than x[ (i , j) ] ? I was considering the 2 options.

Well, you can also do:

``````x[t in getproperty.(E[A], (:ij,))]
``````

To index using tuples like `x[(1, 3)]`. It is not pretty but it does the job. I sincerely always work with struct of vector and not vector of struct when making mathematical models. Feels more natural.

`x[i, j]` is just more common than `x[(i, j)]` so, if you try to use the first, then probably everything will be indexed in the same way, and you will not lose time with small bugs because you forgot to wrap the indexes sometimes (or wrapped indexes from a matrix that should not be wrapped). You can achieve the `x[i, j]` notation with:

``````x[i in first.(getproperty.(E[A], (:ij,))), j in last.(getproperty.(E[A], (:ij,)))]
``````
1 Like