 # How to use the summation in Julia?

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? I started doing like this

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

#sets
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]))
``````

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

For your example, you want something like this:

``````using JuMP
model = Model()

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

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)
)
``````
2 Likes

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

4 Likes

Oi @abelsiqueira, vou olhar sim.

@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.

I started doing like this

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

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

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

2 Likes