主要内容

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

多维函数的替代优化

此示例展示了三个推荐求解器在最小化问题上的行为。目标函数是 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 维中对此函数的运行情况,而最大函数数量只有 200 个,这很有挑战性。

设置问题。

N = 20; % any even number
mf = 200; % 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.84977, 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: 774.8, 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: 493.704, 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 = 2.000000e+02.

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

table(fvalm,pfval,fmfval,'VariableNames',{'surrogateopt','patternsearch','fmincon'})
ans=1×3 table
    surrogateopt    patternsearch    fmincon
    ____________    _____________    _______

       8.8498           774.8         493.7 

再进行 200 次函数计算表明其他求解器可以快速接近真实解,而 surrogateopt 并没有显著改善。从先前的解重新启动求解器,这会为每次优化添加 200 个函数计算。

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: 8.37539, 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: 326.729, 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: 8.59887, 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 = 2.000000e+02.
table(fvalm,pfval,fmfval,'VariableNames',{'surrogateopt','patternsearch','fmincon'})
ans=1×3 table
    surrogateopt    patternsearch    fmincon
    ____________    _____________    _______

       8.3754          326.73        8.5989 

另请参阅

主题