3
3
options.FunctionTolerance.
Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: -179.987 Mean:
-78.6061, xlabel Generation, ylabel Fitness value contains 2 objects of type scatter. These objects
represent Best fitness, Mean fitness. Axes object 2 with title Stopping Criteria, xlabel Progress
contains an object of type bar.
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.484
Run ga again.
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -185.867
ga gives different results in the two runs because the state of the random number generator
changes from one run to another.
If you want to reproduce your results before you run ga, you can save the state of the random
number stream.
thestate = rng;
Run ga.
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467
Reset the stream and rerun ga. The results are identical to the previous run.
rng(thestate);
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467
If you run ga before specifying to reproduce the results, you can reset the random number
generator as long as you have the output structure.
strm = RandStream.getGlobalStream;
strm.State = Output.rngstate.State;
Rerun ga. Again, the results are identical.
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467
Modify Stopping Criteria
ga uses four different criteria to determine when to stop the solver. ga stops when it reaches the
maximum number of generations; by default, this number is 100 times the number of variables.
ga also detects if the best fitness value does not change for some time given in seconds (stall time
limit), or for some number of generations (maximum stall generations). Another criteria is the
maximum time limit in seconds. Modify the stopping criteria to increase the maximum number
of generations to 300 and the maximum stall generations to 100.
See Also
optimoptions
Related Topics
Set and Change Options
How the Genetic Algorithm Works