|On this page…|
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.
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 set(gca,'xlim',[1,options.Generations],'Yscale','log'); hold on; xlabel Generation title('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; plot(state.Generation, change, '.r'); title(['Change in Best Fitness Value']) end
Then save the file as gaplotchange.m in a folder on the MATLAB path.
To use the custom plot function, select Custom in the Plot functions pane and enter @gaplotchange in the field to the right. To compare the custom plot with the best fitness value plot, also select Best fitness. Now, if you run the example described in Minimize Rastrigin's Function, the tool displays plots similar to those shown in the following figure.
Note that because the scale of the y-axis in the lower custom plot is logarithmic, the plot only shows changes that are greater than 0. The logarithmic scale enables you to see small changes in the fitness function that the upper plot does not reveal.
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 settings
state — Information about the current generation
flag — String indicating the current status of the algorithm
The most important lines of the plot function are the following:
Creates the persistent variable last_best—the best score in the previous generation. Persistent variables are preserved over multiple calls to the plot function.
Sets up the plot before the algorithm starts. options.Generations 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 variable best 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.
plot(state.Generation, change, '.r')
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.