Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

Genetic Algorithm and Direct Search Toolbox 2.4.2

Creating and Managing Options for the Genetic Algorithm

This is a demonstration of how to create and manage options for the genetic algorithm function ga using gaoptimset in the Genetic Algorithm and Direct Search Toolbox.

Contents

Setting up a problem for ga

The function, ga, searches for an unconstrained minimum of a function using the genetic algorithm. For this demo, we will use ga to minimize the fitness function shufcn. shufcn is a real valued function of two variables.

We can use the function plotobjective in the toolbox to plot the function shufcn over the range [-2 2;-2 2].

plotobjective(@shufcn,[-2 2; -2 2]);

To use the ga solver, we need to provide at least two input arguments: a fitness function and the number of variables in the problem. The first two output arguments returned by ga are x, the best point found, and Fval, the function value at the best point. A third output argument, exitFlag, tells you the reason why ga stopped. ga can also return a fourth argument, Output, which contains information about the performance of the solver.

FitnessFunction = @shufcn;
numberOfVariables = 2;

We can run the ga solver:

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);

fprintf('The number of generations was : %d\n', Output.generations);
fprintf('The number of function evaluations was : %d\n', Output.funccount);
fprintf('The best function value found was : %g\n', Fval);
The number of generations was : 100
The number of function evaluations was : 2000
The best function value found was : -186.634

How the genetic algorithm works

The genetic algorithm, function ga, works on a population by using a set of operators that are applied to the population. A population is a set of points in the design space. The initial population is generated randomly by default. The next generation of the population is computed using the fitness of the individuals in the current generation.

Visualizing and monitoring perfomance

ga can accept one or more plot functions through an options argument. This feature is useful for visualizing the performance of the solver at run time. Plot functions can be selected using gaoptimset.

Here we use gaoptimset to create an options structure to select two plot functions. The first plot function is gaplotbestf, which plots the best and mean score of the population at every generation. The second plot function is gaplotstopping, which plots the percentage of stopping criteria satisfied.

'PlotFcns',{@gaplotbestf,@gaplotstopping});

We again run the ga solver:

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);


Specifying population options

The default initial population is created using a uniform random number generator. Default values for the population size and the range of the initial population are used to create the initial population.

Population Size

The default population size used by ga is 20. This may not be sufficient for problems with a large number of variables, or a smaller population size may be sufficient for smaller problems. Since we only have two variables, we specify a population size of 10. We will pass our options structure opts, created above, to gaoptimset to modify the value of the parameter PopulationSize to be 10.

'PopulationSize',10);

Population Range

The initial population is generated using a uniform random number generator in a default range of [0;1]. This creates an initial population where all the points are in the range 0 to 1. For example, a population of size 3 in a problem with two variables could look like:

Population = rand(3,2)

Population =

    0.1026    0.2810
    0.8642    0.7818
    0.1511    0.0476

The initial range can be set by changing the PopInitRange option using gaoptimset. The range must be a matrix with two rows. If the range has only one column, then the range of every variable is the given range. For example, if we set the range to [-1; 1], then the initial range for both our variables is -1 to 1. To specify a different initial range for each variable, the range must be specified as a matrix with two rows and numberOfVariables columns. If we set the range to [-1 0; 1 1], then the first variable will be in the range -1 to 1 and the second variable will be in the range 0 to 1 (so each column corresponds to a variable).

We will pass our options structure opts created above to gaoptimset to modify the value of the parameter PopInitRange.

opts = gaoptimset(opts,'PopInitRange',[-1 0;1 1]);

We then run the ga solver:

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);

fprintf('The number of generations was : %d\n', Output.generations);
fprintf('The number of function evaluations was : %d\n', Output.funccount);
fprintf('The best function value found was : %g\n', Fval);
The number of generations was : 100
The number of function evaluations was : 1000
The best function value found was : -186.631


Reproducing your results

By default, ga starts with a random initial population which is created using MATLAB random number generators. The next generation is produced using ga operators that use these same random number generators. Every time a random number is generated, the state of the random number generators change. This means that even if you do not change any options, when you run again you may get different results.

Here we run the solver twice to show this phenomenon:

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
fprintf('The best function value found was : %g\n', Fval);
The best function value found was : -186.657
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
fprintf('The best function value found was : %g\n', Fval);
The best function value found was : -186.725

The results are different because the states of the random number generators have changed from one run to another.

We can reproduce our results if we reset the states of the random number generators by using information returned by ga. ga returns the states of the random number generators in the Output argument. This information can be used to reset the states so the results of the next two runs are the same.

First we run the ga solver:

'The best function value found was : %g\n', Fval);
The best function value found was : -186.73

then we reset the states:

rand('state',Output.randstate);
randn('state',Output.randnstate);

then we run ga again:

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
fprintf('The best function value found was : %g\n', Fval);
The best function value found was : -186.73

Modifying the stopping criteria

ga uses four different criteria to determine when to stop the solver. ga stops when the maximum number of generations is reached. By default this number is 100. ga also detects if there is no change in the best fitness value for some time given in seconds (stall time limit), or for some number of generations (stall generation limit). Another criteria is the maximum time limit in seconds. Here we modify the stopping criteria to increase the maximum number of generations to 150 and the stall generations limit to 100, then run the ga solver.

opts = gaoptimset(opts,'Generations',150,'StallGenLimit', 100);
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);

fprintf('The number of generations was : %d\n', Output.generations);
fprintf('The number of function evaluations was : %d\n', Output.funccount);
fprintf('The best function value found was : %g\n', Fval);
The number of generations was : 150
The number of function evaluations was : 1500
The best function value found was : -186.684


Choosing ga operators

ga starts with a random set of points in the population and uses operators to produce the next generation of the population. The different operators are scaling, selection, crossover, and mutation. The toolbox provides several functions to choose from for each operator. Here we choose @fitscalingprop for FitnessScalingFcn and set the selection function and mutation function:

opts = gaoptimset('SelectionFcn',@selectiontournament, ...
    'MutationFcn',@mutationuniform, 'FitnessScalingFcn', @fitscalingprop);

We can then run the ga solver with these options:

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);

fprintf('The number of generations was : %d\n', Output.generations);
fprintf('The number of function evaluations was : %d\n', Output.funccount);
fprintf('The best function value found was : %g\n', Fval);
The number of generations was : 98
The number of function evaluations was : 1960
The best function value found was : -16.861

The best function value may improve or it may get worse by choosing different operators. Choosing a good set of operators for your problem is often best done by experimentation. The graphical interface for the genetic algorithm, gatool provides an environment for easily experimenting with different options and then quickly trying them out by running the ga solver.

Contact sales
Free technical kit
Trial software
E-mail this page

Get Pricing and
Licensing Options