MILP: how to work a for loop into a constraint

9 views (last 30 days)
I am trying to add a Constraint to my optimization (linear programming), that ensures, that a resource only works on one task at every given time (it's logical, that a resource/human cannot work on two screwing jobs at the same time).
My optimization variable is a boolean 3D array x[task, resource, time] which is 1 if at the time a task is started by the resource; 0 otherwise.
Time is considered in seconds of unit 1.
processing_time(task, ress) is a matrix with the indivudal times a ressource needs for every job there is.
I managed to build the for loops, that should be help up, but how do I combine it with/into a constraint for my optimization?
Explanation of the loops: I am looking at every processing time window for the jobs and and the sum over them are not allowed to be higher than 1 (so in the time window (processing time) of a task there is only the one (1) start of this one given task in the 3D array).
Mainly the "sum <= 1" is my Constraint.
So how do I get this to work in my optimization problem?
for ress = 1:nR
for time = 1:nPT
for task = 1:nT
if (time-processing_time(task,ress) >0)
sum (x(task,ress,(time-processing_time(task,ress)):time)<=1)
end
end
end
end
Thanks ahead!

Answers (1)

Jaynik
Jaynik on 5 Oct 2023
Hi Andra,
I understand that you are trying to add a constraint to optimize the assignment of the variable 'x'. I see that you are approaching the problem by getting the sum of 1s present in 'x'. Instead, you can create a boolean array of assigned resources assuming that the resources are numbered from 1 to nR. You can directly assign the index to "true" if the resource is available and "false" otherwise. Following is a psuedo code to perform the task.
if (time - processing_time(task, ress) > 0)
% Check if the resource is available
if resource_available(ress)
time_starting = time - processing_time(task, ress) + 1; % Calculate the starting and ending time for the task
time_ending = time;
x(task, ress, time_starting:time_ending) = 1; % Assign the task to the resource for the given time range
resource_available(ress) = false; % Update resource availability to false for the given time range
else
x(task, ress, time) = 0;
end
end
Hope this helps!

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!