# List comprehension of JuMP variables

**URL:** https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [June 13, 2021, 5:49pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848 "2021-06-13T17:49:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 13, 2021, 5:49pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/1 "2021-06-13T17:49:27Z")

</div>

Beginner to Julia and want to quickly recapitulate an ILP model I wrote in Python to evaluate speed in JuMP.

I’m wondering how list comprehensions work for JuMP objects?

In Python, I use list comprehension to make the following constraints:

```julia
# PYTHON
for k in range(0,math.ceil(K_window/34)):
    model.addConstr(y_i_k.sum('*',[l for l in range(k*34,min(k*34+34,K_window))]) <=1,name='Seen_'+str(k))

```

After reading about Julia syntax on list comprehension, I tried the following but it does not work:

```julia
# JULIA
for k in 1:floor(Int, K_window/34)
    @constraint(model, sum(y_i_k[:, l] for l in (k*34):min(k*34, min(k*34+34, K_window)), dims=1) <=1)
end

```

I realize this is question may alternatively be categorized as “First Steps”, I have a hunch it’s just me not understanding Julia syntax? Thank you for your help.

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [June 13, 2021, 6:11pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/2 "2021-06-13T18:11:18Z")

</div>

So `y_i_k[:, l]` is the `l`th column of `y_i_k`. So the sum is a vector. Then you constraint this sum to be smaller than 1. What does it means for a vector to be smaller than a number ?  
If you want each entry to be smaller than 1, do `.<=` instead of `<=`.

---

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 13, 2021, 6:12pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/3 "2021-06-13T18:12:50Z")

</div>

I see, I’d like to get the column sum. I was expecting a scalar output, rather than a vector.

More explicitly, I want the sum of all entry in that column to be less than or equal to 1

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [June 13, 2021, 6:14pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/4 "2021-06-13T18:14:48Z")

</div>

> [@anon94806364](#):
>
> `@constraint(model, sum(y_i_k[:, l] for l in (k*34):min(k*34, min(k*34+34, K_window)), dims=1) <=1)`

Here, you are constraining, for each row, the sum of all entries of that row that are in the column range `(k*34):min(k*34, min(k*34+34, K_window))` to be smaller than or equal to 1.

---

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 13, 2021, 6:21pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/5 "2021-06-13T18:21:25Z")

</div>

Oops, yes you are right, I am trying to constrain each row sum of given indeces to be less than or equal to 1.

I find the following expression works. Maybe there is a more elegant way of writing this?

```julia
for k in 1:floor(Int, K_window/34)
    @constraint(model, [i in 1:I_round], sum(y_i_k[i, l] for l in [k*34:min(k*34, min(k*34+34, K_window))]) <=1)
end

```

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [June 13, 2021, 6:29pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/6 "2021-06-13T18:29:46Z")

</div>

> [@anon94806364](#):
>
> `[k*34:min(k*34, min(k*34+34, K_window))]`

I doesn’t work for me because `[k*34:min(k*34, min(k*34+34, K_window))]` is a vector containing one range so the for loop has one iteration with `l = k*34:min(k*34, min(k*34+34, K_window))` and the `sum` is therefore the some of one term which is the term itself and you are left with a vector that cannot be compared to 1.  
So either do  
`sum(y_i_k[i, l] for l in k*34:min(k*34, min(k*34+34, K_window)))`  
or  
`sum(y_i_k[i, k*34:min(k*34, min(k*34+34, K_window))])`

---

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 13, 2021, 9:42pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/7 "2021-06-13T21:42:31Z")

</div>

Thank you for your patience and guidance @blegat!

May I ask why sometimes the list comprehensions look like they are “evaluated”, rather than expressed?

What I mean is, in Python I have written this line:

```julia
for i in range(I_round):
    for k in range(K_window):
        model.addConstr(gp.quicksum(r_i_j[i,j]*y_i_k[i,k]*s_j_k[j,k] for j in range(J))>= T1*y_i_k[i,k])

```

In Julia, the following are not expressed as terms of r\_i\_j\*y\_i\_k\*s\_j\_k, but directly evaluated (0). I wonder why this is the case?

```julia
for i in 1:I_round
    for k in 1:K_window
        @constraint(model, sum(r_i_j[i,1:J]*y_i_k[i,k]*s_j_k[1:J,k]) >= T1*y_i_k[i,k] )
    end
end

```

---

<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: [June 13, 2021, 9:59pm UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/8 "2021-06-13T21:59:49Z")

</div>

Hi there! Since you’re new to Julia, please read this post.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

In particular, it’s easier to help if you provide a minimal working example that we can copy-paste and see the same result.

> but directly evaluated (0)

Presumably, there is a typo somewhere. I can’t say more because I can’t run your code. The direct equivalent of your Python constraint is:

```Julia
@constraint(
    model, 
    [i=1:I_round, k=1:K_window],
    sum(r_i_j[i, j] * y_i_k[i, k] * s_j_k[j, k] for j in 1:J) >= T1 * y_i_k[i, k],
)

```

---

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 14, 2021, 5:42am UTC](https://discourse.julialang.org/t/list-comprehension-of-jump-variables/62848/9 "2021-06-14T05:42:03Z")

</div>

I see, the indeces can be provided outside the summation, and the list comprehension can still be employed inside.

I will read over the introductory post, thank you for pointing me to this reference!
