Main Content

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

ga를 사용하여 전역 최적화와 국소 최적화 비교

전역 최솟값 찾기

때때로 최적화의 목표는 함수의 전역 최솟값 또는 전역 최댓값(탐색 공간의 다른 점에서 함수 값이 더 작거나 더 큰 점)을 찾는 것입니다. 그러나 최적화 알고리즘은 때때로 함수 값이 인근 점에서보다는 더 작지만 탐색 공간의 멀리 있는 점에서보다는 더 클 수도 있는 국소 최솟값을 반환합니다. 유전 알고리즘은 때때로 올바른 설정을 통해 이러한 결함을 극복할 수 있습니다.

예를 들어 다음과 같은 함수가 있다고 가정하겠습니다.

f(x)={-exp(-(x100)2)forx100,-exp(-1)+(x-100)(x-102)forx>100.

이 함수를 플로팅합니다.

t = -10:.1:103;
for ii = 1:length(t)
    y(ii) = two_min(t(ii));
end
plot(t,y)

Figure contains an axes object. The axes object contains an object of type line.

함수에는 2개의 국소 최솟값이 있습니다. 하나는 함수 값이 -1인 x = 0에 있고, 다른 하나는 함수 값이 1 1/ex = 101에 있습니다. 후자의 값이 작기 때문에 전역 최솟값은 x = 101에서 발생합니다.

디폴트 파라미터를 사용하여 ga 실행하기

two_min 헬퍼 함수의 코드는 이 예제의 마지막 부분에 있습니다. 디폴트 파라미터로 ga를 실행하여 two_min 함수를 최소화합니다. 각 반복에서 ga 모집단의 범위를 플로팅하려면 gaplot1drange 헬퍼 함수(이 예제의 마지막 부분에 포함)를 사용합니다.

rng default % For reproducibility
options = optimoptions('ga','PlotFcn',@gaplot1drange);
[x,fval] = ga(@two_min,1,[],[],[],[],[],[],[],options)
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 Range of Population, Mean, xlabel Generation contains an object of type errorbar.

x = -0.0688
fval = -1.0000

유전 알고리즘은 x = 0에서 국소 최솟값에 매우 가까운 점을 반환합니다. 참고로 모든 개체는 –60에서 60 사이에 있습니다. 모집단은 절대로 전역 최솟값 x = 101 근처의 점을 탐색하지 않습니다.

초기 범위 증가시키기

유전 알고리즘이 더 넓은 범위의 점을 탐색하도록, 즉 모집단의 다양성을 늘리는 한 가지 방법은 초기 범위를 늘리는 것입니다. 초기 범위는 점 x = 101을 포함할 필요는 없지만 x = 101 근처에서 개체를 생성할 수 있을 정도로 충분히 커야 합니다. InitialPopulationRange 옵션을 [-10;90]으로 설정하고 솔버를 반환합니다.

options.InitialPopulationRange = [-10;90];
[x,fval] = ga(@two_min,1,[],[],[],[],[],[],[],options)
ga stopped because it exceeded options.MaxGenerations.

Figure Genetic Algorithm contains an axes object. The axes object with title Range of Population, Mean, xlabel Generation contains an object of type errorbar.

x = 100.9783
fval = -1.3674

이번에는 사용자 지정 플롯에서 훨씬 더 광범위한 개체를 보여줍니다. 초반부터 101 근처에 개체가 있고, 모집단 평균은 101로 수렴하기 시작합니다.

헬퍼 함수

다음 코드는 two_min 헬퍼 함수를 생성합니다.

function y = two_min(x)
if x <= 100
    y = -exp(-(x/100)^2);
else
    y = -exp(-1) + (x-100)*(x-102);
end
end

다음 코드는 gaplot1drange 헬퍼 함수를 생성합니다.

function state = gaplot1drange(options,state,flag)
%gaplot1drange Plots the mean and the range of the population.
%   STATE = gaplot1drange(OPTIONS,STATE,FLAG) plots the mean and the range
%   (highest and the lowest) of individuals (1-D only).  
%
%   Example:
%   Create options that use gaplot1drange
%   as the plot function
%     options = optimoptions('ga','PlotFcn',@gaplot1drange);

%   Copyright 2012-2014 The MathWorks, Inc.

if isinf(options.MaxGenerations) || size(state.Population,2) > 1
    title('Plot Not Available','interp','none');
    return;
end
generation = state.Generation;
score = state.Population;
smean = mean(score);
Y = smean;
L = smean - min(score);
U = max(score) - smean;

switch flag

    case 'init'
        set(gca,'xlim',[1,options.MaxGenerations+1]);
        plotRange = errorbar(generation,Y,L,U);
        set(plotRange,'Tag','gaplot1drange');
        title('Range of Population, Mean','interp','none')
        xlabel('Generation','interp','none')
    case 'iter'
        plotRange = findobj(get(gca,'Children'),'Tag','gaplot1drange');
        newX = [get(plotRange,'Xdata') generation];
        newY = [get(plotRange,'Ydata') Y];
        newL = [get(plotRange,'Ldata') L];
        newU = [get(plotRange,'Udata') U];       
        set(plotRange,'Xdata',newX,'Ydata',newY,'Ldata',newL,'Udata',newU);
end
end

관련 항목