Run Nested FOR-loop Parallelly for Multivariable Function Optimization
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joshua Roy Palathinkal
el 1 de Dic. de 2021
F_max = 0; % Temp var for max of F
F_curr = 0; % Temp var for current F
for x = -0.02:0.001:0.02
for x_1 = 20:100
for x_2 = 20:100
for x_3 = 20:100
for x_4 = 20:100
for x_5 = -15:15
for x_6 = -15:15
for x_7 = -15:15
for x_8 = -15:15
F_curr = double(F(x,x_1,x_2,x_3,x_4,x_5,x_6,x_7,x_8));
if F_curr>F_max
F_max = F_curr;
end
end
end
end
end
end
end
end
end
end
F is a (symbolic) function of 9 (symbolic) variables x, x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8. The formula of F is given in the following Link1.
How do I edit this code so that it can run parallelly? The reason I can't apply the usual solution is because the variables aren't looping by integer iterations starting from 0 (e.g. i = 1:n (some integer)).
1 comentario
Sam Marshalik
el 17 de Dic. de 2021
Hey Joshua, I do not currently have an opportunity to play around with the code, but you will want to employ parfor to speed this up. The issue is that F_max is a temporary variable on all of the workers and they will not be able to exchange information to determine who has the highest F_max (parfor workers are not able to communicate with one another).
I think something you can try is making F_max a sliced output variable (https://www.mathworks.com/help/parallel-computing/sliced-variable.html#bq_tiga) - this will give you a large array with all of the values from your loops. You can then determine the highest value (max(F_max)) from the entire list.
You can also do the following to deal with the outermost parfor-loop, since the non-integers will be an issue:
x = 0:0.1:1;
parfor idx = 1:length(x)
disp(x(idx))
end
Respuesta aceptada
Matt J
el 18 de Dic. de 2021
Editada: Matt J
el 18 de Dic. de 2021
I don't think a loop over all 9 variables is going to be practical (10^15 combinations).
An important observation, though, is that your function F() is linear with respect to x5,x6,x7,x8,x9. This means that the maximum will be achieved at one of the extreme values of these variables. Consequently, you don't have to search -15:15. You only have to search the two end points -15 and 15 for each of these variables for a total of 16 combinations. For each of the 16 combinations, you need to do a grid search over X, X_1,X_2,X_3,X_4 but the dimension of that search (EDIT:) can be done with increased vectorization as follows.
Xgrid ={ -0.02:.001:0.02;
[-15,15]};
Xgrid= Xgrid([1,2,2,2,2]);
sizeT=repelem(numel(20:100),1,4);
[X_1,X_2,X_3,X_4]=ndgridVecs(20:100); % ndgridVecs available at https://www.mathworks.com/matlabcentral/fileexchange/74956-ndgridvecs
[X_0, X_5,X_6,X_7,X_8]=ndgrid(Xgrid{:});
Fdouble=@(x1,x2,x3,x4,x5,x6,x7,x8,x9) -(x1.^2+x2.^2+x3.^2+x4.^2+x5.^2+x6.^2+x7.^2+x8.^2+x9.^2); %example function
%Fdouble=matlabFunction(F); %true function
N=numel(X_0);
F_max=nan(1,N);
loc=cell(1,N);
tic
parfor n=1:numel(X_0)
[x_0, x_5,x_6,x_7,x_8] = deal(X_0(n), X_5(n),X_6(n),X_7(n), X_8(n));
T=Fdouble(x_0,X_1,X_2,X_3,X_4, x_5,x_6,x_7,x_8);
[F_max(n),I]=max(T,[],'all','linear');
loc{n}=[x_0,I, x_5,x_6,x_7,x_8]; %location of maximum
end
[F_max,nmax]=max(F_max);
loc=loc{nmax};
[j,k,l,m]=ind2sub(sizeT,loc(2));
loc=[loc(1), X_1(j),X_2(k),X_3(l),X_4(m) ,loc(3:end)];
toc%Elapsed time is 46.687547 seconds.
3 comentarios
Matt J
el 18 de Dic. de 2021
Editada: Matt J
el 18 de Dic. de 2021
The reason for stating that is because, the coefficients of [x5,x6,x7,x8] in the function is [-1,1,1,-1]
The coefficients that I see are complicated functions of x,x1..x4. I don't see how you can anticipate their signs.
I've modified my answer above, however, and tested that it runs with more modest memory consumption.
Matt J
el 18 de Dic. de 2021
Editada: Matt J
el 18 de Dic. de 2021
And coefficient of 1st fraction is +1
No, it isn't. The coefficient of x7 in the first fraction is,
which means that if is negative at the optimum, then the whole coefficeint will be negative and the value of x7 that will maximize the function is -15, not +15.
Más respuestas (1)
Matt J
el 17 de Dic. de 2021
Editada: Matt J
el 17 de Dic. de 2021
Xgrid ={ -0.02:0.001:0.02;
20:100;
20:100;
20:100;
20:100;
-15:15;
-15:15;
-15:15;
-15:15};
sz=cellfun(@numel,Xgrid);
N=prod(sz);
J=numel(sz);
Fdouble=matlabFunction(F);
F_max=-inf;
parfor n=1:N
sub=cell(J,1);
[sub{1:J}]=ind2sub(sz,n); %convert to subscripts
X=cellfun(@(A,B) A(B), Xgrid,sub,'uni',0); %lookup grid values
F_max=max(F_max, Fdouble(X{:}) ); %reduction
end
3 comentarios
Joshua Roy Palathinkal
el 17 de Dic. de 2021
Editada: Joshua Roy Palathinkal
el 18 de Dic. de 2021
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!