Parfor loop with complex structure
7 views (last 30 days)
Show older comments
My idea is to run multiple independent simulations only changing parameters, but I want to keep all the results stored in 1 unique workspace that is also loaded so when I stop and rerun the code it checks from which iteration it should continue.
For this purpose I have 3 nested loops, the inner one being a parfor loop and the outer ones a for loop. The problem is that for all this data to be organized I want to dynamically create sublevels on the workspace structure named after the iteration indexes and I want to store 12 different variables with such different dimensions and types.
I tried to index the first level of the output structure as in examples, also creating an empty structure inside the loop but it does not work. Also, it seems save() cannot be executed inside a parfor loop.
Is there some workaround or method to accomplish my idea?
version_of_mpc='v5_2';
for days_horizon=1:7
days_horizon
for minutes_samplingtime=3:6
minutes_samplingtime
parfor cost_of_benefit_index=1:30
cost_of_benefit=cost_of_benefit_index*10;
outputfile_name1=['results_Hp' num2str(days_horizon,'%.2f') 'days_Ts' num2str(minutes_samplingtime,'%.2f') 'min_weightCost' num2str(cost_of_benefit,'%.2f') '_FEAS.fig'];
outputfile_name2=['results_Hp' num2str(days_horizon,'%.2f') 'days_Ts' num2str(minutes_samplingtime,'%.2f') 'min_weightCost' num2str(cost_of_benefit,'%.2f') '_INFEAS.fig'];
outputfile_name3='results.mat';
full_name1=fullfile(['figures directory...' version_of_mpc],outputfile_name1);
full_name2=fullfile(['same figures directory...' version_of_mpc],outputfile_name2);
full_name3=fullfile(['Workspace output file directory...' version_of_mpc],outputfile_name3);
cost_of_benefit
if not (isfile(full_name1) || isfile(full_name2)) % checking what simulations are done
[Xhist, Uhist_corrected, Uhist_controller, cost_prod_hist, pgrid_hist, i, percent, irradiation_plt, P_pv_plt, Price_energy_plt, CPU_time, total_simulation_time]=mpc_sim(minutes_samplingtime,days_horizon,cost_of_benefit);
plot_and_save_v4(Xhist, Uhist_corrected, Uhist_controller, cost_prod_hist, pgrid_hist, i, percent, irradiation_plt, P_pv_plt, Price_energy_plt, CPU_time, total_simulation_time,minutes_samplingtime,days_horizon,cost_of_benefit,version_of_mpc,full_name3);
first_lvl_name='FEAS'
if percent < 100
first_lvl_name='INFEAS'
end
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Xhist')=Xhist;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Uhist_corrected')=Uhist_corrected;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Uhist_controller')=Uhist_controller;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('cost_prod_hist')=cost_prod_hist;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('pgrid_hist')=pgrid_hist;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('i')=i;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('percent')=percent;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('irradiation_plt')=irradiation_plt;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('P_pv_plt')=P_pv_plt;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Price_energy_plt')=Price_energy_plt;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('CPU_time')=CPU_time;
results.(version_of_mpc).('ALL').(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('total_simulation_time')=total_simulation_time;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Xhist')=Xhist;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Uhist_corrected')=Uhist_corrected;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Uhist_controller')=Uhist_controller;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('cost_prod_hist')=cost_prod_hist;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('pgrid_hist')=pgrid_hist;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('i')=i;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('percent')=percent;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('irradiation_plt')=irradiation_plt;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('P_pv_plt')=P_pv_plt;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('Price_energy_plt')=Price_energy_plt;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('CPU_time')=CPU_time;
results.(version_of_mpc).(first_lvl_name).(['Hp_' num2str(days_horizon,'%.0f') 'days']).(['Ts_' num2str(minutes_samplingtime,'%.0f') 'min']).(['weightCost_' num2str(cost_of_benefit,'%.0f')]).('total_simulation_time')=total_simulation_time;
% i don't save any .mat file, i don't know how.
% i even tried evalin inside of the plot_and_save function in
% wich saving the figure works, but neither the creation of the
% structure nor saving the .mat file worked
end
end
end
end
%system('shutdown -s');
7 Comments
Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!