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
Walter Roberson
2022년 8월 15일
What if you multiply x by a logical condition ? That would zero out some locations, and that would add nothing to the sum.
답변 (2개)
Constraint2 = sum(x(:, :, (t - pt(j, r)):t), [1, 3]) <= 1;
댓글 수: 6
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);
My guess is that the above fails similarily.
I think there is no other way than looping in this case.
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
2022년 8월 15일
Torsten
2022년 8월 15일
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.
Andra Vartolomei
2022년 8월 15일
Jan
2022년 8월 18일
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.
Bruno Luong
2022년 8월 15일
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
Andra Vartolomei
2022년 8월 15일
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))
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)
Andra Vartolomei
2022년 8월 18일
편집: Andra Vartolomei
2022년 8월 18일
Bruno Luong
2022년 8월 18일
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.
카테고리
도움말 센터 및 File Exchange에서 Choose a Solver에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!