effects of genetic algorithm options -凯发k8网页登录
this example shows the effects of some options for the genetic algorithm function ga
. you create and change options by using the optimoptions
function.
set up a problem for ga
ga
searches for a minimum of a function using the genetic algorithm. for this example, use ga
to minimize the fitness function shufcn
, a real-valued function of two variables.
plot shufcn
over the range = [-2 2;-2 2]
by calling plotobjective
, which is included when you run this example.
plotobjective(@shufcn,[-2 2; -2 2]);
to use the ga
solver, provide at least two input arguments: a fitness function and the number of variables in the problem. the first two output arguments returned by ga
are x
, the best point found, and fval
, the function value at the best point. a third output argument, exitflag
, indicates why ga
stopped. ga
can also return a fourth argument, output
, which contains information about the performance of the solver.
fitnessfunction = @shufcn; numberofvariables = 2;
run the ga
solver.
rng default % for reproducibility [x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the number of generations is: %d\n', output.generations);
the number of generations is: 124
fprintf('the number of function evaluations is: %d\n', output.funccount);
the number of function evaluations is: 5881
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.199
if you run this example without the rng default
command, your results can differ, because ga
is a stochastic algorithm.
how the genetic algorithm works
the genetic algorithm works on a population using a set of operators that are applied to the population. a population is a set of points in the design space. the initial population is generated randomly by default. the algorithm computes the next generation of the population using the fitness of the individuals in the current generation. for details, see .
add visualization
to visualize the solver performance while it is running, set a 'plotfcn'
option using optimoptions
. in this case, select two plot functions in a cell array. set gaplotbestf
, which plots the best and mean score of the population at every generation. also set gaplotstopping
, which plots the percentage of stopping criteria satisfied.
opts = optimoptions(@ga,'plotfcn',{@gaplotbestf,@gaplotstopping});
run the ga
solver, including the opts
argument.
[x,fval,exitflag,output] = ...
ga(fitnessfunction,numberofvariables,[],[],[],[],[],[],[],opts);
optimization terminated: average change in the fitness value less than options.functiontolerance.
specify population options
population options can have a large effect on solver performance. the speed of each iteration depends on the population size: a larger population leads to slower iterations. conversely, a larger population leads to ga
exploring more thoroughly, so can lead to a better solution. similarly, a wider initial range can lead to more thorough exploration, but can require a larger population to explore the wider range with a similar thoroughness.
specify population size
ga
creates a default initial population by using a uniform random number generator. the default population size used by ga
is 50 when the number of decision variables is less than 5, and 200 otherwise. the default size might not work well for some problems; for example, a smaller population size can be sufficient for smaller problems. since the current problem has only two variables, specify a population size of 10. set the value of the option populationsize
to 10 in the existing options, opts
.
opts.populationsize = 10;
specify initial population range
the default method for generating an initial population uses a uniform random number generator. for problems without integer constraints, ga
creates an initial population where all the points are in the range –10 to 10. for example, you can generate a population of size three in the default range using this command:
population = [-10,-10] 20*rand(3,2);
you can set the initial range by changing the initialpopulationrange
option. the range must be a matrix with two rows. if the range has only one column, that is, it is 2-by-1, then the range of every variable is the given range. for example, if you set the range to [-1; 1]
, then the initial range for both variables is –1 to 1. to specify a different initial range for each variable, you must specify the range as a matrix with two rows and numberofvariables
columns. for example, if you set the range to [-1 0; 1 2]
, then the first variable has the range –1 to 1, and the second variable has the range 0 to 2 (each column corresponds to a variable).
modify the value of the option initialpopulationrange
in the existing options, opts
.
opts.initialpopulationrange = [-1 0; 1 2];
run the ga
solver.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables,[],[],[], ...
[],[],[],[],opts);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the number of generations is: %d\n', output.generations);
the number of generations is: 67
fprintf('the number of function evaluations is: %d\n', output.funccount);
the number of function evaluations is: 614
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -179.987
reproduce results
by default, ga
starts with a random initial population created using matlab® random number generators. the solver produces the next generation using ga
operators that also use these same random number generators. every time a random number is generated, the state of the random number generators changes. so, even if you do not change any options, you can get different results when you run the solver again.
run the solver twice to show this phenomenon.
run the ga
solver.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.484
run ga
again.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -185.867
ga
gives different results in the two runs because the state of the random number generator changes from one run to another.
if you want to reproduce your results before you run ga
, you can save the state of the random number stream.
thestate = rng;
run ga
.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.467
reset the stream and rerun ga
. the results are identical to the previous run.
rng(thestate); [x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.467
if you run ga
before specifying to reproduce the results, you can reset the random number generator as long as you have the output
structure.
strm = randstream.getglobalstream; strm.state = output.rngstate.state;
rerun ga
. again, the results are identical.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.467
modify stopping criteria
ga
uses four different criteria to determine when to stop the solver. ga
stops when it reaches the maximum number of generations; by default, this number is 100 times the number of variables. ga
also detects if the best fitness value does not change for some time given in seconds (stall time limit), or for some number of generations (maximum stall generations). another criteria is the maximum time limit in seconds. modify the stopping criteria to increase the maximum number of generations to 300 and the maximum stall generations to 100.
opts = optimoptions(opts,'maxgenerations',300,'maxstallgenerations', 100);
rerun the ga
solver.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables,[],[],[], ...
[],[],[],[],opts);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the number of generations is: %d\n', output.generations);
the number of generations is: 299
fprintf('the number of function evaluations is: %d\n', output.funccount);
the number of function evaluations is: 2702
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.729
specify ga
operators
ga
starts with a random set of points in the population and uses operators to produce the next generation of the population. the different operators are scaling, selection, crossover, and mutation. the toolbox provides several functions to specify for each operator. specify fitscalingprop
for fitnessscalingfcn
and selectiontournament
for selectionfcn
.
opts = optimoptions(@ga,'selectionfcn',@selectiontournament, ... 'fitnessscalingfcn',@fitscalingprop);
rerun ga
.
[x,fval,exitflag,output] = ga(fitnessfunction,numberofvariables,[],[],[], ...
[],[],[],[],opts);
optimization terminated: average change in the fitness value less than options.functiontolerance.
fprintf('the number of generations is: %d\n', output.generations);
the number of generations is: 52
fprintf('the number of function evaluations is: %d\n', output.funccount);
the number of function evaluations is: 2497
fprintf('the best function value found is: %g\n', fval);
the best function value found is: -186.417
the best function value can improve or get worse based on the specified operators. experimenting with different operators is often the best way to determine which set of operators works best for your problem.