ga関数(遺伝的アルゴリズム)の最適化計算で、全ての世代を保存するにはどうすればよいですか?
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 22 Jul 2016
Edited: MathWorks Support Team
on 22 Sep 2021
Genetic Algorithm(GA, 遺伝的アルゴリズム)を用いて、最適化計算を行っています。このときの経過を変数として出力する方法を教えてください。
例えば、PlotFcns コールバックで @gaplotbestf を設定すると、ベスト適応度(BestFit) をグラフに表示することができますが、この値を配列として得る方法を教えてください。
Accepted Answer
MathWorks Support Team
on 22 Sep 2021
Edited: MathWorks Support Team
on 22 Sep 2021
最適化計算での各世代での経過を変数として出力するには、OutputFcn(optimoptions 関数で設定する場合)、もしくは OutputFcns(gaoptimset 関数で設定する場合) にて、ユーザ側で、得たい値を出力するコールバックを定義する必要があります。
各種情報は、OutputFcn(s) の第2入力引数である state の中に格納されています。
以下は、ベスト適応度を出力させる OutputFcn(s) の記述方法です。
function [state,options,optchanged] = myfun(options,state,flag)
% OutputFcn sample
persistent BestF
optchanged = false;
switch flag
case 'init'
BestF(1) = min(state.Score);
case 'iter'
BestF = [BestF, min(state.Score)];
case 'done'
assignin('base','BestF',BestF)
end
ポイントとしては、各イタレーションで、 BestF(ベスト適応度)を継承させるために persistent 変数として定義しています。
また、最適化計算終了時、ベースワークスペースに変数を格納するため、 assignin 関数を使用しています。
また、OutputFcn 以外の手段としては、PlotFcns コールバックの@gaplotbestf で描画した結果からデータを取得する方法があります。
例えば、表示された Figure から取得する場合、 以下のコードでご確認いただけます。
h = findobj(gcf, 'Type','line','Tag', 'gaplotbestf');
h.YData
0 Comments
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!