create custom plot function
about custom plot functions
if none of the plot functions that come with the software is suitable for the output you want to plot, you can write your own custom plot function, which the genetic algorithm calls at each generation to create the plot. this example shows how to create a plot function that displays the change in the best fitness value from the previous generation to the current generation.
creating the custom plot function
to create the plot function for this example, copy and paste the following code into a new file in the matlab® editor.
function state = gaplotchange(options, state, flag) % gaplotchange plots the logarithmic change in the best score from the % previous generation. % persistent last_best % best score in the previous generation if(strcmp(flag,'init')) % set up the plot xlim([1,options.maxgenerations]); axx = gca; axx.yscale = 'log'; hold on; xlabel generation title('log absolute change in best fitness value') end best = min(state.score); % best score in the current generation if state.generation == 0 % set last_best to best. last_best = best; else change = last_best - best; % change in best score last_best = best; if change > 0 % plot only when the fitness improves plot(state.generation,change,'xr'); end end
save the file as gaplotchange.m
in a folder on the matlab path.
using the custom plot function
to use the custom plot function, include it in the options.
rng(100) % for reproducibility options = optimoptions('ga','plotfcn',{@gaplotbestf,@gaplotchange}); [x,fval] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options)
optimization terminated: maximum number of generations exceeded. x = -0.0003 0.0014 fval = 4.2189e-04
the plot shows only changes that are greater than 0, which are improvements in best fitness. the logarithmic scale enables you to see small changes in the best fitness function that the upper plot does not reveal.
how the plot function works
the plot function uses information contained in the following structures, which the genetic algorithm passes to the function as input arguments:
options
— the current options settingsstate
— information about the current generationflag
— current status of the algorithm
the most important lines of the plot function are the following:
persistent last_best
creates the persistent variable
last_best
—the best score in the previous generation. persistent variables are preserved over multiple calls to the plot function.xlim([1,options.maxgenerations]);
axx = gca;
axx.yscale = 'log';
sets up the plot before the algorithm starts.
options.maxgenerations
is the maximum number of generations.best = min(state.score)
the field
state.score
contains the scores of all individuals in the current population. the variablebest
is the minimum score. for a complete description of the fields of the structure state, see structure of the plot functions.change = last_best - best
the variable change is the best score at the previous generation minus the best score in the current generation.
if change > 0
plot only if there is a change in the best fitness.
plot(state.generation,change,'xr')
plots the change at the current generation, whose number is contained in
state.generation
.
the code for gaplotchange
contains many of
the same elements as the code for gaplotbestf
,
the function that creates the best fitness plot.