メインコンテンツ

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

gaを使ったRastrigins関数の最小化、問題ベース

この例では、問題ベースのアプローチで遺伝的アルゴリズムを使用して、複数の最小値を持つ関数を最小化する方法を示します。2 つの変数 xy の場合、Rastrigin 関数は次のように定義されます。

ras = @(x, y) 20 + x.^2 + y.^2 - 10*(cos(2*pi*x) + cos(2*pi*y));

各方向に 10 倍に拡大された関数をプロットします。

rf3 = @(x, y) ras(x/10, y/10);
fsurf(rf3,[-30 30],"ShowContours","on")
title("rastriginsfcn([x/10,y/10])")
xlabel("x")
ylabel("y")

Figure contains an axes object. The axes object with title rastriginsfcn([x/10,y/10]), xlabel x, ylabel y contains an object of type functionsurface.

この関数には多くの局所最小値と、x = 0、y = 0 で達成される 0 の大域的最小値があります。グローバル最適化とは何ですか?を参照してください。

最適化変数 xy を作成します。変数が ±100 によって制限されることを指定します。

x = optimvar("x","LowerBound",-100,"UpperBound",100);
y = optimvar("y","LowerBound",-100,"UpperBound",100);

目的関数 rastriginsfcn(x) を使用して最適化問題を作成します。

prob = optimproblem("Objective",ras(x,y));

メモ: 多項式、有理式、および初等関数 (exp など) で構成されていない非線形関数がある場合は、その関数を fcn2optimexpr を使用して最適化式に変換します。非線形関数から最適化式への変換最適化変数および式でサポートされる演算を参照してください。

gaplotbestf プロット関数を使用するには、ga オプションを作成します。

options = optimoptions("ga","PlotFcn","gaplotbestf");

ga をソルバーとして使用して問題を解きます。

rng default % For reproducibility
[sol,fval] = solve(prob,"Solver","ga","Options",options)
Solving problem using ga.
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Figure Genetic Algorithm contains an axes object. The axes object with title Best: 1.98992 Mean: 1.98992, xlabel Generation, ylabel Fitness value contains 2 objects of type scatter. These objects represent Best fitness, Mean fitness.

sol = struct with fields:
    x: 0.9950
    y: 0.9950

fval = 
1.9899

結果の関数値は最小値ですか?再度探索を実行してください。ga は確率的アルゴリズムであるため、結果が異なる場合があります。

[sol2,fval2] = solve(prob,"Solver","ga","Options",options)
Solving problem using ga.
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Figure Genetic Algorithm contains an axes object. The axes object with title Best: 0.994959 Mean: 0.99496, xlabel Generation, ylabel Fitness value contains 2 objects of type scatter. These objects represent Best fitness, Mean fitness.

sol2 = struct with fields:
    x: 0.9950
    y: -4.9289e-06

fval2 = 
0.9950

2 番目のソリューションの方が関数値が低いため、より優れています。ga によって返される解が大域解であるとは限りません。

参考

| |

トピック