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:
Putting together only the first part:
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:
@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:
@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):
@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.
– Raquel Santos