Matlab GA parallel computing
25 次查看(过去 30 天)
显示 更早的评论
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:
- 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?
- 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?
- 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 个评论
Star Strider
2024-11-28
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.
回答(1 个)
Walter Roberson
2024-11-28
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:
- 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
- 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 个评论
Edric Ellis
2024-12-3
@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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!