Documentation Center

  • Trial Software
  • Product Updates

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
[x fval] = ga(@rastriginsfcn, 2)

This returns

Optimization terminated:
 average change in the fitness value less than options.TolFun.

x =
   -0.0017   -0.0185

fval =
    0.0682

Setting Options at the Command Line

You can specify any of the options that are available for ga by passing an options structure 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 the options structure using the function gaoptimset.

options = gaoptimset(@ga)

This returns the structure options with the default values for its fields.

options =
        PopulationType: 'doubleVector'
          PopInitRange: []
        PopulationSize: '50 when numberOfVariables <= 5, else 200'
            EliteCount: '0.05*PopulationSize'
     CrossoverFraction: 0.8000
        ParetoFraction: []
    MigrationDirection: 'forward'
     MigrationInterval: 20
     MigrationFraction: 0.2000
           Generations: '100*numberOfVariables'
             TimeLimit: Inf
          FitnessLimit: -Inf
         StallGenLimit: 50
             StallTest: 'averageChange'
        StallTimeLimit: Inf
                TolFun: 1.0000e-06
                TolCon: 1.0000e-06
     InitialPopulation: []
         InitialScores: []
        InitialPenalty: 10
         PenaltyFactor: 100
          PlotInterval: 1
           CreationFcn: @gacreationuniform
     FitnessScalingFcn: @fitscalingrank
          SelectionFcn: @selectionstochunif
          CrossoverFcn: @crossoverscattered
           MutationFcn: {[@mutationgaussian]  [1]  [1]}
    DistanceMeasureFcn: []
             HybridFcn: []
               Display: 'final'
              PlotFcns: []
            OutputFcns: []
            Vectorized: 'off'
           UseParallel: 0

The function 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 the options structure, such as options.PopulationSize. You can display any of these values by entering options. followed by 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 an options structure 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 = gaoptimset('PopulationSize', 100)

This creates the options structure 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 the options structure, such as setting PlotFcns to @gaplotbestf, which plots the best fitness function value at each generation, call gaoptimset with the syntax

options = gaoptimset(options,'PlotFcns',@plotbestf)

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

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

options = gaoptimset('PopulationSize',100,'PlotFcns',@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.

Was this topic helpful?