Products & Services Solutions Academia Support User Community Company

Learn more about Genetic Algorithm and Direct Search Toolbox   

Genetic Algorithm Optimizations Using the Optimization Tool GUI

Introduction

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.

Displaying Plots

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.

Plot Options describes the types of plots you can create.

Example — Creating a Custom Plot Function

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:

Creating the Custom Plot Function

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.

Using the Plot Function

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.

How the Plot Function Works

The plot function uses information contained in the following structures, which the genetic algorithm passes to the function as input arguments:

The most important lines of the plot function are the following:

The code for gaplotchange contains many of the same elements as the code for gaplotbestf, the function that creates the best fitness plot.

Reproducing Your Results

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.

Example — Resuming the Genetic Algorithm from the Final Population

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:

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:

  1. Click Export to Workspace.

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

  3. Click OK.

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:

  1. Select Import Problem from the File menu. This opens the dialog box shown in the following figure.

  2. Select ackley_uniform.

  3. Click Import.

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.

  


Recommended Products

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