how can i save every generation's best position using ga

14 ビュー (過去 30 日間)
li jian
li jian 2014 年 3 月 28 日
コメント済み: Tsuwei Tan 2021 年 4 月 20 日
i'm using the ga function in matlab, i want to save every generation's best position
how can i do that.becanse i cannot find
[x,fval,exitflag,output,population,scores] = ga(fitnessfcn,nvars,...)
return such values
  1 件のコメント
Tsuwei Tan
Tsuwei Tan 2021 年 4 月 20 日
options = optimoptions('ga',..'OutputFcns',@ga_save_each_gen);
I did a simple funciton ga_save_each_gen to do so.
function [state,options,optchanged]=ga_save_each_gen(options,state,flag)
Score_gen=state.Score;
Population_gen=state.Population;
Generation_gen=state.Generation;
optchanged=[];
save(['gen_' num2str(Generation_gen,'%.4d') '.mat'],'Score_gen','Population_gen','Generation_gen')
end

サインインしてコメントする。

回答 (1 件)

A Jenkins
A Jenkins 2014 年 3 月 28 日
Use gaoptimset to change the options. Display and PlotFcns will show some detail. You may also want to look at the OutputFcns option where you can write your own custom function to collect data.
A = [1 1; -1 2; 2 1];
b = [2; 2; 3];
lb = zeros(2,1);
ga_options=gaoptimset('Display','iter','PlotFcns',@gaplotbestf);
[x,fval,exitflag,output,population,scores] = ga(@lincontest6,...
2,A,b,[],[],lb,[],[],ga_options);
  3 件のコメント
li jian
li jian 2014 年 3 月 31 日
Hi Jenkins,I have solved the problem with the help of the custom plot function in PlotFcns option
zainab1996
zainab1996 2019 年 1 月 1 日
can you help with the code plz, i have the same problem

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by