混合整数替代优化
此示例说明如何解决涉及整数变量的优化问题。从 R2019b 开始,surrogateopt
接受整数约束。在此示例中,找到点 x
,使 multirosenbrock
函数在十维空间中从 -3 到 6 的整数值参量上最小化。multirosenbrock
函数是一个缩放较差且难以优化的函数。它的最小值为 0,在点 [1,1,...,1]
处达到。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