Options and Outputs
Running ga with the Default Options
To run the genetic algorithm with the default options, callga
with the syntax
[x,fval] = ga(@fitnessfun, nvars)
The input arguments toga
are
@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 pointfval
— 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 forga
by passingoptions
as an input argument toga
using the syntax
[x,fval] = ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options)
This syntax does not specify any linear equality, linear inequality, or nonlinear constraints.
You createoptions
using the functionoptimoptions
.
options = optimoptions(@ga);
This returnsoptions
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 ofoptions
, such asoptions.PopulationSize
. You can display any of these values by enteringoptions
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 createoptions
with a field value that is different from the default — for example to setPopulationSize
to100
instead of its default value50
— enter
options = optimoptions('ga','PopulationSize',100);
This createsoptions
with all values set to their defaults except forPopulationSize
, which is set to100
.
If you now enter,
ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options)
ga
runs the genetic algorithm with a population size of100
.
If you subsequently decide to change another field inoptions
, such as settingPlotFcn
to@gaplotbestf
, which plots the best fitness function value at each generation, calloptimoptions
with the syntax
options = optimoptions(options,'PlotFcn',@plotbestf);
This preserves the current values of all fields ofoptions
except forPlotFcn
, which is changed to@plotbestf
. Note that if you omit the input argumentoptions
,optimoptions
resetsPopulationSize
to its default value.
You can also set bothPopulationSize
andPlotFcn
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 callga
with the syntax
[x,fval,exitflag,output,population,scores] = ga(@fitnessfcn, nvars)
Besidesx
andfval
, this function returns the following additional output arguments:
exitflag
— Integer value corresponding to the reason the algorithm terminatedoutput
— Structure containing information about the performance of the algorithm at each generationpopulation
— Final populationscores
— Final scores
See thega
reference page for more information about these arguments.