# Code taking too long to run. Need some advice

First time user here and I am working on a basic scheduling of jobs. The data for the code is generated using:

# Data generator

n = 20 # number of tasks
min_end = 5 # the min end time of any task
max_end = 15 # the max end time of any task
min_workload = 1 # the min workload of any task
max_workload = 4 # the max workload of any task, here we restrict max_workload <= min_end
min_bonus = 5 # the min bonus
max_bonus = 25 # the max bonus

function gen_data(is_float)
if max_workload > min_end
error(“Min end time needs to be greater that max workload”)
end

``````if is_float == false
end_time = rand(min_end:max_end, n) # the end time of each task
for i=1:n
end
start_time = end_time - workload # the start time of each task
bonus = rand(min_bonus:max_bonus, n) # the bonus of each task
else
end_time = rand(n)*(max_end - min_end) + min_end # the end time of each task
for i=1:n
end
start_time = end_time - workload # the start time of each task
bonus = rand(n)*(max_bonus - min_bonus) + min_bonus # the bonus of each task
end

data = cat(2, end_time, workload, start_time, bonus)

# return it
return data
``````

end

Function where the problem is defined is

function minNumProcessors(end_time, start_time, conflicts)
# end_time: the end time of each task
# start_time: the start time of each task
# conflicts: conflicts[i] is the set of tasks whose time conflicts with task i
#
# returned: (x,y,obj), where
# 1. x[i,j] indicates whether task i is scheduled on processor j
# 2. y[j] indicates whether processor j is used;
# 3. obj is the maximum total bonus

``````num = length(bonus) # num of tasks

m = Model(GLPK.Optimizer)

@variable(m, x[1:num, 1:num], Bin) # whether task i is scheduled on processor j
@variable(m, y[1:num], Bin) # whether processor j is used

for i=1:num # the set partition constraint
@constraint(m, sum(x[i,j] for j=1:num) == 1)
end

for i=1:num
for j=1:num
for k in conflicts[i]
# if two tasks conflicts, they cannot be scheduled on the same task
@constraint(m, x[i, j] + x[k, j] <= 1)
end
end
end

for j=1:num # if processor j is assigned at least one task, then it is used
@constraint(m, sum(x[i,j] for i=1:num) <= num*y[j])
end

@objective(m, Min, sum(y[j] for j=1:num))
JuMP.optimize!(m)

return (JuMP.value.(x), JuMP.value.(y), JuMP.objective_value(m))
``````

end
;

Finally, it is run in

(A2_x, A2_y, A2_obj) = minNumProcessors(end_time, start_time, conflicts)
println("Minimum number of processors needed: ", A2_obj)

It is taking a very long time to run. Most of the questions here that deal with slow execution, has to do with plots. Can someone point out why it is so slow in my case ?

I am on mobile now, but there is a page in the documentation with all the “tricks” to avoid performance issues.

In yr case one for sure: avoid global variables or make them const.

2 Likes