How can I use GlobalSearch with Parallelization?

3 次查看(过去 30 天)
Hello, I am trying the code below which uses 'parfor' and 'globalsearch' at the same time.
But I got an warning message saying 'The function RUN might make an invalid workspace access inside PARFOR loop'
I am not sure what this means. But when I run the code ignoring such message, something is running but I am a bit worried that this would give me wrong results.
So I was wondering whether this warning is something that I can just ignore or I should not use PARFOR and GLOBALSEARCH(especailly RUN) at the same time.
Thanks in advance.
parfor i_a = 1:Na %Loop over state variable a
for i_d = 1:Nd %Loop over state variable d
for i_y = 1:Ny %Loop over state variable y
for i_t = 1:Nt %Loop over state variable y
noadjamount = (1-delta)*D(i_d);
if noadjamount < D(1)
noadjamount = D(1);
end
problem1 = createOptimProblem('fmincon',...
'objective',@(x)-adjvalue_model1(x,i_a,i_d,i_y,i_t,Utility,A,D,Y,T,R,delta,fixed,Interpol_1,Na,Nd),...
'x0',[x0(i_a,i_d,i_y,i_t); x1(i_a,i_d,i_y,i_t)],'options',...
optimoptions(@fmincon,'Algorithm','sqp','Display','off'));
gs1 = GlobalSearch('Display','off');
rng(14,'twister') % for reproducibility
[adj_sol, adjval] = run(gs1,problem1);
problem2 = createOptimProblem('fmincon',...
'objective',@(x)-noadjvalue_model1(x,i_a,i_d,i_y,i_t,Utility,A,D,Y,T,R,delta,fixed,Interpol_1,Na,Nd),...
'x0',x2(i_a,i_d,i_y,i_t),'options',...
optimoptions(@fmincon,'Algorithm','sqp','Display','off'));
gs2 = GlobalSearch('Display','off');
rng(14,'twister') % for reproducibility
[noadj_sol, noadjval] = run(gs2,problem2);
[Vnew(i_a,i_d,i_y,i_t), ind] = max([-adjval, -noadjval],[],2);
% Store values needed(Policy function, Value, Adjust or not)
pol_ap(i_a,i_d,i_y,i_t) = (ind == 1).* adj_sol(1) + (ind == 2).*noadj_sol;
pol_dp(i_a,i_d,i_y,i_t) = (ind == 1).* adj_sol(2) + (ind == 2).*noadjamount;
indadj(i_a,i_d,i_y,i_t) = (ind == 1).*1;
indnadj(i_a,i_d,i_y,i_t) = (ind == 2).*1;
x0(i_a,i_d,i_y,i_t) = adj_sol(1);
x1(i_a,i_d,i_y,i_t) = adj_sol(2);
x2(i_a,i_d,i_y,i_t) = noadj_sol;
end
end
end
end

回答(1 个)

Alvaro
Alvaro 2023-1-19
Try moving that call of run to its own function.

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by