Issues with running genetic algorithm (GA) in parallel computation
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
Aashray
el 24 de Abr. de 2025
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:
0 comentarios
Ver también
Categorías
Más información sobre Genetic Algorithm en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!