how to use 'OutputFcn' in GA

24 views (last 30 days)
imola
imola on 27 Aug 2014
Edited: michio on 26 Oct 2023
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

Answers (1)

Alan Weiss
Alan Weiss on 27 Aug 2014
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 Comments
imola
imola on 27 Aug 2014
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 on 27 Aug 2014
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!