Vectorization of nested loop
显示 更早的评论
Hi, I am trying to do a vectorization of this nested for loop:
for jj = 1:nTasks
for ii = 1:nFc
% corresponding indices
fc_idx = jj + (ii - 1)*nTasks;
const = [const, 0 <= FC.hc_tot(fc_idx)];
const = [const, FC.hc(fc_idx) == interp1(FC.PowerData, FC.hcData, FC.P(fc_idx), 'milp','extrap')];
const = [const, implies(FC.Pflag(fc_idx) == 1, FC.hc_tot(fc_idx) == scenario.duration(jj)*FC.hc(fc_idx))];
end
end
What I was able to do this part of the vectorization:
const = [const, 0 <= FC.hc_tot];
const = [const, FC.hc == interp1(FC.PowerData, FC.hcData, FC.P, 'milp','extrap')];
% % need to glue manually to vectorize 'implies' constraint (https://groups.google.com/g/yalmip/c/zJMdJkslSPs)
temp = binvar(nFc*nTasks,1);
const = [const, implies(FC.Pflag == ones(nFc*nTasks,1), temp), implies(temp, FC.hc_tot == repmat(scenario.duration,[nFc,1]).*FC.hc)];
I am not sure how to integrate this part
fc_idx = jj + (ii - 1)*nTasks;
into the vectorization, and along with the other terms that uses fc_idx in the for loop mentioned above.
Can I get an assistance on how this can be done?
3 个评论
Jan
2022-7-15
Why do you want to vectorize the code? The bottleneck is not the loop, but the slow interp1 and the iterative growing of the result. So do you want to vectorize the code as a training or do you want to improve the speed?
Bertrand Low
2022-7-15
Johan Löfberg
2022-7-19
YALMIP specific questions much better asked at the YALMIP forums.
Note that the 'extrap' flag makes no difference. The model you get is just a pwa model between the data-points
回答(1 个)
Jan
2022-7-15
It is hard to improve the speed of code without having data to run the code. But start with calling interp1 once only.
Q = interp1(FC.PowerData, FC.hcData, FC.P, 'milp','extrap');
C = cell(nFC, nTasks);
for jj = 1:nTasks
for ii = 1:nFc
% corresponding indices
fc_idx = jj + (ii - 1)*nTasks;
C{ii, jj} = [0 <= FC.hc_tot(fc_idx),
FC.hc(fc_idx) == Q(fc_idx), ...
implies(FC.Pflag(fc_idx) == 1, ...
FC.hc_tot(fc_idx) == scenario.duration(jj)*FC.hc(fc_idx))];
end
end
const = cell2vec(C); % See: https://www.mathworks.com/matlabcentral/fileexchange/28916-cell2vec
3 个评论
Bertrand Low
2022-7-15
Bertrand Low
2022-7-15
Jan
2022-7-16
You expect that the vectorization improves the speed. I do not expect this. An optimization should start at the bottlenecks of the code and this is the interpolation, not the loop - at least this is my assumption. I cannot check this because you do not provide some input data. Therefore I cannot check my idea to vectorize the code also and I will not post completely untested code. This could be more confusing than useful.
There is no reason for an apology. If you want a solution, post some input data.
If you want to increase the speed, you could try my suggestion and post the results of a speed comparison.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!