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.
functionstate = gaplotchange(options, state, flag)% GAPLOTCHANGE Plots the logarithmic change in the best score from the% previous generation.%persistentlast_best% Best score in the previous generationif(strcmp(flag,'init'))% Set up the plotxlim([1,options.MaxGenerations]); axx = gca; axx.YScale ='log'; holdon; xlabelGenerationtitle('Log Absolute Change in Best Fitness Value')endbest = min(state.Score);% Best score in the current generationifstate.Generation == 0% Set last_best to best.last_best = best;elsechange = last_best - best;% Change in best scorelast_best = best;ifchange > 0% Plot only when the fitness improvesplot(state.Generation,change,'xr');endend
Save the file asgaplotchange.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 reproducibilityoptions = 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
情节函数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
——前一代最好的分数。波西斯tent 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, seeStructure 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 forgaplotchange
contains many of the same elements as the code forgaplotbestf
, the function that creates the best fitness plot.