Sum(sum()) with optimization variable
15 次查看(过去 30 天)
显示 更早的评论
I want to write the following equation as a constraint
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1098505/image.jpeg)
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 个)
Jan
2022-8-15
编辑:Jan
2022-8-15
Constraint2 = sum(x(:, :, (t - pt(j, r)):t), [1, 3]) <= 1;
6 个评论
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!