MATLAB Answers

Is there a bug in the genetic algorithm (ga) under certain conditions?

1 view (last 30 days)
Doug Rank
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', ...
'InitialPopulationMatrix', initialPopulation,...
'OutputFcn', outputFunction,...
'MaxGenerations', maxGenerations, ...
'MaxTime', maxTime, ...
'PenaltyFactor', penaltyFactor, ...
'PlotInterval', plotInterval, ...
'PopulationSize', populationSize, ...
'PlotFcn',{@gaplotbestf,@gaplotscores,@gaplotscorediversity});
[x,fval,exitflag,output,population,scores] = ga(fitnessFunction, ...
3, A, b, [], [], [], ...
[], nonlinfcn, [], opts);
disp(genArray);
disp(funEvalArray);
function score = FitnessFunction(x)
score = x*x';
end
function [state, options, optchanged] = OutputFunction(options,state,flag)
optchanged = false;
gen = state.Generation;
fitnessEvals = state.FunEval;
disp('output called')
disp(gen);
disp(fitnessEvals);
end
function [c, ceq] = NonLinearConstraintFunction(x)
c = 20 - sum(x.*x);
ceq = [];
end

Answers (1)

Alan Weiss
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.
Alan Weiss
MATLAB mathematical toolbox documentation
  5 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!