Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

ga를 사용하여 Rastrigin 함수 최소화하기(문제 기반)

이 예제는 복수 최솟값을 갖는 함수를 문제 기반 접근법으로 유전 알고리즘을 사용하여 최소화하는 방법을 보여줍니다. 두 변수 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을 가집니다. What Is Global Optimization? 항목을 참조하십시오.

최적화 변수 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을 사용하여 해당 함수를 최적화 표현식으로 변환합니다. Convert Nonlinear Function to Optimization Expression 항목과 Supported Operations for Optimization Variables and Expressions 항목을 참조하십시오.

ga 옵션을 만들어 gaplotbestf 플롯 함수를 사용합니다.

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

두 번째가 더 낮은 함수 값을 가지므로 더 나은 해입니다. ga에서 반환하는 해가 전역해라는 보장은 없습니다.

참고 항목

| |

관련 항목