Sum(sum()) with optimization variable

I want to write the following equation as a constraint
and x is an optimization variable.
I managed to get to following formulation
Constraint2 = sum(sum(x,3),1) <= ones(1,nRess)
Now the problem is, that I do not know how to change the running boundries of the inner sum, to the above given form (so not all the elements of the dimension 3, but only starting from l=t-pt_jr to t, and also while l>0).
pt_jr is a 2D matrix.
I tried to write it with sum(sum()) as I read about vectorization and didn't want to generate too long working times wir for loops.
Thanks ahead for any ideas.

1 comentario

Walter Roberson
Walter Roberson el 15 de Ag. de 2022
What if you multiply x by a logical condition ? That would zero out some locations, and that would add nothing to the sum.

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 15 de Ag. de 2022
Editada: Jan el 15 de Ag. de 2022
Constraint2 = sum(x(:, :, (t - pt(j, r)):t), [1, 3]) <= 1;

6 comentarios

Torsten
Torsten el 15 de Ag. de 2022
Editada: Torsten el 15 de Ag. de 2022
pt_jr depends on j and r ...
Further, this didn't work for the OP:
nTasks = 3;
nRess = 2;
maxPT = 50;
x = optimvar('x',nTasks,nRess,maxPT,'Type', 'integer','LowerBound',0,'UpperBound',1);
Constraint1 = sum(x,[2 3]) == ones(nTasks, 1);
Error using optim.internal.problemdef.SumOperator
Dimension argument must be a positive integer scalar within indexing range.

Error in optim.problemdef.OptimizationExpression/sum
My guess is that the above fails similarily.
I think there is no other way than looping in this case.
Jan
Jan el 15 de Ag. de 2022
Editada: Torsten el 15 de Ag. de 2022
The error message fom the sum command means, that the OP is working with an old Matlab version.
Then:
Constraint2 = squeeze(sum(sum(x(:, :, (t - pt(j, r)):t), 1), 3)) <= 1;
Andra Vartolomei
Andra Vartolomei el 15 de Ag. de 2022
I managed to solve the other question you pointed out with the approach, that I tried for my second constraint, namely sum (sum(__,dim),dim).
But yes, pt_jr is another matrix depending on j and r.
If I do not take the limitations of the second sum into consideration (pt_jr), than it works, but that is not the point sadly.
Torsten
Torsten el 15 de Ag. de 2022
The error message fom the sum command means, that the OP is working with an old Matlab version.
But it's the forum version: 2022 a.
@Jan: should I build your intel into for-loops, like this?
pt= [15 22; 8 13; 10 15];
x = optimvar('x',3,2,50,'Type', 'integer','LowerBound',0,'UpperBound',1);
for r = 1:2
for j = 1:3;
for t = 1:50
if (t - pt(j,r) >0)
Constraint2 = squeeze(sum(sum(x(:, :, (t - pt(j, r)):t), 1), 3)) <= 1;
end
end
end
end
Regarding your comment to me using an old Matlab edition with the sum(sum(...)) approach - I am using the Version 2022a :)
Jan
Jan el 18 de Ag. de 2022
@Torsten, @Andra Vartolomei: My guess was completely wrong. After:
nTasks = 3;
nRess = 2;
maxPT = 50;
x = optimvar('x',nTasks,nRess,maxPT,'Type', 'integer','LowerBound',0,'UpperBound',1);
x is not an array we can sum over. While I was talking about an array, the introduction of optimvar() was out of my view. I have no idea, what the realtion between the question and this code is.

Iniciar sesión para comentar.

Bruno Luong
Bruno Luong el 15 de Ag. de 2022
What about this:
L = reshape(1:size(x,3),1,1,[])
b = L >= t-pt_rj & L <= t;
Constraint2 = sum(sum(b.*x,3),1) <= ones(1,size(x,2))

4 comentarios

Andra Vartolomei
Andra Vartolomei el 15 de Ag. de 2022
I think there has been a confusion, pt_rj ist a matrix pt with the indices r and j, therefore a 2D matrix. So I suppose that in your approach I would than have to include a for loop, right?
Bruno Luong
Bruno Luong el 15 de Ag. de 2022
Editada: Bruno Luong el 15 de Ag. de 2022
No, the auto expansion should make it works
I assume pt_rj the same size as x(:,:,1), t is constant.
Such detail is important and should not left out when you ask question, so we won't guess.
x = optimvar('x',2,3,4);
pt_jr=randi(3,size(x,1),size(x,2))
pt_jr = 2×3
2 2 3 1 2 3
t = 2;
L = reshape(1:size(x,3),1,1,[]);
b = L >= t-pt_jr & L <= t;
Constraint2 = sum(sum(b.*x,3),1) <= ones(1,size(x,2));
show(Constraint2)
(1, 1) x(1, 1, 1) + x(2, 1, 1) + x(1, 1, 2) + x(2, 1, 2) <= 1 (1, 2) x(1, 2, 1) + x(2, 2, 1) + x(1, 2, 2) + x(2, 2, 2) <= 1 (1, 3) x(1, 3, 1) + x(2, 3, 1) + x(1, 3, 2) + x(2, 3, 2) <= 1
Andra Vartolomei
Andra Vartolomei el 18 de Ag. de 2022
Editada: Andra Vartolomei el 18 de Ag. de 2022
Thank you for your input and yes, I should have written more information but I was afraid that if it gets too long I will end up with no replies.
I tried adapting your approach, as it still does not work as I inted it to.
Here are the other requirements that I probably should have added from the start, and that need to be met:
  • t stands for time
  • x_jrt (optim. var.) has to schedule different tasks/jobs (j) to different ressources (r) at certain time points (t) along the time frame
  • In x this means, that every page (3rd dimension, t) represents a time point. At every time point/on every page a job (row) can be started by a resource (column)
  • there are two other constraints I consider:
  • (Constr. 1): that every jobs is being completed without interruption
  • (Constr. 2, this question): every resource can only work on one task at a time
  • (Cosntr. 3): tasks have to be finished if they are required for follow-up tasks
I tried to adapt your reply and didn't quite manage.
  • I need t (you set it to t=2) to be the time starting from 0 to whatever time frame is maximale needed/calculated earlier within the sum of max processing times (for example 50).
  • If a task (lets say 1) is started at t=0 and lasts 15 time units, there should be a 1 (one, number) in the 1st page (finished 1st time unit) where the resource A (column 1) took up the specific task (here 1) AND there should not be another 1 (one, number) in any of the following pages until task 1 is finished (in this case including page 15) for this resource (as it is "occupied" for the entire duration of this task, in this example 15 time units)
  • in my formulation pt_rj is the processing time that resource r requires for task j (here, 15 time units for task 1)
  • this is what I tried to formulate in Constraint 2
  • at time unit 16 the same resource could pick up the next task, as the processing time is over
As you can see, it's pretty hard to formualte the problem and I didn't want to overwhelm anyone with it, therefore I just posted the mathematical form I managed to formulate for this constraint.
Bruno Luong
Bruno Luong el 18 de Ag. de 2022
I'm lost, it sounds like you are stuck with how to transforming whatever the scheduling problem you want to solve in math formulation, and not transforming math into matlab.
If that is the case I won't be able to help you, for the simple reason is that I don't understand what you wrote in the description.

Iniciar sesión para comentar.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 15 de Ag. de 2022

Comentada:

Jan
el 18 de Ag. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by