필터 지우기
필터 지우기

Customised output function for ga

조회 수: 3 (최근 30일)
Yaser Khojah
Yaser Khojah 2018년 5월 18일
댓글: Walter Roberson 2020년 12월 31일
I have been looking how to create my own output function in GA to get each x and fval of each iteration and generation. Although I have looked into all the answered questions for such a question, nothing has been working so far. Here is my output function and please help me with what I'm missing.
function [state,options,optchanged] = gaoutfun(options,state,flag)
persistent history_pop history_Best history_Score
history_pop = [];
history_Best = [];
history_Score = [];
optchanged = false;
switch flag
case 'init'
history_pop = [history_pop;state.Population];
assignin('base','gapopulationhistory',history_pop);
history_Best = [history_Best;state.Best];
assignin('base','gabesthistory',history_Best);
history_Score = [history_Score;state.Score];
assignin('base','gascorehistory',history_Score);
case 'iter'
history_pop = [history_pop;state.Population];
assignin('base','gapopulationhistory',history_pop);
history_Best = [history_Best;state.Best];
assignin('base','gabesthistory',history_Best);
history_Score = [history_Score;state.Score];
assignin('base','gascorehistory',history_Score);
case 'done'
history_pop = [history_pop;state.Population];
assignin('base','gapopulationhistory',history_pop);
history_Best = [history_Best;state.Best];
assignin('base','gabesthistory',history_Best);
history_Score = [history_Score;state.Score];
assignin('base','gascorehistory',history_Score);
end

채택된 답변

Walter Roberson
Walter Roberson 2018년 5월 18일
function [state,options,optchanged] = gaoutfun(options,state,flag)
persistent state_record
if isempty(state_record)
state_record = struct('Population', {}, 'Best', {}, 'Score', {});
end
if nargin == 0
state = state_record;
options = [];
optchanged = [];
else
state_record(end+1) = struct('Population', state.Population, 'Best', state.Best', 'Score', state.Score);
optchanged = false;
end
Before re-using this function, call
clear gaoutfun
Let the function be called as usual. Do not expect any changes outside the function when it is called.
After the ga run is finished, call
record = gaoutfun();
with no inputs. A structure of the state information will be returned.
However, this is likely to be a bit inefficient because the struct is expanded each iteration. I would suggest that when isempty() succeeds, that options be examined to find the maximum generations, and that state_record be initialized that large. Use state.Generation as the index for the output instead of end+1
  댓글 수: 4
MD. Rokibujjaman sovon
MD. Rokibujjaman sovon 2020년 12월 31일
what is the scorehistory and besthistory? is scorehistory is the fval value for each iteration?
Thanks.
Walter Roberson
Walter Roberson 2020년 12월 31일
https://www.mathworks.com/help/gads/ga.html#d122e41218
https://www.mathworks.com/help/gads/some-genetic-algorithm-terminology.html
https://www.mathworks.com/matlabcentral/answers/322582-how-is-score-in-matlab-s-genetic-algorithm-defined

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Weiss
Alan Weiss 2018년 5월 18일
Have you looked at the documentation example that creates a history in an output function for ga?
Alan Weiss
MATLAB mathematical toolbox documentation
  댓글 수: 1
Yaser Khojah
Yaser Khojah 2018년 5월 18일
편집: Walter Roberson 2018년 5월 18일
Dear Alan,
Yeah I did and could not get any close. I saw this example https://uk.mathworks.com/help/gads/custom-output-function-for-genetic-algorithm.html#d119e19384 and I tried to make something similar but nothing worked. Is there away you spot what I missing in my code? Thank you so much.
I saw this one too https://uk.mathworks.com/help/matlab/math/output-functions.html but does not work for GA. I'm lost now and been working on it for awhile and can't get what I need.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by