Plotting ga custom plot with additional inputs (calculated parameters )

2 vues (au cours des 30 derniers jours)
AdarG
AdarG le 1 Avr 2019
Commenté : AdarG le 1 Avr 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!

Réponses (1)

Alan Weiss
Alan Weiss le 1 Avr 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 commentaires
Alan Weiss
Alan Weiss le 1 Avr 2019
I have a hard time understanding what you are doing. You set a population size of 1, which is nonsensical and probably causes ga to fail. You set state.StopFlag = 1, which I suppose stops the optimization immediately. You set your options using a plot function that is based on a matrix apex_accu_mat that does not exist at the time you create the options. It is not at all clear to me if apex_accu_mat is needd in the evaluation of your fitness function or not; if not, then just calculate it in the plot function.
Sorry, I don't think that I can help you with this any more than to suggest that you reread the examples of how to pass parameters.
Alan Weiss
MATLAB mathematical toolbox documentation
AdarG
AdarG le 1 Avr 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.

Connectez-vous pour commenter.

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by