0% found this document useful (0 votes)
16 views5 pages

3

The document describes the execution and results of a genetic algorithm (ga) in MATLAB, detailing the stopping criteria and variations in results due to random number generation. It demonstrates how to reproduce results by saving and resetting the random number generator state, as well as modifying stopping criteria and specifying ga operators. The final results show variations in best function values based on different configurations and operators used in the ga solver.

Uploaded by

milad.gholamifar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

3

The document describes the execution and results of a genetic algorithm (ga) in MATLAB, detailing the stopping criteria and variations in results due to random number generation. It demonstrates how to reproduce results by saving and resetting the random number generator state, as well as modifying stopping criteria and specifying ga operators. The final results show variations in best function values based on different configurations and operators used in the ga solver.

Uploaded by

milad.gholamifar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ga stopped because the average change in the fitness value is less than

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.

fprintf('The number of generations is: %d\n', Output.generations);


The number of generations is: 67
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 614
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -179.987
Reproduce Results
By default, ga starts with a random initial population created using MATLAB® random number
generators. The solver produces the next generation using ga operators that also use these same
random number generators. Every time a random number is generated, the state of the random
number generators changes. So, even if you do not change any options, you can get different
results when you run the solver again.

Run the solver twice to show this phenomenon.

Run the ga solver.

[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.

opts = optimoptions(opts,'MaxGenerations',300,'MaxStallGenerations', 100);


Rerun the ga solver.

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...


[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: -186.729 Mean:
-186.202, 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.

fprintf('The number of generations is: %d\n', Output.generations);


The number of generations is: 299
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 2702
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.729
Specify ga Operators
ga starts with a random set of points in the population and uses operators to produce the next
generation of the population. The different operators are scaling, selection, crossover, and
mutation. The toolbox provides several functions to specify for each operator. Specify
fitscalingprop for FitnessScalingFcn and selectiontournament for SelectionFcn.

opts = optimoptions(@ga,'SelectionFcn',@selectiontournament, ...


'FitnessScalingFcn',@fitscalingprop);
Rerun ga.

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...


[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than
options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 52
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 2497
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.417
The best function value can improve or get worse based on the specified operators.
Experimenting with different operators is often the best way to determine which set of operators
works best for your problem.

See Also
optimoptions
Related Topics
Set and Change Options
How the Genetic Algorithm Works

You might also like