このページは機械翻訳を使用して翻訳されました。最新版の英語を参照するには、ここをクリックします。
gamultiobj
のカスタム出力関数
この例では、20世代ごとに最適な目的関数の値を出力する gamultiobj
のカスタム出力関数を作成する方法を示します。ベクトル目的関数は 3 次元で、2 つの変数の関数です。
function F = fitnessfcn(x) F = [norm(x)^2,... 0.5*norm(x(:)-[2;-1])^2+2,... 2*norm(x(:)-[-2;-1])^2-1]; end
最適化の進行状況を監視するには、出力関数の構造 で説明されている構文を持つ出力関数を作成します。出力を適度に短く保つために、3 つの目的関数で見つかった最良 (最小) の値を 20世代ごとに出力します。
function [state,options,optchanged] = gamextent(options,state,flag) optchanged = false; switch flag case "init" disp("Best function values in each dimension for this generation") case "iter" if mod(state.Generation,20) == 1 % Suppress 95% of iterations. fprintf("Generation %-.0f\n",state.Generation) score = state.Score; nobj = size(score,2); [~,indx] = min(score); % Find indices of minimal objective function values. for i=1:nobj % Print scores for the smallest objective function values % in each dimension. fprintf("%-.3f ",score(indx(i),:)) fprintf("\n") end fprintf("\n") % Blank line before next output end end end
出力関数を呼び出すオプションを設定し、ソルバーを実行します。
opts = optimoptions("gamultiobj",OutputFcn=@gamextent); rng default % For reproducibility x = gamultiobj(@fitnessfcn,2,[],[],[],[],[],[],[],opts);
Best function values in each dimension for this generation Generation 1 2.568 9.068 10.845 4.085 2.078 30.132 3.478 7.478 0.240 Generation 21 0.001 4.446 9.073 5.135 2.001 31.589 4.983 9.955 -0.999 Generation 41 0.000 4.514 9.042 5.020 2.000 31.151 4.983 9.955 -0.999 Generation 61 0.000 4.508 9.061 5.020 2.000 31.151 4.943 9.964 -1.000 Generation 81 0.000 4.494 8.989 5.021 2.000 31.151 4.943 9.964 -1.000 Generation 101 0.000 4.497 9.003 5.017 2.000 31.026 5.003 9.989 -1.000 gamultiobj stopped because the average change in the spread of Pareto solutions is less than options.FunctionTolerance.
反復が進むにつれて、最初の目的関数の最適値は 0 に近づき、2 番目の目的関数の最適値は 2 に近づき、3 番目の目的関数の最適値は -1 に近づきます。このアプローチは、各世代の主対角線で確認できます。