メインコンテンツ

このページは機械翻訳を使用して翻訳されました。最新版の英語を参照するには、ここをクリックします。

遺伝的アルゴリズムオプションの効果

この例では、遺伝的アルゴリズム関数 ga のいくつかのオプションの効果を示します。オプションを作成および変更するには、optimoptions 関数を使用します。

ga に問題を設定する

ga は遺伝的アルゴリズムを使用して関数の最小値を探索します。この例では、ga を使用して、2 つの変数の実数値関数である適応度関数shufcn を最小化します。

この例を実行すると含まれる plotobjective を呼び出して、範囲= [-2 2;-2 2] にわたって shufcn をプロットします。

plotobjective(@shufcn,[-2 2; -2 2]);

Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

ga ソルバーを使用するには、適応度関数と問題内の変数の数という少なくとも 2 つの入力引数を指定します。ga によって返される最初の 2 つの出力引数は、見つかった最適なポイントである x と、最適なポイントでの関数値である Fval です。3 番目の出力引数 exitFlag は、ga が停止した理由を示します。ga は、ソルバーのパフォーマンスに関する情報を含む 4 番目の引数 Output も返すことができます。

FitnessFunction = @shufcn;
numberOfVariables = 2;

ga ソルバーを実行します。

rng default % For reproducibility
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 124
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 5881
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.199

rng default コマンドを使用せずにこの例を実行すると、ga は確率的アルゴリズムであるため、結果が異なる場合があります。

遺伝的アルゴリズムの仕組み

遺伝的アルゴリズムは、母集団に適用される一連の演算子を使用して母集団に対して作用します。母集団とは、設計空間内の点の集合です。デフォルトでは、初期母集団はランダムに生成されます。アルゴリズムは、現在の世代の個体の適応度を使用して、母集団の次の世代を計算します。詳細は、遺伝的アルゴリズムの仕組みを参照してください。

視覚化を追加する

実行中のソルバーのパフォーマンスを視覚化するには、optimoptions を使用して 'PlotFcn' オプションを設定します。この場合、セル配列内の 2 つのプロット関数を選択します。各世代における母集団の最高スコアと平均スコアをプロットする gaplotbestf を設定します。また、満たされた停止基準の割合をプロットする gaplotstopping も設定します。

opts = optimoptions(@ga,'PlotFcn',{@gaplotbestf,@gaplotstopping});

opts 引数を含めて ga ソルバーを実行します。

[x,Fval,exitFlag,Output] = ...
    ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: -186.71 Mean: -123.753, xlabel Generation, ylabel Fitness value contains 2 objects of type scatter. These objects represent Best fitness, Mean fitness. Axes object 2 with title Stopping Criteria, xlabel Progress contains an object of type bar.

母集団オプションを指定する

母集団オプションはソルバーのパフォーマンスに大きな影響を与える可能性があります。各反復の速度は母集団のサイズに依存します。母集団が大きいほど反復は遅くなります。逆に言えば、母集団が多いほど、ga はより徹底的に探索することになり、より良い解決策につながる可能性があります。同様に、初期範囲を広くすると、より徹底した探索が可能になりますが、同様の徹底性で広い範囲を探索するには、より多くの母集団が必要になる可能性があります。

母集団規模を指定する

ga は、均一な乱数ジェネレータを使用してデフォルトの初期母集団を作成します。ga が使用するデフォルトの母集団サイズは、決定変数の数が 5 未満の場合は 50、それ以外の場合は 200 です。デフォルトのサイズは、一部の問題では適切に機能しない可能性があります。たとえば、小さな問題の場合は、母集団サイズを小さくしても十分な場合があります。現在の問題には変数が 2 つしかないため、母集団のサイズを 10 に指定します。既存のオプション opts で、オプション PopulationSize の値を 10 に設定します。

opts.PopulationSize = 10;

初期母集団範囲を指定する

初期母集団を生成するためのデフォルトの方法では、均一な乱数ジェネレータが使用されます。整数制約のない問題の場合、ga はすべてのポイントが -10 から 10 の範囲にある初期母集団を作成します。たとえば、次のコマンドを使用して、デフォルトの範囲内でサイズ 3 の母集団を生成できます。

Population = [-10,-10] + 20*rand(3,2);

InitialPopulationRange オプションを変更することで初期範囲を設定できます。範囲は2 行の行列である必要があります。範囲に列が 1 つしかない場合、つまり 2 行 1 列の場合、すべての変数の範囲は指定された範囲になります。たとえば、範囲を [-1; 1] に設定すると、両方の変数の初期範囲は -1 ~ 1 になります。変数ごとに異なる初期範囲を指定するには、2 行と numberOfVariables 列を持つ行列として範囲を指定する必要があります。たとえば、範囲を [-1 0; 1 2] に設定すると、最初の変数の範囲は -1 ~ 1 になり、2 番目の変数の範囲は 0 ~ 2 になります (各列が変数に対応します)。

既存のオプション opts 内のオプション InitialPopulationRange の値を変更します。

opts.InitialPopulationRange = [-1 0; 1 2];

ga ソルバーを実行します。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...
    [],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: -179.987 Mean: -78.6061, xlabel Generation, ylabel Fitness value contains 2 objects of type scatter. These objects represent Best fitness, Mean fitness. Axes object 2 with title Stopping Criteria, xlabel Progress contains an object of type bar.

fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 67
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 614
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -179.987

結果を再現する

デフォルトでは、ga は、MATLAB ® 乱数ジェネレータを使用して作成されたランダムな初期母集団から開始されます。ソルバーは、同じ乱数ジェネレータを使用する ga 演算子を使用して、次の世代を生成します。乱数が生成されるたびに、乱数ジェネレータの状態が変化します。したがって、オプションを変更しない場合でも、ソルバーを再度実行すると異なる結果が得られる可能性があります。

この現象を確認するには、ソルバーを 2 回実行します。

ga ソルバーを実行します。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.484

ga をもう一度実行します。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -185.867

ga は、乱数ジェネレーターの状態が実行ごとに変化するため、2 回の実行で異なる結果を生成します。

ga を実行する前に結果を再現したい場合は、乱数ストリームの状態を保存できます。

thestate = rng;

ga を実行します。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467

ストリームをリセットして、ga を再実行します。結果は前回の実行と同一です。

rng(thestate);
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467

結果の再現を指定する前に ga を実行すると、output 構造があれば乱数ジェネレーターをリセットできます。

strm = RandStream.getGlobalStream;
strm.State = Output.rngstate.State;

gaを再実行します。再び、結果は同じです。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467

停止基準の変更

ga は、ソルバーを停止するタイミングを決定するために 4 つの異なる基準を使用します。ga は、最大世代数に達すると停止します。デフォルトでは、この数は変数の数の 100 倍です。ga は、最適な適応度が秒単位で指定された時間 (ストール時間制限)、または世代数 (最大ストール世代) にわたって変化しないかどうかも検出します。もう 1 つの基準は、秒単位での最大時間制限です。停止基準を変更して、最大世代数を 300 に、最大ストール世代を 100 に増やします。

opts = optimoptions(opts,'MaxGenerations',300,'MaxStallGenerations', 100);

ga ソルバーを再実行します。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...
    [],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: -186.729 Mean: -186.202, xlabel Generation, ylabel Fitness value contains 2 objects of type scatter. These objects represent Best fitness, Mean fitness. Axes object 2 with title Stopping Criteria, xlabel Progress contains an object of type bar.

fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 299
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 2702
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.729

ga演算子を指定する

ga は、母集団内のランダムな点の集合から開始し、演算子を使用して母集団の次の世代を生成します。さまざまな演算子としては、スケーリング、選択、交叉、突然変異があります。ツールボックスには、各演算子に指定するいくつかの関数が用意されています。FitnessScalingFcn の場合は fitscalingpropSelectionFcn の場合は selectiontournament を指定します。

opts = optimoptions(@ga,'SelectionFcn',@selectiontournament, ...
                        'FitnessScalingFcn',@fitscalingprop);

gaを再実行します。

[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...
    [],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 52
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 2497
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.417

指定された演算子に基づいて、最適な関数値が向上したり、悪化したりする可能性があります。多くの場合、さまざまな演算子を試してみることが、どの演算子セットが問題に最も適しているかを判断する最善の方法です。

参考

トピック