Is there a bug in the genetic algorithm (ga) under certain conditions?
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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
Alan Weiss
on 10 Mar 2021
In my opinion, there is very little reason to use ga as a solver except to handle integer constraints for nonlinear problems. For your problem, which you say has nonlinear and integer constraints, it might be worthwhile to use ga, though. Your custom mutation, crossover, and creation functions might be a good argument for ga in your application.
Maybe I didn't answer your question. My opinion is that ga is vastly overused, and most problems can be better handled differently. But for your case, knowing no problem details, I cannot say; maybe ga is the most appropriate solver.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!