This section presents an example that shows how to find the minimum of Rastrigin's function, a function that is often used to test the genetic algorithm.

For two independent variables, Rastrigin's function is defined as

$$Ras(x)=20+{x}_{1}^{2}+{x}_{2}^{2}-10\left(\mathrm{cos}2\pi {x}_{1}+\mathrm{cos}2\pi {x}_{2}\right).$$

Global
Optimization Toolbox software contains
the `rastriginsfcn.m`

file, which computes the values
of Rastrigin's function. The following figure shows a plot of Rastrigin's
function.

As the plot shows, Rastrigin's function has many local minima—the
“valleys” in the plot. However, the function has just
one global minimum, which occurs at the point [0 0] in the *x-y* plane,
as indicated by the vertical line in the plot, where the value of
the function is 0. At any local minimum other than [0 0], the value
of Rastrigin's function is greater than 0. The farther the local minimum
is from the origin, the larger the value of the function is at that
point.

Rastrigin's function is often used to test the genetic algorithm, because its many local minima make it difficult for standard, gradient-based methods to find the global minimum.

The following contour plot of Rastrigin's function shows the alternating maxima and minima.

This section explains how to find the minimum of Rastrigin's function using the genetic algorithm.

Because the genetic algorithm uses random number generators, the algorithm returns slightly different results each time you run it.

To find the minimum, do the following steps:

Enter

`optimtool('ga')`

at the command line to open the Optimization app.Enter the following in the Optimization app:

In the

**Fitness function**field, enter`@rastriginsfcn`

.In the

**Number of variables**field, enter`2`

, the number of independent variables for Rastrigin's function.The

**Fitness function**and**Number of variables**fields should appear as shown in the following figure.

Click the

**Start**button in the**Run solver and view results**pane, as shown in the following figure.While the algorithm is running, the

**Current iteration**field displays the number of the current generation. You can temporarily pause the algorithm by clicking the**Pause**button. When you do so, the button name changes to**Resume**. To resume the algorithm from the point at which you paused it, click**Resume**.When the algorithm is finished, the

**Run solver and view results**pane appears as shown in the following figure. Your numerical results might differ from those in the figure, since`ga`

is stochastic.The display shows:

The final value of the fitness function when the algorithm terminated:

Objective function value: 5.550533778020394E-4

Note that the value shown is very close to the actual minimum value of Rastrigin's function, which is

`0`

. Setting the Initial Range, Setting the Amount of Mutation, and Set Maximum Number of Generations describe some ways to get a result that is closer to the actual minimum.The reason the algorithm terminated.

Optimization terminated: maximum number of generations exceeded.

The final point, which in this example is

`[-0.002 -0.001]`

.

To find the minimum of Rastrigin's function from the command line, enter

rng(1,'twister') % for reproducibility [x,fval,exitflag] = ga(@rastriginsfcn,2)

This returns

Optimization terminated: average change in the fitness value less than options.FunctionTolerance. x = -1.0421 -1.0018 fval = 2.4385 exitflag = 1

`x`

is the final point returned by the algorithm.`fval`

is the fitness function value at the final point.`exitflag`

is integer value corresponding to the reason that the algorithm terminated.

Because the genetic algorithm uses random number generators, the algorithm returns slightly different results each time you run it.

The Optimization app **Plot functions** pane
enables you to display various plots that provide information about
the genetic algorithm while it is running. This information can help
you change options to improve the performance of the algorithm. For
example, to plot the best and mean values of the fitness function
at each generation, select the box next to **Best fitness**,
as shown in the following figure.

When you click **Start**, the Optimization
app displays a plot of the best and mean values of the fitness function
at each generation.

Try this on Minimize Rastrigin's Function:

When the algorithm stops, the plot appears as shown in the following figure.

The points at the bottom of the plot denote the best fitness values, while the points above them denote the averages of the fitness values in each generation. The plot also displays the best and mean values in the current generation numerically at the top.

To get a better picture of how much the best fitness values
are decreasing, you can change the scaling of the *y*-axis
in the plot to logarithmic scaling. To do so,

Select

**Axes Properties**from the**Edit**menu in the plot window to open the Property Editor attached to your figure window as shown below.Click the

**Y Axis**tab.In the

**Y Scale**pane, select**Log**.The plot now appears as shown in the following figure.

Typically, the best fitness value improves rapidly in the early generations, when the individuals are farther from the optimum. The best fitness value improves more slowly in later generations, whose populations are closer to the optimal point.

When you display more than one plot, you can open a larger version of a plot
in a separate window. Right-click (**Ctrl**-click for macOS) on a blank area in a plot while `ga`

is
running, or after it has stopped, and choose the sole menu item.

Plot Options describes the types of plots you can create.