主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

多维函数的替代优化

R2026a

此示例展示了三个推荐求解器在最小化问题上的行为。目标函数是 multirosenbrock 函数:

type 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);

multirosenbrock 函数在点 [1,1,...,1] 处有一个局部最小值 0。看看针对一般非线性问题的三个最佳求解器在 20 维中对此函数的运行情况,而最大函数数量只有 300 个,这很有挑战性。

设置问题。

N = 20; % Any even number
mf = 300; % max fun evals
fun = @multirosenbrock;
lb = -3*ones(1,N);
ub = -lb;
rng default
x0 = -3*rand(1,N);

设置 surrogateopt 的选项以仅使用 200 个函数计算,然后运行求解器。

options = optimoptions("surrogateopt",MaxFunctionEvaluations=mf);
[xm,fvalm,~,~,pop] = surrogateopt(fun,lb,ub,options);

Figure Optimization Plot Function contains an axes object. The axes object with title Best Function Value: 8.68202, xlabel Iteration, ylabel Function value contains an object of type scatter. This object represents Best function value.

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.

为 patternsearch 设置类似的选项,包括一个绘图函数来监控优化。

psopts = optimoptions("patternsearch",PlotFcn="psplotbestf",MaxFunctionEvaluations=mf);
[psol,pfval] = patternsearch(fun,x0,[],[],[],[],lb,ub,[],psopts);
patternsearch stopped because it exceeded options.MaxFunctionEvaluations.

Figure Pattern Search contains an axes object. The axes object with title Best Function Value: 539.571, xlabel Iteration, ylabel Function value contains an object of type scatter.

为 fmincon 设置类似的选项。

opts = optimoptions("fmincon",PlotFcn="optimplotfval",MaxFunctionEvaluations=mf);
[fmsol,fmfval,eflag,fmoutput] = fmincon(fun,x0,[],[],[],[],lb,ub,[],opts);

Figure Optimization Plot Function contains an axes object. The axes object with title Current Function Value: 99.8691, xlabel Iteration, ylabel Function value contains an object of type scatter.

Solver stopped prematurely.

fmincon stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 3.000000e+02.

对于这个极其有限的函数计算次数,surrogateopt 解最接近 0 的真实最小值。

table(fvalm,pfval,fmfval,VariableNames=["surrogateopt","patternsearch","fmincon"])
ans=1×3 table
    surrogateopt    patternsearch    fmincon
    ____________    _____________    _______

       8.682           539.57        99.869 

再进行 300 次函数计算后可以发现,其他求解器在向真实解靠拢方面取得了良好进展,而 surrogateopt 则没有显著改善。从先前的解重新启动求解器,这会为每次优化添加 300 个函数计算。

options = optimoptions(options,InitialPoints=pop);
[xm,fvalm,~,~,pop] = surrogateopt(fun,lb,ub,options);

Figure Optimization Plot Function contains an axes object. The axes object with title Best Function Value: 7.89806, xlabel Iteration, ylabel Function value contains an object of type scatter. This object represents Best function value.

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.
[psol,pfval] = patternsearch(fun,psol,[],[],[],[],lb,ub,[],psopts);
patternsearch stopped because it exceeded options.MaxFunctionEvaluations.

Figure Pattern Search contains an axes object. The axes object with title Best Function Value: 215.99, xlabel Iteration, ylabel Function value contains an object of type scatter.

[fmsol,fmfval,eflag,fmoutput] = fmincon(fun,fmsol,[],[],[],[],lb,ub,[],opts);

Figure Optimization Plot Function contains an axes object. The axes object with title Current Function Value: 9.44012, xlabel Iteration, ylabel Function value contains an object of type scatter.

Solver stopped prematurely.

fmincon stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 3.000000e+02.
table(fvalm,pfval,fmfval,VariableNames=["surrogateopt","patternsearch","fmincon"])
ans=1×3 table
    surrogateopt    patternsearch    fmincon
    ____________    _____________    _______

       7.8981          215.99        9.4401 

另请参阅

主题