# How to use the summation in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [June 16, 2020, 2:05pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522 "2020-06-16T14:05:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 16, 2020, 2:05pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522/1 "2020-06-16T14:05:25Z")

</div>

I need to put this objective function in Julia, but I don’t know how to build a sum that has 3 indexes. Can someone help me?  
 ![FunObjJulia](https://global.discourse-cdn.com/julialang/original/3X/b/0/b0e9bf964f70951b29caafa50577dc5e7aae0076.jpeg)

I started doing like this

```julia
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))
CR = [11; 11; 11; 11; 11]
CO = [50; 50; 50; 50; 50]

#sets
a = 80 #atividades
t = 18 #peírodo de tempo
w = 5 #centro de trabalho
#variable
@variable(Model1,r[1:a,1:t], lower_bound=0)
@variable(Model1,o[1:a,1:t], lower_bound=0)
#objective function
@objective(Model1,Min,sum(CR[w]*(r[a,t]+o[a,t])+CO[w]*o[a,t]))

```

---

<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 16, 2020, 2:15pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522/2 "2020-06-16T14:15:09Z")

</div>

The JuMP repo has a number of examples that should be helpful. For example: [JuMP.jl/cannery.jl at v0.21.2 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/blob/v0.21.2/examples/cannery.jl)

For your example, you want something like this:

```nohighlight
using JuMP
model = Model()

CR = [11; 11; 11; 11; 11]
CO = [50; 50; 50; 50; 50]

A = 1:80 #atividades
T = 1:18 #peírodo de tempo
W = 1:5 #centro de trabalho

@variable(model, r[A, T] >= 0)
@variable(model, o[A, T] >= 0)

@objective(
    model, 
    Min, 
    sum(CR[w] * (r[a, t] + o[a, t]) + CO[w] * o[a, t] for w in W, a in A, t in T)
)

```

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [June 16, 2020, 2:21pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522/3 "2020-06-16T14:21:57Z")

</div>

Hi @RaquelSantos, I noticed from your code that you may be Brazilian. In that case (or if you can understand brazilian portuguese), I have created some tutorials, including a few for JuMP: [https://www.youtube.com/playlist?list=PLOOY0eChA1uyjKRQDeR4-LfsObNqslIUh](https://www.youtube.com/playlist?list=PLOOY0eChA1uyjKRQDeR4-LfsObNqslIUh)

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 16, 2020, 2:35pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522/4 "2020-06-16T14:35:35Z")

</div>

Oi @abelsiqueira, vou olhar sim.  
Obrigada pela ajuda!!

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 16, 2020, 5:59pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522/5 "2020-06-16T17:59:28Z")

</div>

@odow, @abelsiqueira Thanks for the help!

I tried to combine all the equations in a single function as in the image, only the first two sums were compiled.

 ![FOJulia4](https://global.discourse-cdn.com/julialang/original/3X/6/f/6fa9c1d22ce2ce410f4f4e2ae4b8ee9d90c3b2f1.jpeg)

I started doing like this

```julia
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))
CR = [11; 11; 11; 11; 11]
CO = [50; 50; 50; 50; 50]
PS = [4951400; 4951400; 14154430]

A = 1:80 #atividades
T = 1:18 #peírodo de tempo
W = 1:5 #centro de trabalho
I = 1:5 #centro de trabalho 

@variable(Model1, r[A,T] >= 0)
@variable(Model1, o[A,T] >= 0)
@variable(Model1,wr[W,T] >= 0)
@variable(Model1,wo[W,T] >= 0) 
@variable(Model1,d[I] >= 0) 

@objective(Model1,Min,sum(CR[w]*(r[a,t]+o[a,t])+CO[w]*o[a,t] for w in W, a in A, t in T)+sun(CR[w]*(wr[w,t]+wo[w,t])+CO[w]*wo[w,t] for w in W, t in T)+sun(PS[i]*(1-d[i]) for i in I))

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 16, 2020, 6:07pm UTC](https://discourse.julialang.org/t/how-to-use-the-summation-in-julia/41522/6 "2020-06-16T18:07:52Z")

</div>

> [@RaquelSantos](#):
>
> `sun(PS[i]*(1-d[i]) for i in I)`

it looks like you accidently wrote `sun` instead of `sum` for the third one
