| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Genetic Algorithm and Direct Search Toolbox |
| Contents | Index |
| Learn more about Genetic Algorithm and Direct Search Toolbox |
| On this page… |
|---|
Example — Creating a Custom Plot Function Example — Resuming the Genetic Algorithm from the Final Population |
The Optimization Tool GUI is described in the chapter Optimization Tool in the Optimization Toolbox User's Guide. This section describes some places where there are some differences between the use of the genetic algorithm in the Optimization Tool and the use of other solvers.
The Plot functions pane, shown in the following figure, enables you to display various plots of the results of the genetic algorithm.

Select the check boxes next to the plots you want to display. For example, if you select Best fitness and Best individual, and run the example described in Example — Rastrigin's Function, the tool displays plots similar to those shown in the following figure.

The upper plot displays the best and mean fitness values in each generation. The lower plot displays the coordinates of the point with the best fitness value in the current generation.
Note When you display more than one plot, clicking on any plot while the genetic algorithm is running or after the solver has completed opens a larger version of the plot in a separate window. |
Plot Options describes the types of plots you can create.
If none of the plot functions that come with the software is suitable for the output you want to plot, you can write your own custom plot function, which the genetic algorithm calls at each generation to create the plot. This example shows how to create a plot function that displays the change in the best fitness value from the previous generation to the current generation.
This section covers the following topics:
To create the plot function for this example, copy and paste the following code into a new M-file in the MATLAB Editor.
function state = gaplotchange(options, state, flag)
% GAPLOTCHANGE Plots the logarithmic change in the best score from the
% previous generation.
%
persistent last_best % Best score in the previous generation
if(strcmp(flag,'init')) % Set up the plot
set(gca,'xlim',[1,options.Generations],'Yscale','log');
hold on;
xlabel Generation
title('Change in Best Fitness Value')
end
best = min(state.Score); % Best score in the current generation
if state.Generation == 0 % Set last_best to best.
last_best = best;
else
change = last_best - best; % Change in best score
last_best=best;
plot(state.Generation, change, '.r');
title(['Change in Best Fitness Value'])
end
Then save the M-file as gaplotchange.m in a folder on the MATLAB path.
To use the custom plot function, select Custom in the Plot functions pane and enter @gaplotchange in the field to the right. To compare the custom plot with the best fitness value plot, also select Best fitness. Now, if you run the example described in Example — Rastrigin's Function, the tool displays plots similar to those shown in the following figure.

Note that because the scale of the y-axis in the lower custom plot is logarithmic, the plot only shows changes that are greater then 0. The logarithmic scale enables you to see small changes in the fitness function that the upper plot does not reveal.
The plot function uses information contained in the following structures, which the genetic algorithm passes to the function as input arguments:
options — The current options settings
state — Information about the current generation
flag — String indicating the current status of the algorithm
The most important lines of the plot function are the following:
persistent last_best
Creates the persistent variable last_best—the best score in the previous generation. Persistent variables are preserved over multiple calls to the plot function.
set(gca,'xlim',[1,options.Generations],'Yscale','log');
Sets up the plot before the algorithm starts. options.Generations is the maximum number of generations.
best = min(state.Score)
The field state.Score contains the scores of all individuals in the current population. The variable best is the minimum score. For a complete description of the fields of the structure state, see Structure of the Plot Functions.
change = last_best - best
The variable change is the best score at the previous generation minus the best score in the current generation.
plot(state.Generation, change, '.r')
Plots the change at the current generation, whose number is contained in state.Generation.
The code for gaplotchange contains many of the same elements as the code for gaplotbestf, the function that creates the best fitness plot.
To reproduce the results of the last run of the genetic algorithm, select the Use random states from previous run check box. This resets the states of the random number generators used by the algorithm to their previous values. If you do not change any other settings in the Optimization Tool, the next time you run the genetic algorithm, it returns the same results as the previous run.
Normally, you should leave Use random states from previous run unselected to get the benefit of randomness in the genetic algorithm. Select the Use random states from previous run check box if you want to analyze the results of that particular run or show the exact results to others. After the algorithm has run, you can clear your results using the Clear Status button in the Run solver settings.
Note If you select Include information needed to resume this run, then selecting Use random states from previous run has no effect on the initial population created when you import the problem and run the genetic algorithm on it. The latter option is only intended to reproduce results from the beginning of a new run, not from a resumed run. |
The following example shows how export a problem so that when you import it and click Start, the genetic algorithm resumes from the final population saved with the exported problem. To run the example, enter the following information in the Optimization Tool:
Set Fitness function to @ackleyfcn, which computes Ackley's function, a test function provided with the software.
Set Number of variables to 10.
Select Best fitness in the Plot functions pane.
Click Start.
This displays the following plot.

Suppose you want to experiment by running the genetic algorithm with other options settings, and then later restart this run from its final population with its current options settings. You can do this using the following steps:
In the dialog box that appears,
Select Export problem and options to a MATLAB structure named.
Enter a name for the problem and options, such as ackley_uniform, in the text field.
Select Include information needed to resume this run.
The dialog box should now appear as in the following figure.

This exports the problem and options to a structure in the MATLAB workspace. You can view the structure in the MATLAB Command Window by entering
ackley_uniform
ackley_uniform =
fitnessfcn: @ackleyfcn
nvars: 10
Aineq: []
bineq: []
Aeq: []
beq: []
lb: []
ub: []
nonlcon: []
rngstate: []
solver: 'ga'
options: [1x1 struct]After running the genetic algorithm with different options settings or even a different fitness function, you can restore the problem as follows:
This sets the Initial population and Initial scores fields in the Population panel to the final population of the run before you exported the problem. All other options are restored to their setting during that run. When you click Start, the genetic algorithm resumes from the saved final population. The following figure shows the best fitness plots from the original run and the restarted run.

Note If, after running the genetic algorithm with the imported problem, you want to restore the genetic algorithm's default behavior of generating a random initial population, delete the population in the Initial population field. |
![]() | Using the Genetic Algorithm | Using the Genetic Algorithm from the Command Line | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |