Main Content

Options and Outputs

Running ga with the Default Options

To run the genetic algorithm with the default options, callgawith the syntax

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

The input arguments togaare

  • @fitnessfun— A function handle to the file that computes the fitness function.Compute Objective Functionsexplains 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 atx

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

You can run the example described inMinimize Rastrigin's Function从命令行输入

rng(1,'twister') % for reproducibility [x,fval] = ga(@rastriginsfcn,2)

This returns

优化终止:平均变化的健康ness 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 forgaby passingoptionsas an input argument togausing the syntax

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

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

You createoptionsusing the functionoptimoptions.

options = optimoptions(@ga);

This returnsoptionswith the default values for its fields.gauses these default values if you do not pass in options as an input argument.

The value of each option is stored in a field ofoptions, such asoptions.PopulationSize. You can display any of these values by enteringoptionsfollowed 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 createoptionswith a field value that is different from the default — for example to setPopulationSizeto100instead of its default value50— enter

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

This createsoptionswith all values set to their defaults except forPopulationSize, which is set to100.

If you now enter,

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

garuns the genetic algorithm with a population size of100.

If you subsequently decide to change another field inoptions, such as settingPlotFcnto@gaplotbestf, which plots the best fitness function value at each generation, calloptimoptionswith the syntax

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

This preserves the current values of all fields ofoptionsexcept forPlotFcn, which is changed to@plotbestf. Note that if you omit the input argumentoptions,optimoptionsresetsPopulationSizeto its default value.

You can also set bothPopulationSizeandPlotFcnwith 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 callgawith the syntax

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

Besidesxandfval, 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 thegareference page for more information about these arguments.

See Also

Related Topics