Out of memory error during parameter optimization with fmincon and parallel computing
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello all,
I am currently running into a problem while optimizing some paremeters of my Simulink model with fmincon.
I have a really large Simulink model, where I need to optimize some parameters (here it is the third row of a matrix which is an input to a 2d lookup table). To speed up the whole process, I am using the parallel computing toolbox.
The optimization itself runs without problems and produces the required results, though I can never finish the optimization because I get an out of memory error. RAM usage is always around 90 % but it seems that Matlab extends the memory usage to my hard drive. If I wait long enough I can see, that all of my storage is used up and this is, when I get the 'Out of memory' - error.
To avoid this, I started to initialize all constant variables once on every worker and tried to reduce the number of logged signals in Simulink (now around 20 - could actually be just 2 - was around 150) and set the archive options to store 0 runs. This gave me a better performance in speed but also a longer time, until I got the error. Still it was not enough, to finish the optimization process.
I do not understand, why the memory usage is slowly increasing, since every worker initializes the variables in the base workspace once (so the memory should be allocated) and the results from each simulation run should only be available in the function 'calculate_error' and be deleted afterwards.
What is happening here and how can I change any settings to have my optimization finished?
Any help is highly appreciated! :)
% Assumptions:
% - Simulink-Model: 'TestMdl'
% - simulated signal: 'Signal_sim'
% - measured signal: 'Signal_meas'
% - Matrix: 'xMat'
% - third row of matrix has influence on the simulated signal
% load the model
mdl = 'TestMdl';
% load the parameter initial parameter for the optimization
x_opt_0 = xMat(3,:);
% define the goal function
objective = @(x) calculate_error(x, mdl);
% define the number of parameters to be optimized
num_parameters = size(x_opt_0); % Beispiel-Anzahl
% define the limits
lb = 0 * ones(num_parameters);
ub = 8191.875 * ones(num_parameters);
% define the options for fmincon
options = optimoptions('fmincon', ...
'UseParallel', true, ...
'MaxFunctionEvaluations', 20000, ...
'Display', 'iter');
% load the base parameters onto every parallel worker
spmd
mdl = load_system(mdl);
set_param(mdl, 'SimulationMode', 'accelerator');
set_param(mdl, 'FastRestart', 'on');
assignin('base',"sample_time",sample_time);
assignin('base',"sim_end_time",sim_end_time);
assignin('base',"sim_start_time",sim_start_time);
assignin('base',"meas_dat",meas_dat);
assignin('base',"xMat",xMat);
end
% optimize
initial_guess = x_opt_0;
[x_opt, fval] = fmincon(objective, initial_guess, [], [], [], [], lb, ub, [], options);
%% calculate the RMSE
function RMSE = calculate_error(x, mdl)
% assign the optimized variable x to the base workspace of the current worker
assignin('base', 'x', x)
% add the variable x to the matrix beeing used in the simulation
evalin('base', 'xMat(3,:) = x;');
% start the simulation
simout = sim(mdl);
% get the logged signals from the simulation
logsout = simout.logsout;
% get the simulated signal
signal1 = get(logsout, 'Signal_sim').Values.Data;
% get the measured signal
signal2 = get(logsout, 'Signal_meas').Values.Data;
% calculate the squared error
squared_errors = (signal1 - signal2).^2;
% calculate the RMSE
RMSE = sqrt(mean(squared_errors));
end
0 comentarios
Respuestas (1)
Jasvin
el 18 de Sept. de 2023
Hi Justus,
While there are many moving components involved here, especially with the parallel workers, I think a possible approach would involve adjusting the solver algorithm itself. Perhaps the one you are currently using “interior-point” which is the default algorithm is not well suited for this optimization problem.
Please check out the following guide to figure out which solver algorithm works best for you,
Hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Manual Performance Optimization 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!