How to use the sum in Julia?

Asked

Viewed 132 times

1

I need to put this objective function on Julia. Can someone help me?

inserir a descrição da imagem aqui

I started to do so, but I do not know how to join the objective functions as in the image above.

using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))
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(Model1,r[A,T], lower_bound=0)
@variable(Model1,o[A,T], lower_bound=0)
@variable(Model1,wr[W,T], lower_bound=0)
@variable(Model1,wo[W,T], lower_bound=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))
@objective(Model1,Min,sum(CR[w]*(wr[w,t]+wo[w,t])+CO[w]*wo[w,t] for w in W, t 
in T))

1 answer

2


A suggestion to solve complex equations is to "break them" into smaller parts, then go assembling and testing the program gradually, for each item you insert, and avoid arriving at the end and occur an error that will be difficult to locate (and that goes for any programming language).

Come on!

Assuming the variable declaration is correct, to mount the objective function you can follow the following steps:

From the original equation:

Equação matemática original

Putting together only the first part:

primeira parte da equação

The code is as follows::

@objective(Model1, Min, sum(CR[widx] 
                            for widx in W))

You can also put everything in one line, but this way it is easier to view.

At this point, the hint is to use different names for the looping variables (widx, for example, instead of w which may be confused with W).

Then add the part of the equation that is within the first parentheses:

segunda parte da equação

@objective(Model1, Min, sum(CR[widx] * (r[aidx, tidx] + o[aidx, tidx]) 
                            for widx in W for aidx in A for tidx in T))

Here, the two remaining variables of the sum have already been included a and t, with the names aidx and tidx, respectively.

The 3 loopings of the first summation (a,w,t) are already defined, so it is simpler to assemble the rest of this first sum:

primeira somatória da equação

@objective(Model1, Min, sum(CR[widx] * (r[aidx, tidx] + o[aidx, tidx]) + 
                            CO[widx] * o[aidx, tidx]) 
                            for widx in W for aidx in A for tidx in T))

The second sum follows the same development line (added with the first):

primeira parte da segunda somatória

@objective(Model1, Min, sum(CR[widx] * (r[aidx, tidx] + o[aidx, tidx]) +
                            CO[widx] * o[aidx, tidx] 
                            for widx in W for aidx in A for tidx in T) + 
                        sum(CR[widx] 
                            for widx in W))
                        

After adding the index widx of the second sum and test, just add the next variable t (defined in the code as tidx) and finish assembling the complete equation:

@objective(Model1, Min, sum(CR[widx] * (r[aidx, tidx] + o[aidx, tidx]) +
                            CO[widx] * o[aidx, tidx] 
                            for widx in W for aidx in A for tidx in T) +
                        sum(CR[widx] * (wr[widx, tidx] + wo[widx, tidx]) +
                            CO[widx] + wo[widx,tidx] 
                            for widx in W for tidx in T))
                            

Assuming your model has no restrictions (Constraint), simply call the function of optimizing:

julia> optimize!(Model1)
Welcome to the CBC MILP Solver
Version: 2.10.3
Build Date: Jan  1 1970

command line - Cbc_C_Interface -solve -quit (default strategy 1)
Empty problem - 0 rows, 3060 columns and 0 elements
Optimal - objective value 0
Optimal objective 0 - 0 iterations time 0.002
Total time (CPU seconds):       0.03   (Wallclock seconds):       0.03

And check the final result of the optimization:

julia> termination_status(Model1)
OPTIMAL::TerminationStatusCode = 1

Another hint is that the parameter with_optimizer within the Model function is now obsolete, so you can define your model as follows:

Model1 = Model(Cbc.Optimizer)

And to define the vectors, you can use comma ,, instead of ;.
Example:

CR = [11, 11, 11, 11, 11]
  • Problem solved!! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.