# Good practice for subset in Julia/JuMP

**URL:** <https://discourse.julialang.org/t/good-practice-for-subset-in-julia-jump/56405>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 3, 2021, 1:05pm UTC](https://discourse.julialang.org/t/good-practice-for-subset-in-julia-jump/56405 "2021-03-03T13:05:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![felcbd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felcbd/32/37642_2.png) [@felcbd](https://discourse.julialang.org/u/felcbd)\
**Post date:** [March 3, 2021, 1:05pm UTC](https://discourse.julialang.org/t/good-practice-for-subset-in-julia-jump/56405/1 "2021-03-03T13:05:02Z")

</div>

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 !  
Thanks in advance for help

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

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 3, 2021, 1:16pm UTC](https://discourse.julialang.org/t/good-practice-for-subset-in-julia-jump/56405/2 "2021-03-03T13:16:05Z")

</div>

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:

```julia
@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:

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

```

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

---

<div class="post-metadata">

**Author:** ![felcbd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felcbd/32/37642_2.png) [@felcbd](https://discourse.julialang.org/u/felcbd)\
**Post date:** [March 3, 2021, 1:25pm UTC](https://discourse.julialang.org/t/good-practice-for-subset-in-julia-jump/56405/3 "2021-03-03T13:25:42Z")

</div>

Thanks for the quick answer!  
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.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 3, 2021, 1:39pm UTC](https://discourse.julialang.org/t/good-practice-for-subset-in-julia-jump/56405/4 "2021-03-03T13:39:29Z")

</div>

Well, you can also do:

```julia
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.

> [@felcbd](#):
>
> For the head/tail part, is it better to have x[i , j] than x[(i , j)] ? I was considering the 2 options.

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

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

```
