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 个评论

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?
Hi Jan,
I personally feel the nested for loop is sufficient. However, I would like to attempt to vectorise the code as much as possible to improve the speed.
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 个)

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 个评论

Hi Jan,
Thank you for the response.
I was wondering can the vectorisation be achieved without using any for loops? Something like the example below. The confusion I have is getting the term 'fc_idx' into the vectorisation code below.
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)];
does not answer the problem that I am facing. I would like to avoid using for loops.
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 的更多信息

产品

版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by