Issues with running genetic algorithm (GA) in parallel computation

4 次查看(过去 30 天)
Hello,
opts = optimoptions(@ga, ...
'PopulationSize', 10, ...
'MaxGenerations', 50, ...
'EliteCount', 1, ...
'FunctionTolerance', 1e-5, ...
'PlotFcn', @gaplotbestf, ...
'UseParallel',true) ;
[xbestDisc, fbestDisc, exitflag, output, population, scores] = ga(@FCWithDisc,6, A, b, [], [], lb, ub, [], 1:6, opts);
Im trying to use genetic algorithm (GA) to optimize a question with 6 varuables, and use Parallel computing to improve the GA efficiency. However, at the beginning of calculation, errors appear as follows:
Error using fcnvectorizer (line 16)
The source code (D:\Program Files\MATLAB\R2021a\toolbox\globaloptim\globaloptim\private\fcnvectorizer.m) for the
parfor-loop that is trying to execute on the worker could not be found.
Error gaminlppenaltyfcn
Error gapenalty
Error makeState (line 69)
Score = FitnessFcn(state.Population(initScoreProvided+1:end,:));
Error galincon (line 22)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error gapenalty
Error gaminlp
Error ga (line 394)
[x,fval,exitFlag,output,population,scores] = gaminlp(FitnessFcn,nvars, ...
.........
I would appreciate if somebody could help me.
Yi

回答(1 个)

Aashray
Aashray 2025-4-24
Hello Yi!
The error you are seeing occurs when MATLAB's parallel workers are unable to access the fitness function file. This typically happens during the parallel execution of the Genetic Algorithm. To fix this, you can use MATLAB’s built-in addAttachedFiles function to explicitly send the function file to all parallel workers.
The key fix to apply is:
addAttachedFiles(gcp, {'FCWithDisc.m'});
Below is a modified version of your code with the fix:
% Start parallel pool (if not already running)
if isempty(gcp('nocreate'))
parpool;
end
% Attach the fitness function file to all workers
addAttachedFiles(gcp, {'FCWithDisc.m'});
% GA configuration
opts = optimoptions(@ga, ...
'PopulationSize', 10, ...
'MaxGenerations', 50, ...
'EliteCount', 1, ...
'FunctionTolerance', 1e-5, ...
'PlotFcn', @gaplotbestf, ...
'UseParallel', true);
% Example constraint and bound setup
A = []; b = [];
lb = -5 * ones(1,6);
ub = 5 * ones(1,6);
% Run the GA
[xbestDisc, fbestDisc, exitflag, output, population, scores] = ...
ga(@FCWithDisc, 6, A, b, [], [], lb, ub, [], 1:6, opts);
This fix ensures that all workers running in parallel have access to the function file FCWithDisc.m. Please make sure that the fitness function FCWithDisc.m is located in the current folder (or is on the MATLAB path), and that the filename matches exactly, including capitalization.
This should resolve the encountered error and allow GA to run using parallel computing.
You can refer to the following documentation links for more details:
  1. https://www.mathworks.com/help/parallel-computing/parallel.pool.addattachedfiles.html
  2. https://www.mathworks.com/help/gads/genetic-algorithm.html

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by