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에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!