Matlab GA parallel computing

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 comentarios

Star Strider
Star Strider el 28 de Nov. de 2024
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 el 1 de Dic. de 2024
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

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Nov. de 2024
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 comentarios

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?
You use
pool = gcp;
NumWorkers = pool.NumWorkers;
and then use NumWorkers for anything appropriate.
However, when you specify the UseParallel option of optimoptions() then there is no way to directly specify the pool size; it just uses the size of the current pool. The UseParallel option automatically manages the workers using parfeval() or parfor() . My guess is that it uses parfeval() so that it can take advantage of thread-based pools. https://www.mathworks.com/help/gads/how-to-use-parallel-processing.html
Walter Roberson
Walter Roberson el 28 de Nov. de 2024
Note
Subpopulations refer to a form of parallel processing for the genetic algorithm. ga currently does not support this form. In subpopulations, each worker hosts a number of individuals. These individuals are a subpopulation. The worker evolves the subpopulation independently of other workers, except when migration causes some individuals to travel between workers.
Because ga does not currently support this form of parallel processing, there is no benefit to setting PopulationSize to a vector, or to setting the MigrationDirection, MigrationInterval, or MigrationFraction options.
So, there is no point in using the migration facilities for ga()
Edric Ellis
Edric Ellis el 29 de Nov. de 2024
@Walter Roberson just an aside on your statement:
  • My guess is that it uses parfeval() so that it can take advantage of thread-based pools.
Note that thread-based pools can equally run parfor loops. I can't remember exactly which release that support was added - maybe it was there from the first release of thread pools - although it got simpler once thread pools were allowed to be returned by gcp.
Mirhan
Mirhan el 1 de Dic. de 2024
Dear @Walter Roberson, Thank you for your response. As you suggested, I renamed the tasks for different workers to ensure they wouldn’t use the same files. However, when I run my model, it seems that the workers are still interfering with each other. I’m trying to identify the cause of this issue.
In the meantime, if there’s a safer way to run MATLAB with a parallel solver while calling ANSYS for each worker without conflicts, I would be happy to implement that approach.
Here’s how I’ve implemented it:
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
Thank you for your guidance!
Best regards,
Mirhan
Mirhan el 1 de Dic. de 2024
dear @Edric Ellis, thank you for your response!
input_file = sprintf('Optimizatio_Variables%d.inp', worker_id);
Neither of your cmd includes reference to input_file so ANSYS is not going to know to refer to the file.
Walter Roberson
Walter Roberson el 1 de Dic. de 2024
The ability of parfor to run on background pools is first documented in R2024a in the Parallel Computing Toolbox documentation of parfor https://www.mathworks.com/help/releases/R2024a/parallel-computing/parfor.html . That would tend to imply that you need Parallel Computing Toolbox in order to run parfor on a background pool.
If you do not need Parallel Computing Toolbox to run parfor on a background pool, then you would expect the ability to be documented in https://www.mathworks.com/help/releases/R2024a/matlab/ref/parfor.html
Edric Ellis
Edric Ellis el 2 de Dic. de 2024
@Walter Roberson I was thinking primarily of combining parpool("Threads") with parfor, rather than backgroundPool. If you have only MATLAB (and not PCT), then even if parfor could use backgroundPool, it would be pretty useless since the backgroundPool in that case has only a single worker.
Walter Roberson
Walter Roberson el 2 de Dic. de 2024
Editada: Walter Roberson el 3 de Dic. de 2024
I think I am starting to get it. It sounds as if you are saying that parpool has been able to use parpool('threads') since that feature was introduced, but that it has only had the documented ability to use backgroundPool since R2024a.
However, I do not understand about the backgroundPool only having a single worker? I do not see anything documented in https://www.mathworks.com/help/matlab/ref/parallel.backgroundpool.html about background pools being restricted to a single worker unless you have Parallel Computing Toolbox installed. It was my understanding that background pools provided parallel execution even in the absence of the Parallel Computing Toolbox ?
Edric Ellis
Edric Ellis el 3 de Dic. de 2024
@Walter Roberson Hm, I'm not an expert on all the details of thread-based pools. It looks like there might be some documentation updates needed there to clarify things. The intention of backgroundPool is for offloading work from the main MATLAB thread, not providing parallel execution - but it shares the same primary API parfeval as the means of achieving that.
Walter Roberson
Walter Roberson el 3 de Dic. de 2024
The limitations section for backgroundPool says
Pools created using parpool(‘Threads’) and backgroundPool are both thread-based pools which utilize the same resources. It is possible that activity on one pool may block activity on the other and vice versa.
I would be rather surprised if we were to read that as implying that pure backgroundPool threads could block each other (other than at internal synchronization points.
I would thus have difficulty reading the backgroundPool documentation as implying that backgroundPool threads share a single worker with fast-switching between the various threads. I have always read the backgroundPool documentation as implying that there are multiple workers.
Edric Ellis
Edric Ellis el 3 de Dic. de 2024
@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 el 3 de Dic. de 2024
Huh, you are right about NumWorkers ! Color me surprised !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 28 de Nov. de 2024

Comentada:

el 3 de Dic. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by