Plotting ga custom plot with additional inputs (calculated parameters )

5 views (last 30 days)
AdarG
AdarG on 1 Apr 2019
Commented: AdarG on 1 Apr 2019
This quesion was also asked in a similar way as a comment here but I didn't get an answer.
I do optimization with a genetic algorithm. During the optimization I calculate a matrix (nx2) apex_accu_mat and I want to plot it during the optimization (see plot definition below).
I get error that matlab does not "know" apex_accu_mat. I can't pass the currrent value of apex_accu_mat to the custom plot function.
I tried this:
options = optimoptions('ga','PlotFcns',...
{@gaplotbestf, @(options,state,flag) plot_mean_apex_loc(options,state,flag,apex_accu_mat)},...
'PlotInterval',2,'MaxGenerations',2,'PopulationSize',1);
Then, after the definition of the fitness function I define the custom plot function as follows:
function state=plot_mean_apex_loc(options,state,flag,apex_accu_mat)
state.StopFlag=1;
apex_accu_mean=mean(apex_accu_mat);
plot(apex_accu_mean(1),apex_accu_mean(2),'o');
end
Thanks!

Answers (1)

Alan Weiss
Alan Weiss on 1 Apr 2019
When you create an anonymous function to pass extra parameters, the parameters that are available in the nested function are the ones that existed at the time the nested function was created. That's why your current version does not work.
To continue, you might want to use a nested function to pass the extra parameters. You could also use a global variable.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  3 Comments
AdarG
AdarG on 1 Apr 2019
Hi Alan, thank you for your comment.
The only help I need is to understand how to plot a variable that exists and is calculatedonly inside the fitness function. The variable is needed to evaluate the fitness funciton.
If you have a concrete example, or just a scheme, of such case, it will be best. In the meantime I will go again through other examples.
Thanks again.

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!