Plotting ga custom plot with additional inputs (calculated parameters )
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Alan Weiss
2019-4-1
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 个评论
Alan Weiss
2019-4-1
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!