how to use 'OutputFcn' in GA

14 次查看(过去 30 天)
imola
imola 2014-8-27
编辑: michio 2023-10-26
Dear All,
I'm trying to use the next function to find some information about previous generation and I found difficulty with writing the state and the options
options = gaoptimset('OutputFcn', @outputfun);
[state,options,optchanged] = outputfun(options,state,flag)
I have this code which is work,
function [x,fval,exitflag,output,population,score] = optimGA(nvars,lb,PopulationSize_Data,Generations_Data,TolFun_Data,InitialPopulation_Data)
%%This is an auto generated MATLAB file from Optimization Tool.
could you please explain to me how to write it. or you may have better idea
Thank you in advance

回答(1 个)

Alan Weiss
Alan Weiss 2014-8-27
GA will pass in options, state, and flag. You should pass those exact same options and state to the output function output arguments, and set optchanged to false, unless for some reason you want to change the options during your output function call. See the documentation.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 个评论
imola
imola 2014-8-27
Dear Alan,
what do you mean by (You should pass those exact same options and state to the output function output arguments) could you give me example, because I couldn't understand the documentation I wrote it like this
function[state,options,optchanged] = outputfun(options, state, flag)
flag=false;
options = gaoptimset(state,Population);
options = gaoptimset(options,'Display', 'diagnose');
options = gaoptimset('OutputFcn', @outputfun);
ga(options)
I have this (state,Population) gave error because of population and I don't know if the other lines are correct or no.
thanks for replying
Alan Weiss
Alan Weiss 2014-8-27
function[state,options,optchanged] = outputfun(options, state, flag)
optchanged = false;
% DO NOT set options, state, or flag!
% Just write code to calculate whatever it is you want here.
% If you need to read an option, then read it, don't set it.
% Make sure that your options include @outputfun as your output function.
Alan Weiss
MATLAB mathematical toolbox documentation

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by