Is there a bug in the genetic algorithm (ga) under certain conditions?
3 views (last 30 days)
Show older comments
Doug Rank on 27 Feb 2021
Commented: Doug Rank on 11 Mar 2021
I discovered some abnormal behavior in the ga when I made changes.
The fitness function is evaluated far more times than I would expect when MaxGenerations is set to 10 an PopulationSize is 4. (I would expect roughly 40 or 44 calls).
Further, the output function is called many more times than 10. The generations roll back to 0 several times.
This is potentially an issue because the fitness function for my application (integer problem, binary elements only in solution, linear and nonlinear constraints, custom crossover and mutation) takes a long time to run and is the main bottleneck.
Here is a quick script that demonstrates the issue:
nVars = 3;
initialPopulation = [1,0,0;1,1,1;1,0,1;1,1,0];
A = [-1 0 0];
b = -1;
outputFunction = @OutputFunction;
fitnessFunction = @FitnessFunction;
nonlinfcn = @NonLinearConstraintFunction;
plotInterval = 1;
maxGenerations = 10;
maxTime = 60*60;
penaltyFactor = 100;
populationSize = 4;
opts = optimoptions('ga', ...
'MaxGenerations', maxGenerations, ...
'MaxTime', maxTime, ...
'PenaltyFactor', penaltyFactor, ...
'PlotInterval', plotInterval, ...
'PopulationSize', populationSize, ...
[x,fval,exitflag,output,population,scores] = ga(fitnessFunction, ...
3, A, b, , , , ...
, nonlinfcn, , opts);
function score = FitnessFunction(x)
score = x*x';
function [state, options, optchanged] = OutputFunction(options,state,flag)
optchanged = false;
gen = state.Generation;
fitnessEvals = state.FunEval;
function [c, ceq] = NonLinearConstraintFunction(x)
c = 20 - sum(x.*x);
ceq = ;
Alan Weiss on 28 Feb 2021
You have a nonlinear constraint function. This changes the algorithm quite a bit; see Nonlinear Constraint Solver Algoirithm. See also how many function evaluations occur in Nonlinear Constraints Using ga.
MATLAB mathematical toolbox documentation
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!