Main Content

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

Optimization Toolbox의 출력 함수

출력 함수란?

특정 문제에서는 각 반복마다 최적화 알고리즘에서 출력값을 얻고자 할 수 있습니다. 예를 들어 알고리즘에 의해 계산된 점 시퀀스를 찾고 그러한 점을 플로팅하고자 할 수 있습니다. 이렇게 하려면 각 반복마다 최적화 함수가 호출하는 출력 함수를 생성하십시오. 관련 구문 등 자세한 내용은 Output Function and Plot Function Syntax 항목을 참조하십시오.

이 예제에서는 출력 함수에 솔버 기반 접근법을 사용합니다. 문제 기반 접근법에 대해서는 Output Function for Problem-Based Optimization 항목을 참조하십시오.

일반적으로 출력 함수를 사용하는 솔버는 비선형 함수를 입력값으로 받을 수 있습니다. 함수 도움말 페이지의 옵션 섹션에서 출력 함수를 사용할 수 있는 솔버를 확인할 수 있습니다.

출력 함수 사용하기

이 예제에서는 출력 함수를 사용하여, 제약 조건이 있는 비선형 최적화 문제를 풀기 위한 fmincon 풀이 과정을 모니터링하는 방법을 보여줍니다. 각 fmincon 반복 끝에서 출력 함수는 다음을 수행합니다.

  • 현재 점을 플로팅합니다.

  • 변수 history에 현재 점과 그에 대응하는 목적 함수 값을 저장하고, 변수 searchdir에 현재 탐색 방향을 저장합니다. 탐색 방향은 현재 점에서 다음 점으로의 방향을 가리키는 벡터입니다.

그 외에도 내역을 fmincon 함수 외부에서 사용할 수 있도록 하려면 fmincon을 호출하고 출력 함수 변수를 반환하는 중첩 함수 내에서 최적화를 수행하십시오. 이러한 정보를 전달하는 방법에 대한 자세한 내용은 추가 파라미터 전달하기 항목을 참조하십시오. 이 예제의 마지막 부분에 있는 runfmincon 헬퍼 함수에는 중첩 함수 호출이 포함되어 있습니다.

목적 함수 및 제약 조건 함수

다음 함수를 최소화하는 문제입니다.

f(x)=exp(x1)(4x12+2x22+4x1x2+2x2+1)

여기에는 다음과 같은 비선형 부등식 제약 조건이 적용됩니다.

x1+x2-x1x23/2x1x2-10.

runfmincon 내의 중첩된 objfun 함수는 목적 함수를 구현합니다. runfmincon 내의 중첩된 confun 함수는 제약 조건 함수를 구현합니다.

솔버 호출하기

문제에 대한 해를 구하고 fmincon 반복 내역을 확인하기 위해 runfmincon 함수를 호출합니다.

[xsol,fval,history,searchdir] = runfmincon;
                                Max     Line search  Directional  First-order 
 Iter F-count        f(x)   constraint   steplength   derivative   optimality Procedure 
    0      3       1.8394          0.5                                         Infeasible start point
    1      6      1.85127     -0.09197            1        0.109        0.778   
    2      9     0.300167         9.33            1       -0.117        0.313  Hessian modified twice  
    3     12     0.529835       0.9209            1         0.12        0.232   
    4     16     0.186965       -1.517          0.5       -0.224         0.13   
    5     19    0.0729085       0.3313            1       -0.121        0.054   
    6     22    0.0353323     -0.03303            1      -0.0542       0.0271   
    7     25    0.0235566     0.003184            1      -0.0271      0.00587   
    8     28    0.0235504    9.035e-08            1      -0.0146     8.51e-07   
Active inequalities (to within options.ConstraintTolerance = 1e-06):
  lower      upper     ineqlin   ineqnonlin
                                     1
                                     2

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the  value of the constraint tolerance.

Figure contains an axes object. The axes object with title Sequence of Points Computed by fmincon contains 18 objects of type line, text. One or more of the lines displays its values using only markers

출력 함수는 fmincon으로 값을 구한 점에 대한 플롯을 생성합니다. 각 점은 반복 횟수로 레이블이 지정됩니다. 최적의 점은 8번째 반복에서 발생합니다. 시퀀스에서 마지막 두 점은 너무 가까워서 겹칩니다.

출력값 history는 두 개의 필드를 포함한 구조체입니다.

disp(history)
       x: [9x2 double]
    fval: [9x1 double]

historyfval 필드에는 fmincon에 의해 계산된 점 시퀀스에 대응하는 목적 함수 값이 포함됩니다.

disp(history.fval)
    1.8394
    1.8513
    0.3002
    0.5298
    0.1870
    0.0729
    0.0353
    0.0236
    0.0236

이 값은 헤더가 f(x)인 열에 있는 반복 출력값에 동일하게 표시됩니다.

historyx 필드에는 fmincon에 의해 계산된 점 시퀀스가 포함됩니다.

disp(history.x)
   -1.0000    1.0000
   -1.3679    1.2500
   -5.5708    3.4699
   -4.8000    2.2752
   -6.7054    1.2618
   -8.0679    1.0186
   -9.0230    1.0532
   -9.5471    1.0471
   -9.5474    1.0474

searchdir 출력값에는 각 반복에서의 fmincon의 탐색 경로가 포함됩니다. 탐색 방향은 현재 반복에서 계산된 점에서 다음 반복에서 계산된 점으로 향하는 벡터입니다.

disp(searchdir)
   -0.3679    0.2500
   -4.2029    2.2199
    0.7708   -1.1947
   -3.8108   -2.0268
   -1.3625   -0.2432
   -0.9552    0.0346
   -0.5241   -0.0061
   -0.0003    0.0003

헬퍼 함수

다음 코드는 runfmincon 함수를 생성합니다. 이 함수는 outfun 출력 함수, objfun 목적 함수, confun 비선형 제약 조건 함수를 중첩 함수로 포함합니다.

function [xsol,fval,history,searchdir] = runfmincon
 
% Set up shared variables with outfun
history.x = [];
history.fval = [];
searchdir = [];
 
% Call optimization
x0 = [-1 1];
options = optimoptions(@fmincon,'OutputFcn',@outfun,... 
    'Display','iter','Algorithm','active-set');
[xsol,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);
 
 function stop = outfun(x,optimValues,state)
     stop = false;
 
     switch state
         case 'init'
             hold on
         case 'iter'
         % Concatenate current point and objective function
         % value with history. x must be a row vector.
           history.fval = [history.fval; optimValues.fval];
           history.x = [history.x; x];
         % Concatenate current search direction with 
         % searchdir.
           searchdir = [searchdir;... 
                        optimValues.searchdirection'];
           plot(x(1),x(2),'o');
         % Label points with iteration number and add title.
         % Add .15 to x(1) to separate label from plotted 'o'.
           text(x(1)+.15,x(2),... 
                num2str(optimValues.iteration));
           title('Sequence of Points Computed by fmincon');
         case 'done'
             hold off
         otherwise
     end
 end
 
 function f = objfun(x)
     f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) +... 
                    2*x(2) + 1);
 end
 
 function [c, ceq] = confun(x)
     % Nonlinear inequality constraints
     c = [1.5 + x(1)*x(2) - x(1) - x(2);
         -x(1)*x(2) - 10];
     % Nonlinear equality constraints
     ceq = [];
 end
end

관련 항목