How can I use GlobalSearch with Parallelization?

3 visualizzazioni (ultimi 30 giorni)
Chang seok Ma
Chang seok Ma il 17 Mag 2021
Risposto: Alvaro il 19 Gen 2023
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

Risposte (1)

Alvaro
Alvaro il 19 Gen 2023
Try moving that call of run to its own function.

Community Treasure Hunt

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

Start Hunting!

Translated by