メインコンテンツ

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

遺伝的アルゴリズムにおけるハイブリッド方式

この例では、遺伝的アルゴリズムと別の最適化手法を使用して関数を最適化するためのハイブリッド スキームの使用方法を示します。ga は局所的最小値の近傍にすぐに到達できますが、収束を達成するには多くの関数評価が必要になる場合があります。解決プロセスを高速化するには、まず ga を少数の世代にわたって実行し、最適なポイントに近づきます。次に、ga からのソリューションを別の最適化ソルバーの初期点として使用し、より高速で効率的な局所的探索を実行します。

Rosenbrock 関数

この例では、Rosenbrock 関数 (Dejong の 2 番目の関数とも呼ばれる)を適応度関数として使用します。

f(x)=100(x(2)-x(1)2)2+(1-x(1))2.

ローゼンブロック関数は、この関数を最小化しようとするとほとんどの方法で収束が遅くなるため、最適化においては悪名高い関数です。ローゼンブロック関数は、点 x* = (1,1) で唯一の最小値を持ち、関数値は f(x*)=0 になります。

Rosenbrock 関数のコードは dejong2fcn ファイルにあります。

type dejong2fcn.m
function scores = dejong2fcn(pop)
%DEJONG2FCN Compute DeJongs second function.
%This function is also known as Rosenbrock's function

%   Copyright 2003-2004 The MathWorks, Inc.

scores = zeros(size(pop,1),1);
for i = 1:size(pop,1)
    p = pop(i,:);
    scores(i) = 100 * (p(1)^2 - p(2)) ^2 + (1 - p(1))^2;
end

ローゼンブロック関数を -2 <= x(1) <= 2; -2 <= x(2) <=2 の範囲でプロットします。

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

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

遺伝的アルゴリズムによるソリューション

まず、ga のみを使用して、Rosenbrock 関数の最小値を見つけます。

FitnessFcn = @dejong2fcn;
numberOfVariables = 2;

最適化プロセスを監視するためのプロット関数を含めます。

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

再現性のために乱数ストリームを設定し、オプションを使用して ga を実行します。

rng default
[x,fval] = ga(FitnessFcn,numberOfVariables,[],[],[],[],[],[],[],options)
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: 0.491302 Mean: 664119, 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.

x = 1×2

    0.3454    0.1444

fval = 
0.4913

デフォルトの停止基準を使用すると、ga はあまり正確なソリューションを提供しません。停止基準を変更してより正確な解を求めることもできますが、ga では、グローバル最適値 x* = (1,1) に近づくために多くの関数評価が必要になります。

代わりに、ga のハイブリッド関数オプションを使用して、ga が停止する場所から開始するより効率的な局所的探索を実行します。

ハイブリッド関数の追加

ga が停止した時点からハイブリッド関数が始まります。ハイブリッド関数の選択肢は fminsearchpatternsearch、または fminunc です。この最適化の例は滑らかで制約がないため、ハイブリッド関数として fminunc を使用します。ハイブリッド関数を指定するときに、追加の引数としてプロット オプションを fminunc に提供します。

fminuncOptions = optimoptions(@fminunc,'PlotFcn',{'optimplotfval','optimplotx'});
options = optimoptions(options,'HybridFcn',{@fminunc, fminuncOptions});

ハイブリッド関数として fminunc を使用して、ga を再度実行します。

[x,fval,exitflag,output] = ga(FitnessFcn,numberOfVariables,[],[],[],[],[],[],[],options)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Figure Optimization Plot Function contains 2 axes objects. Axes object 1 with title Current Function Value: 1.72147e-11, xlabel Iteration, ylabel Function value contains an object of type scatter. Axes object 2 with title Current Point, xlabel Variable number, ylabel Current point contains an object of type bar.

Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.

<stopping criteria details>

Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: 0.664192 Mean: 4.61776e+06, 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.

x = 1×2

    1.0000    1.0000

fval = 
1.7215e-11
exitflag = 
1
output = struct with fields:
      problemtype: 'unconstrained'
         rngstate: [1×1 struct]
      generations: 51
        funccount: 2534
          message: 'ga stopped because the average change in the fitness value is less than options.FunctionTolerance.↵FMINUNC: Local minimum found.↵↵Optimization completed because the size of the gradient is less than↵the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 2.473362e-07, is less ↵than options.OptimalityTolerance = 1.000000e-06.↵'
    maxconstraint: []
       hybridflag: 1

ga プロットは、各世代における母集団の最良値と平均値を示します。プロット タイトルは、ga が停止したときに検出された最良の値を示します。ハイブリッド関数fminunc は、ga によって見つかった最適なポイントから始まります。fminunc プロットは、gafminunc を一緒に使用して得られるソリューション xfval を示しています。この場合、ハイブリッド関数を使用すると、ソリューションの精度と効率が向上します。output.hybridflag フィールドは、fminunc が終了フラグ 1 で停止し、x が真の局所的最小値であることを示しています。

参考

トピック