This is machine translation

Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Translate This Page

MathWorks Machine Translation

The automated translation of this page is provided by a general purpose third party translator tool.

MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.

Minimize Rastrigin's Function

Rastrigin's Function

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+x12+x22−10(cos2πx1+cos2πx2).

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.

Finding the Minimum of Rastrigin's Function

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

Note
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:

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

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

Displaying Plots

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.

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.

Note
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 Mac) on a blank area in a plot while ga is
running, or after it has stopped, and choose the sole menu item.