Set Maximum Number of Generations

The Generations option in Stopping criteria determines the maximum number of generations the genetic algorithm runs for—see Stopping Conditions for the Algorithm. Increasing the Generations option often improves the final result.

As an example, change the settings in the Optimization app as follows:

  • Set Fitness function to @rastriginsfcn.

  • Set Number of variables to 10.

  • Select Best fitness in the Plot functions pane.

  • Set Generations to Inf.

  • Set Stall generations to Inf.

  • Set Stall time limit to Inf.

Run the genetic algorithm for approximately 300 generations and click Stop. The following figure shows the resulting best fitness plot after 300 generations.

Note that the algorithm stalls at approximately generation number 170—that is, there is no immediate improvement in the fitness function after generation 170. If you restore Stall generations to its default value of 50, the algorithm could terminate at approximately generation number 220. If the genetic algorithm stalls repeatedly with the current setting for Generations, you can try increasing both the Generations and Stall generations options to improve your results. However, changing other options might be more effective.

The command-line options relating to Generations and Stall generations are MaxGenerations and MaxStallGenerations, respectively. To run this example at the command line:

options = optimoptions('ga','MaxGenerations',300,...
x = ga(@rastriginsfcn,10,[],[],[],[],[],[],[],options);


When Mutation function is set to Gaussian, increasing the value of Generations might actually worsen the final result. This can occur because the Gaussian mutation function decreases the average amount of mutation in each generation by a factor that depends on the value specified in Generations. Consequently, the setting for Generations affects the behavior of the algorithm.

Related Topics