Main Content

Options and Outputs

Running ga with the Default Options

To run the genetic algorithm with the default options, call ga with the syntax

[x,fval] = ga(@fitnessfun, nvars)

The input arguments to ga are

  • @fitnessfun — A function handle to the file that computes the fitness function. Compute Objective Functions explains how to write this file.

  • nvars — The number of independent variables for the fitness function.

The output arguments are

  • x — The final point

  • fval — The value of the fitness function at x

For a description of additional input and output arguments, see the reference page for ga.

You can run the example described in Minimize Rastrigin's Function from the command line by entering

rng(1,'twister') % For reproducibility
% Define Rastrigin's function
rastriginsfcn = @(pop)10.0 * size(pop,2) + sum(pop .^2 - 10.0*cos(2*pi.*pop),2);
[x,fval] = 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

Setting Options at the Command Line

You can specify any of the options that are available for ga by passing options as an input argument to ga using the syntax

[x,fval] = ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options)

This syntax does not specify any linear equality, linear inequality, or nonlinear constraints.

You create options using the function optimoptions.

options = optimoptions(@ga);

This returns options with the default values for its fields. ga uses these default values if you do not pass in options as an input argument.

The value of each option is stored in a field of options, such as options.PopulationSize. You can display any of these values by entering options followed by a period and the name of the field. For example, to display the size of the population for the genetic algorithm, enter

options.PopulationSize
ans =

'50 when numberOfVariables <= 5, else 200'

To create options with a field value that is different from the default — for example to set PopulationSize to 100 instead of its default value 50 — enter

options = optimoptions('ga','PopulationSize',100);

This creates options with all values set to their defaults except for PopulationSize, which is set to 100.

If you now enter,

ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options)

ga runs the genetic algorithm with a population size of 100.

If you subsequently decide to change another field in options, such as setting PlotFcn to @gaplotbestf, which plots the best fitness function value at each generation, call optimoptions with the syntax

options = optimoptions(options,'PlotFcn',@plotbestf);

This preserves the current values of all fields of options except for PlotFcn, which is changed to @plotbestf. Note that if you omit the input argument options, optimoptions resets PopulationSize to its default value.

You can also set both PopulationSize and PlotFcn with the single command

options = optimoptions('ga','PopulationSize',100,'PlotFcn',@plotbestf);

Additional Output Arguments

To get more information about the performance of the genetic algorithm, you can call ga with the syntax

[x,fval,exitflag,output,population,scores] = ga(@fitnessfcn, nvars)

Besides x and fval, this function returns the following additional output arguments:

  • exitflag — Integer value corresponding to the reason the algorithm terminated

  • output — Structure containing information about the performance of the algorithm at each generation

  • population — Final population

  • scores — Final scores

See the ga reference page for more information about these arguments.

See Also

Related Topics