# Is there a way to bring the range of a summation back from an array?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, range, sum\
**Created:** [May 30, 2022, 10:27pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937 "2022-05-30T22:27:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicentemunozlh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vicentemunozlh/32/36766_2.png) [@vicentemunozlh](https://discourse.julialang.org/u/vicentemunozlh)\
**Post date:** [May 30, 2022, 10:27pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/1 "2022-05-30T22:27:49Z")

</div>

Hello, I am new to Julia and trying to model a  
relatively complicated optimization problem from other lenguage(GAMS). I am stuck in trying to apply a summation in a constraint and its depndence index to be in a range present in an array.

For example:

```julia
a=["1:10","5:10","7:10"]
@variable(m, x[i in 1:32, j in 1:10])
@constraint(m, con[i in 1:32], sum(x[i,j] for j in a[i] ) >=0 )

```

I have 2 problems from these:

1. Apparently you cant call a array value to the sum
2. I dont think the range beeing a string can be used either.

If anyone has an idea to replace this would be greate. The model has over 200 variables so its too time consuming to do it manually.

Thanks!

---

<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:** [May 31, 2022, 6:18am UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/2 "2022-05-31T06:18:40Z")

</div>

I moved this to the Optimization (Mathematical) category, since it appears you are using JuMP specifically.

You probably want to write `a` as just

```julia
a = [1:10, 5:10, 7:10]

```

then

```julia
@variable(m, x[i in 1:32, j in 1:10])

```

and change your constraint declaration to something like

```julia
@constraint(m, con[i in 1:32, k in 1:length(a)], sum(x[i,j] for j in a[k] ) >=0 )

```

I would need more context to know if this is what you want to do, since in this definition of `a` it has only 3 “sub-ranges” to sum over.

---

<div class="post-metadata">

**Author:** ![vicentemunozlh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vicentemunozlh/32/36766_2.png) [@vicentemunozlh](https://discourse.julialang.org/u/vicentemunozlh)\
**Post date:** [May 31, 2022, 1:57pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/3 "2022-05-31T13:57:02Z")

</div>

Hello, thank you for the fast response! It works perfect like that for array of 1D but I have not been able to do it for 2D array.  
Let me explain better the problem: this is a constraint depending on agents and years.

```julia
using JuMP
A=["1:32";"2:32";;"3:32";"4:32" ......, "32:32"] 

model=Model()
@variable(model, x[i in 1:10,w in 1:32])
@constraint(model, con2[i in 1:10, w in 1:32], x[i,w] + sum(y[i,k] for k in a[i,w] ) >= 0)
println(model)

```

So, A is a 2D array of sets of times. 1:32 = 1,2,3,4 ,… ,32.

Every agent i takes a desition x\_i,w. But, also there is a desition y \_i,k in which the year k will depend on the set of years given by the array A for agent i and year w. So, if it is agent 1 and year 1, the set for the summation is 1:32, so, there will be a sumation of y[1,1] + y[1,2] + … + y[1,32].  
So my problem is how to be able to put the sets of time in a 2D array or any other suggestion to attack the problem.

Thanks !

---

<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:** [May 31, 2022, 7:45pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/4 "2022-05-31T19:45:56Z")

</div>

You might want to take a look at some of the introductory JuMP tutorials: [Getting started with sets and indexing · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_sets_and_indexing/)

Your problem is that `"1:32"` is a `String`, which is something very different to `1:32`:

```julia
julia> x = "1:32"
"1:32"

julia> y = 1:32
1:32

julia> typeof(x)
String

julia> typeof(y)
UnitRange{Int64}

```

Do you want something like

```nohighlight
julia> A = [i:32 for i in 1:32]
32-element Vector{UnitRange{Int64}}:
 1:32
 2:32
 3:32
 4:32
 5:32
 6:32
 7:32
 8:32
 9:32
 10:32
 11:32
 12:32
 13:32
 14:32
 15:32
 16:32
 17:32
 18:32
 19:32
 20:32
 21:32
 22:32
 23:32
 24:32
 25:32
 26:32
 27:32
 28:32
 29:32
 30:32
 31:32
 32:32

```

But even that might be too much. There’s a simpler way that I would write your model:

```julia
using JuMP
model = Model()
@variable(model, x[1:10, 1:32])
@constraint(model, [i=1:10, w=1:32], x[i, w] + sum(y[i, k] for k in w:32) >= 0)

```

---

<div class="post-metadata">

**Author:** ![vicentemunozlh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vicentemunozlh/32/36766_2.png) [@vicentemunozlh](https://discourse.julialang.org/u/vicentemunozlh)\
**Post date:** [May 31, 2022, 8:01pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/5 "2022-05-31T20:01:26Z")

</div>

Hello. I knew that it cant be in string mode. O just put it that way because i have not been able to program a 10x32 matrix or 2D array with the elements beeing ranges

---

<div class="post-metadata">

**Author:** ![sebacea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebacea/32/36520_2.png) [@sebacea](https://discourse.julialang.org/u/sebacea)\
**Post date:** [May 31, 2022, 8:08pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/6 "2022-05-31T20:08:40Z")

</div>

[https://jump.dev/JuMP.jl/stable/tutorials/getting\_started/getting\_started\_with\_sets\_and\_indexing/#Sets-of-other-things](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_sets_and_indexing/#Sets-of-other-things)

```julia
sources = [1:5;]
sinks = [1:32;]
S = [(source, sink) for source in sources, sink in sinks]

```

---

<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:** [May 31, 2022, 9:13pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/7 "2022-05-31T21:13:58Z")

</div>

> [@vicentemunozlh](#):
>
> i have not been able to program a 10x32 matrix or 2D array with the elements beeing ranges

What should `A[i, j]` be?

```julia
julia> A = [a:b for a in 1:10, b in 1:32]
10×32 Matrix{UnitRange{Int64}}:
 1:1 1:2 1:3 1:4 1:5 1:6 1:7 1:8 1:9 1:10 … 1:24 1:25 1:26 1:27 1:28 1:29 1:30 1:31 1:32
 2:1 2:2 2:3 2:4 2:5 2:6 2:7 2:8 2:9 2:10 2:24 2:25 2:26 2:27 2:28 2:29 2:30 2:31 2:32
 3:2 3:2 3:3 3:4 3:5 3:6 3:7 3:8 3:9 3:10 3:24 3:25 3:26 3:27 3:28 3:29 3:30 3:31 3:32
 4:3 4:3 4:3 4:4 4:5 4:6 4:7 4:8 4:9 4:10 4:24 4:25 4:26 4:27 4:28 4:29 4:30 4:31 4:32
 5:4 5:4 5:4 5:4 5:5 5:6 5:7 5:8 5:9 5:10 5:24 5:25 5:26 5:27 5:28 5:29 5:30 5:31 5:32
 6:5 6:5 6:5 6:5 6:5 6:6 6:7 6:8 6:9 6:10 … 6:24 6:25 6:26 6:27 6:28 6:29 6:30 6:31 6:32
 7:6 7:6 7:6 7:6 7:6 7:6 7:7 7:8 7:9 7:10 7:24 7:25 7:26 7:27 7:28 7:29 7:30 7:31 7:32
 8:7 8:7 8:7 8:7 8:7 8:7 8:7 8:8 8:9 8:10 8:24 8:25 8:26 8:27 8:28 8:29 8:30 8:31 8:32
 9:8 9:8 9:8 9:8 9:8 9:8 9:8 9:8 9:9 9:10 9:24 9:25 9:26 9:27 9:28 9:29 9:30 9:31 9:32
 10:9 10:9 10:9 10:9 10:9 10:9 10:9 10:9 10:9 10:10 10:24 10:25 10:26 10:27 10:28 10:29 10:30 10:31 10:32

```

---

<div class="post-metadata">

**Author:** ![vicentemunozlh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vicentemunozlh/32/36766_2.png) [@vicentemunozlh](https://discourse.julialang.org/u/vicentemunozlh)\
**Post date:** [May 31, 2022, 9:24pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937/8 "2022-05-31T21:24:10Z")

</div>

That was what I needed. Thank you very much!
