GA stops with different score every time, even with setting big MaxGeneations and MaxStallGenerations
Show older comments
I am using the GA() solver to solve two-variable non-linear problems. Every time I run the code, I get a different fitness value and different variable values. The following message appears: “GA has been stopped because the average change in fitness is less than options.FunctionTolerance”. I understand that MATLAB uses a random number generator and the values may vary slightly, but in my case the values found for the variables are up to twice as high! So this does not seem optimal. I set MaxStallGenerations and MaxGeneration to 1000, but this doesn’t make much difference. The difference in the fitness function is around 100 (which is about 1% of the fitness function value).
Do you have any ideas on what I can set or change to get more reproducible results?
Answers (3)
John D'Errico
on 18 Mar 2026
Edited: John D'Errico
on 18 Mar 2026
1 vote
Nonlinear solvers, of which GA is one, cannot be forced to ALWAYS return the optimal result. And while tools like GA are designed to be considerably more robust in some respects, they are still subject to the same issues. Sadly, there is no magic knob you can turn to improve things.
For example, nonlinear problems can be ill-posed, with the result the solution may lie along a long curved valley, any point of which is equally valid as a solution or nearly so, and once the solver lands in the valley, it cannot find a new point on that valley which is an improvement within tolerances.
As well, nonlinear problems may have many solutions, and the solver need not always land on the one you want. Again, GA is not immune to this.
Without actually having your specific problem at hand, we cannot learn which of these issues (or perhaps something else) is the problem. Note that 2 variable problems are often fairly easy to diagnose, because we can plot the objective surface. But we cannot see inside your computer, and this makes diagnosis far more difficult.
Star Strider
on 18 Mar 2026
1 vote
The results of genetic algorithm (ga) optimisation improve with the initial number of individuals ('PopulationSize') and the number of generations ('MaxGenerations') permitted. I usually begin with values of 500 and 2000 respectively, however this depends on the number of parameters and the specific problem. The consistence of the results depends on the problem being optimised.
The ga function is usually very good at finding the global minimum, however it may require more than one run with different starting values to find it. Once it appears to have converged successfully on the global minimum, I generally use lsqcurvefit to use gradient descent to optimise the parameters, using the ga results as the initial parameter estimates.
There is no 'magic' to this -- it simply requires some eexperimentation.
3 Comments
I agree with @Star Strider. If increasing MaxGenerations is not helping, it probably means the surface of the fitness function is fraught with capture basins which are not being exhaustively explored. Increasing the PopulationSize is probably the thing to do.
Star Strider
on 18 Mar 2026
@Matt J -- Thank you!
Star Strider
on 19 Mar 2026
@Magdalena -- With respect to your answer, I would not use the most frequently occurring result.
Instead, choose the result with the best fitness value, after increasing the 'PopulationSize' appropriately to increase the probability of finding the global optimum parameter set.
Then, use lsqcurvefit or a similar function that cna accommodate all the inputs, outputs, and constraints in your problem to fine-tune the parameter estimates, using the ga parameters as the initial parameter estimates.
Magdalena
on 19 Mar 2026
0 votes
Categories
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!