The genetic algorithm applies mutations using the option that
you specify on the **Mutation function** pane. The
default mutation option, `Gaussian`

, adds a random
number, or *mutation*, chosen from a Gaussian distribution,
to each entry of the parent vector. Typically, the amount of mutation,
which is proportional to the standard deviation of the distribution,
decreases at each new generation. You can control the average amount
of mutation that the algorithm applies to a parent in each generation
through the **Scale** and **Shrink** options:

**Scale**controls the standard deviation of the mutation at the first generation, which is**Scale**multiplied by the range of the initial population, which you specify by the**Initial range**option.**Shrink**controls the rate at which the average amount of mutation decreases. The standard deviation decreases linearly so that its final value equals 1 –**Shrink**times its initial value at the first generation. For example, if**Shrink**has the default value of`1`

, then the amount of mutation decreases to 0 at the final step.

You can see the effect of mutation by selecting the plot options **Distance** and **Range**,
and then running the genetic algorithm on a problem such as the one
described in Minimize Rastrigin's Function.
The following figure shows the plot after setting the random number
generator.

rng default % for reproducibility options = optimoptions('ga','PlotFcn',{@gaplotdistance,@gaplotrange},... 'MaxStallGenerations',200); % to get a long run [x,fval] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options);

The upper plot displays the average distance between points in each generation. As the amount of mutation decreases, so does the average distance between individuals, which is approximately 0 at the final generation. The lower plot displays a vertical line at each generation, showing the range from the smallest to the largest fitness value, as well as mean fitness value. As the amount of mutation decreases, so does the range. These plots show that reducing the amount of mutation decreases the diversity of subsequent generations.

For comparison, the following figure shows the plots for **Distance** and **Range** when
you set **Shrink** to `0.5`

.

options = optimoptions('ga',options,'MutationFcn',{@mutationgaussian,1,.5}); [x,fval] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options);

With **Shrink** set to `0.5`

,
the average amount of mutation decreases by a factor of 1/2 by the
final generation. As a result, the average distance between individuals
decreases less than before.

The **Crossover fraction** field, in the **Reproduction** options,
specifies the fraction of each population, other than elite children,
that are made up of crossover children. A crossover fraction of `1`

means
that all children other than elite individuals are crossover children,
while a crossover fraction of `0`

means that all
children are mutation children. The following example show that neither
of these extremes is an effective strategy for optimizing a function.

The example uses the fitness function whose value at a point is the sum of the absolute values of the coordinates at the points. That is,

$$f\left({x}_{1},{x}_{2},\mathrm{...},{x}_{n}\right)=\left|{x}_{1}\right|+\left|{x}_{2}\right|+\cdots +\left|{x}_{n}\right|.$$

You can define this function as an anonymous function by setting **Fitness
function** to

@(x) sum(abs(x))

To run the example,

Set

**Fitness function**to`@(x) sum(abs(x))`

.Set

**Number of variables**to`10`

.Set

**Initial range**to`[-1; 1]`

.Select

**Best fitness**and**Distance**in the**Plot functions**pane.

Run the example with the default value of `0.8`

for **Crossover
fraction**, in the **Options > Reproduction** pane.
For reproducibility, switch to the command line and enter

`rng(14,'twister')`

Switch back to Optimization app, and click **Run solver
and view results > Start**. This returns the best fitness
value of approximately `0.0799`

and displays the
following plots.

To see how the genetic algorithm performs when there is no mutation,
set **Crossover fraction** to `1.0`

and
click **Start**. This returns the best fitness
value of approximately `.66`

and displays the following
plots.

In this case, the algorithm selects genes from the individuals
in the initial population and recombines them. The algorithm cannot
create any new genes because there is no mutation. The algorithm generates
the best individual that it can using these genes at generation number
8, where the best fitness plot becomes level. After this, it creates
new copies of the best individual, which are then are selected for
the next generation. By generation number 17, all individuals in the
population are the same, namely, the best individual. When this occurs,
the average distance between individuals is 0. Since the algorithm
cannot improve the best fitness value after generation 8, it stalls
after 50 more generations, because **Stall generations** is
set to `50`

.

To see how the genetic algorithm performs when there is no crossover,
set **Crossover fraction** to `0`

and
click **Start**. This returns the best fitness
value of approximately `3`

and displays the following
plots.

In this case, the random changes that the algorithm applies never improve the fitness value of the best individual at the first generation. While it improves the individual genes of other individuals, as you can see in the upper plot by the decrease in the mean value of the fitness function, these improved genes are never combined with the genes of the best individual because there is no crossover. As a result, the best fitness plot is level and the algorithm stalls at generation number 50.

The example `deterministicstudy.m`

, which is
included in the software, compares the results of applying the genetic
algorithm to Rastrigin's function with **Crossover fraction** set
to `0`

, `.2`

, `.4`

, `.6`

, `.8`

,
and `1`

. The example runs for 10 generations. At
each generation, the example plots the means and standard deviations
of the best fitness values in all the preceding generations, for each
value of the **Crossover fraction**.

To run the example, enter

deterministicstudy

at the MATLAB^{®} prompt. When the example is finished, the
plots appear as in the following figure.

The lower plot shows the means and standard deviations of the best fitness values over 10 generations, for each of the values of the crossover fraction. The upper plot shows a color-coded display of the best fitness values in each generation.

For this fitness function, setting **Crossover fraction** to `0.8`

yields
the best result. However, for another fitness function, a different
setting for **Crossover fraction** might yield the
best result.