Vectorization of nested loop

3 visualizaciones (últimos 30 días)
Bertrand Low
Bertrand Low el 15 de Jul. de 2022
Comentada: Johan Löfberg el 19 de Jul. de 2022
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 comentarios
Bertrand Low
Bertrand Low el 15 de Jul. de 2022
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.
Johan Löfberg
Johan Löfberg el 19 de Jul. de 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

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 15 de Jul. de 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 comentarios
Bertrand Low
Bertrand Low el 15 de Jul. de 2022
does not answer the problem that I am facing. I would like to avoid using for loops.
Jan
Jan el 16 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by