혼합 정수 대리 최적화
이 예제에서는 정수 변수를 포함하는 최적화 문제를 푸는 방법을 보여줍니다. R2019b부터 surrogateopt는 정수 제약 조건을 받습니다. 이 예제에서는 10차원에서 –3부터 6까지의 정수 값 인수에 대해 multirosenbrock 함수를 최소화하는 점 x를 구합니다. multirosenbrock 함수는 스케일링이 좋지 않아 최적화하기 어려운 함수입니다. 이 함수는 점 [1,1,...,1]에서 구해지는 최솟값 0을 가집니다. multirosenbrock 함수의 코드는 이 예제의 마지막 부분에 나와 있습니다.
rng(1,"twister") % For reproducibility nvar = 10; % Any even number lb = -3*ones(1,nvar); ub = 6*ones(1,nvar); fun = @multirosenbrock; intcon = 1:nvar; % All integer variables [sol,fval] = surrogateopt(fun,lb,ub,intcon)

surrogateopt stopped because it exceeded the function evaluation limit set by 'options.MaxFunctionEvaluations'.
sol = 1×10
1 1 1 1 1 1 1 1 1 1
fval = 0
이 경우, surrogateopt는 해를 구합니다.
헬퍼 함수
다음 코드는 multirosenbrock 헬퍼 함수를 생성합니다.
function F = multirosenbrock(x) % This function is a multidimensional generalization of Rosenbrock's % function. It operates in a vectorized manner, assuming that x is a matrix % whose rows are the individuals. % Copyright 2014 by The MathWorks, Inc. N = size(x,2); % assumes x is a row vector or 2-D matrix if mod(N,2) % if N is odd error("Input rows must have an even number of elements") end odds = 1:2:N-1; evens = 2:2:N; F = zeros(size(x)); F(:,odds) = 1-x(:,odds); F(:,evens) = 10*(x(:,evens)-x(:,odds).^2); F = sum(F.^2,2); end