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. explains 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 for .
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 .
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 terminatedoutput
— structure containing information about the performance of the algorithm at each generationpopulation
— final populationscores
— final scores
see the reference page for more information about these arguments.