Main Content

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

입자 군집을 사용하여 최적화하기

이 예제에서는 particleswarm 솔버를 사용하여 최적화하는 방법을 보여줍니다.

이 예제의 목적 함수는 De Jong의 5번째 함수로, 예제를 실행할 때 제공됩니다.

dejong5fcn

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

이 함수는 25개의 국소 최솟값을 가집니다.

디폴트 particleswarm 설정을 사용하여 함수의 최솟값을 구해 봅니다.

fun = @dejong5fcn;
nvars = 2;
rng default % For reproducibility
[x,fval,exitflag] = particleswarm(fun,nvars)
Optimization ended: relative change in the objective value 
over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
x = 1×2

  -31.9521  -16.0176

fval = 5.9288
exitflag = 1

x가 전역 최적해일까요? 이 시점에서는 불분명합니다. 함수 플롯을 보면 이 함수는 범위 [-50,50]의 성분에서 국소 최솟값을 가지고 있음을 알 수 있습니다. 따라서 변수의 범위를 [-50,50]로 제한하면 솔버가 전역 최솟값을 찾는 데 도움이 됩니다.

lb = [-50;-50];
ub = -lb;
[x,fval,exitflag] = particleswarm(fun,nvars,lb,ub)
Optimization ended: relative change in the objective value 
over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
x = 1×2

  -16.0079  -31.9697

fval = 1.9920
exitflag = 1

결과값이 유력해 보입니다. 새로운 해가 이전 해보다 더 낮은 fval을 갖습니다. 하지만 x가 실제로 전역해일까요? 영역을 더 잘 탐색하기 위해 더 많은 입자로 다시 최소화를 수행해 봅니다.

options = optimoptions('particleswarm','SwarmSize',100);
[x,fval,exitflag] = particleswarm(fun,nvars,lb,ub,options)
Optimization ended: relative change in the objective value 
over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
x = 1×2

  -31.9781  -31.9784

fval = 0.9980
exitflag = 1

이번 결과값이 더 유력해 보입니다. 하지만 이 답은 전역해가 맞을까요? 얼마나 정확할까요? 하이브리드 함수를 사용하여 솔버를 다시 실행합니다. particleswarm이 반복을 마친 후 particleswarm은 하이브리드 함수를 호출합니다.

options.HybridFcn = @fmincon;
[x,fval,exitflag] = particleswarm(fun,nvars,lb,ub,options)
Optimization ended: relative change in the objective value 
over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
x = 1×2

  -31.9783  -31.9784

fval = 0.9980
exitflag = 1

particleswarm이 위와 사실상 동일한 해를 찾았습니다. 따라서 particleswarm이 국소 최솟값을 보고하였으며 최종 x가 전역해라는 것을 어느 정도 신뢰할 수 있습니다.

관련 항목