Vectorization of nested loop
Show older comments
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 Comments
Jan
on 15 Jul 2022
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
on 15 Jul 2022
Johan Löfberg
on 19 Jul 2022
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
Answers (1)
Jan
on 15 Jul 2022
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 Comments
Bertrand Low
on 15 Jul 2022
Bertrand Low
on 15 Jul 2022
Jan
on 16 Jul 2022
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!