Matlab GA parallel computing

41 vues (au cours des 30 derniers jours)
Mirhan
Mirhan le 28 Nov 2024 à 15:12
Commenté : Walter Roberson le 3 Déc 2024 à 10:08
Hi everyone,
I am performing an optimization analysis using MATLAB's Genetic Algorithm (GA) to select design variables, which are then passed to ANSYS to calculate some structural properties. My goal is to find the global optimum, so I have set the population size to 100 × number of variables. While this ensures a broader search space and reduces the risk of getting stuck in local optima, it significantly increases the convergence time because finite element analysis (FEA) needs to be performed for each population member. The current setup takes about a week (or more) to converge, which is not feasible.
To address this, I plan to implement parallel computing for the GA. I need help with the following aspects:
  1. Parallel Implementation:On my local desktop, I have Number of workers: 6, which means I can evaluate 6 members of the population simultaneously. However, I want to make my code generic so that it automatically adjusts to the number of workers available on any machine. How can I achieve this in MATLAB?
  2. Improving Convergence Speed:Another approach I’ve come across is using the MigrationInterval and MigrationFractionoptions to divide the population into smaller "islands" that exchange solutions periodically. Would this approach be suitable in my case, and how can I implement it effectively?
  3. Objective Function Parallelization:Below is the current version of my objective function, which works without parallelization. It writes input variables to an ANSYS .inp file, runs the simulation, and reads results from a text file. How should I modify this function (if needed) to make it compatible with MATLAB’s parallel computing features?
function cost = simple_objective(x, L)
% Write input variables to the file
fid = fopen('Design_Variables.inp', 'w+'); % Ansys APDL reads this
fprintf(fid, 'H_head = %f\n', x(1));
fprintf(fid, 'R_top = %f\n', x(2));
fclose(fid);
% Set ANSYS and Run model
ansys_input = 'ANSYS_APDL.dat';
output_file = 'out_file.txt';
cmd = sprintf('SET KMP_STACKSIZE=15000k & "C:\\Program Files\\ANSYS Inc\\v232\\ansys\\bin\\winx64\\ANSYS232.exe" -b -i %s -o %s', ansys_input, output_file);
system(cmd);
% Remove file lock if it exists
if exist('file.lock', 'file')
delete('file.lock');
end
% Read results
fileID = fopen('PC1.txt', 'r');
load_multip = fscanf(fileID,'%f',[1,inf]);
fclose(fileID);
if load_multip == 0 % nondesignable geometry
cost = 1e9; % penalty
else
cost = -load_multip;
end
end
GA Options:
Here are the GA options I am currently using. Do I need to adjust these for parallelization or migration-based approaches?
options = optimoptions("ga", ...
'OutputFcn',@SaveOut, ...
'Display', 'iter', ...
'PopulationSize', 200, ...
'Generations', 50, ...
'EliteCount', 2, ...
'CrossoverFraction', 0.98, ...
'TolFun', 1.0e-9, ...
'TolCon', 1.0e-9, ...
'StallTimeLimit', Inf, ...
'FitnessLimit', -Inf, ...
'PlotFcn',{@gaplotbestf,@gaplotstopping});
I would greatly appreciate it if you could guide me on:
  • How to enable and configure parallel computing for GA in MATLAB.
  • Any adjustments required in the objective function for parallel execution.
  • Whether migration-based GA (using MigrationInterval and MigrationFraction) is a good alternative in my case.
Thank you for your time and help!
  2 commentaires
Star Strider
Star Strider le 28 Nov 2024 à 21:19
With respect to parallel processing, see the options secion of the ga documentation, specifically the 'UseParallel' option, that refers to Vectorize and Parallel Options (User Function Evaluation) and How to Use Parallel Processing in Global Optimization Toolbox. According to the documentation (at least as I read it). it requires the Parallel Computing Toolbox.
Mirhan
Mirhan le 1 Déc 2024 à 19:45
dear @Star Strider, Thank you for the document and your response. I’ve reviewed them and now have a better understanding of how the process works.
While my code appears to be functioning, not all the workers are able to call ANSYS and read the results correctly. Could you please take a look to see if there’s an issue in my code?
Thank you for your support!
Best regards,
function cost = simple_objective(x)
% Get a unique identifier for the current worker
if isempty(getCurrentTask())
worker_id = 0; % Running without parallel pool
else
task = getCurrentTask(); % Store the task
worker_id = task.ID; % Access the ID property safely
end
% Create unique file names for this worker
input_file = sprintf('Optimizatio_Variables%d.inp', worker_id);
ansys_output_file = sprintf('ANSYS_Output_Worker%d.txt', worker_id);
ansys_cmd_file = sprintf('beam_ansys%d.dat', worker_id);
% Write input variables to the file
fid = fopen(input_file, 'w+');
fprintf(fid, 'a = %f\n', x(1));
fprintf(fid, 'b = %f\n', x(2));
fclose(fid);
if ispc %check if it is windows (local) or linux (HPC)
% Windows command
cmd = sprintf('SET KMP_STACKSIZE=15000k & "C:\\Program Files\\ANSYS Inc\\v212\\ansys\\bin\\winx64\\ANSYS212.exe" -b -i %s -o %s', ansys_cmd_file, ansys_output_file);
else
% Linux/HPC command
cmd = sprintf('export KMP_STACKSIZE=15000k; ansys232 -b -i %s -o %s 2>/dev/null', ansys_cmd_file, ansys_output_file);
end
% Run the command
system(cmd);
lock_file = sprintf('file_Worker%d.lock', worker_id);
if exist(lock_file, 'file')
delete(lock_file);
end
result_file = sprintf('PC1_%d.txt', worker_id);
fileID = fopen(result_file, 'r');
if fileID == -1
cost = 1e9; % Assign penalty if the file doesn't exist
return;
end
load_multip = fscanf(fileID, '%f', [1, inf]);
fclose(fileID);
cost = -load_multip
end

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 28 Nov 2024 à 21:22
fid = fopen('Design_Variables.inp', 'w+'); % Ansys APDL reads this
%...
ansys_input = 'ANSYS_APDL.dat';
output_file = 'out_file.txt';
cmd = sprintf('SET KMP_STACKSIZE=15000k & "C:\\Program Files\\ANSYS Inc\\v232\\ansys\\bin\\winx64\\ANSYS232.exe" -b -i %s -o %s', ansys_input, output_file);
%...
fileID = fopen('PC1.txt', 'r');
I checked documentation for command line switches for ANSYS. There is no apparent way to configure the name of the design variables file or the name of the output PC1.txt . There is a significant risk that if you run multiple processes all trying to invoke ANSYS at the same time, that the different files of the different processes would interfere with each other.
There are two possibilities here:
  1. It is possible that those file names are configured inside ANSYS_APDL.dat . If so then you can write a new version of that file for each process, specifying unique file names; Or
  2. You could create a different directory for each run, and use the same file names as you already have, except inside the different directory. Since the files will be in different directories, they should not clash. You might need to copy ANSYS_APDL.dat into the directory.
  13 commentaires
Edric Ellis
Edric Ellis le 3 Déc 2024 à 9:25
@Walter Roberson I hadn't spotted it, but it has been pointed out to me that the NumWorkers property reference describes backgroundPool as having only a single worker unless you have Parallel Computing Toolbox. https://uk.mathworks.com/help/matlab/ref/parallel.backgroundpool.html#mw_1cf17afa-5c49-4668-bc3c-9f291eadb729 . (And, of course, you cannot have parpool("Threads") without a PCT licence)
Walter Roberson
Walter Roberson le 3 Déc 2024 à 10:08
Huh, you are right about NumWorkers ! Color me surprised !

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by