MATLAB Answers

Optimisation Tool GA Custom Plot

24 views (last 30 days)
PB75
PB75 on 31 Mar 2021
Answered: PB75 on 1 Apr 2021
Hi All,
Have searched through the previous questions posted on custom plots and output functions in optimisation toolbox using the GA solver, however, still need some guidance please.
My optimisation problem has 2 variables k(1) & k(2) (PI gains) and the fitness function is to minimum ITAE seen by the controller.
For my report I would like to plot the 2 variables and the fitness function value at the end of each generation (25) to show how the variables and the ITAE change, in a surface plot or heat plot.
I have tried to use @gaplotchange as a starting point, however this shows and error "Too many ouput arguments".
Can anyone help with modifying the 'gaplotchange' code or suggest a better 'PlotFnc' to plot a surface or heat plot at each generation?
Can the history be outputed to the workspace with a predefined output function?
Additionally, does the function get called from the Optimisation Tool>Options>Output function>Custom function ?
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
Thanks
patrick

Accepted Answer

Alan Weiss
Alan Weiss on 31 Mar 2021
Perhaps if you put the names of the functions in curly braces {} you would have success:
{@gaplotbestf,@gaplotchange}
But come to think of it, gaplotbestf is a built in PLOT function, not an OUTPUT function. Maybe you should include the check mark for gaplotbestf in the plot function section and just include the handle for gaplotchange in the output function section.
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (1)

PB75
PB75 on 1 Apr 2021
Hi Alan,
Thanks for your reply.
Error "Too many ouput arguments" was due to me putting the "aplotchange" function in the output function section, needs to be in the plot section.
Cheers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!