# What is the best way to separate out indices of a variable?

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-separate-out-indices-of-a-variable/85317>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization, modelling\
**Created:** [August 4, 2022, 9:05pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-separate-out-indices-of-a-variable/85317 "2022-08-04T21:05:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [August 4, 2022, 9:05pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-separate-out-indices-of-a-variable/85317/1 "2022-08-04T21:05:31Z")

</div>

Hi guys,

What do you recommend to do in this situation?

I have a set `M` consisting of three separate sets of vectors of various size. In other words `M = [m_1, m_2, m_3] ` where `m_i` are vectors of different sizes. For instance `m_1` could be `1:10` while `m_2 = 22:65` and `m_3 = 100:123`(these are separate, no intersection in their elements).

with that, there need to have a variable `x[i,j,m]` to have some constraints as the following:

```julia
# N is a set which contains all net nodes 
# x,z are variables

x[i,j,m] >= z[i,j] for all i,j in N and m in M  

```

Now, we have another variables that are only defined on `m_i` like `y[i,j,m_1]`. So, at some points in some constraints, there’s only need to put condition over a subset of `M` (not all of its components). Similar to the following:

```julia
#I'm not writing like julia-- more like math version

sum(x[i,j,m] for i in N) <= y[i, m] for all j in N and m in m_1

```

I did try to do this by:

```julia
@constraint(model, [j in N, m in m_1], sum( x[i,j,m] for i in N) <= y[i, m] ) 

```

This is not working though. Do you know how to do this? having a variables that can act both over the whole set `M` and also act locally over just `m_1` or just `m_2`  
I’m not sure if the issue is variable either. What I need to find out is that how to have flexible variable `x` that can handle `M` and `m_1` at the same time.

Thanks all!

---

<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:** [August 5, 2022, 9:51pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-separate-out-indices-of-a-variable/85317/2 "2022-08-05T21:51:31Z")

</div>

I think you need something like this:

```julia
M = [1:2, 4:5, 7:8]
model = Model()
all_M = unique(reduce(vcat, M))
@variable(model, x[all_M])
@expression(model, sum(x[m] for m in all_M))
@expression(model, sum(x[m] for m in M[1]))

```

What probably happened is that it used `m_1`, `m_2`, an `m_3` as the keys of the variable, not the values of the `m_` vectors as the keys.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [August 10, 2022, 12:17am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-separate-out-indices-of-a-variable/85317/4 "2022-08-10T00:17:03Z")

</div>

I would say that the former is better for a single `@variable` declaration, since you’re not allocating an intermediate array (`x_indices`) but allowing JuMP to generate variables directly from your separate index sets. The latter is virtually equivalent but necessary if reusing an index set like `x_indices` in multiple locations.

There is the concept of generating values on demand in general in Julia: [Generator Expressions](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions).

---

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [August 10, 2022, 7:07am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-separate-out-indices-of-a-variable/85317/5 "2022-08-10T07:07:22Z")

</div>

Thanks for your info and the link @jd-foster I’ll check it out!
