主要内容

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

运行求解器

R2026a

通过调用运行进行优化

对于 GlobalSearch 和 MultiStart 来说,运行求解器几乎相同。语法上唯一的区别是 MultiStart 接受了描述起点的额外输入。

例如,假设您想找到 sixmin 函数的几个局部极小值

sixmin = 4 x2 – 2.1 x4 + x6/3 + xy – 4 y2 + 4 y4。

[X,Y] = meshgrid(-2:.1:2,-1:.05:1);
Z = 4*X.^2 - 2.1*X.^4 + X.^6./3 + X.*Y - 4*Y.^2 + 4*Y.^4;
surf(X,Y,Z);view(-44,14)
xlabel("x");ylabel("y")
title("sixmin(x,y)")

该函数也称为六峰驼峰函数[3]。所有局部极小值都位于区域 –3 ≤ x,y ≤ 3 内。

使用 GlobalSearch 运行的示例

要使用 GlobalSearch 查找 sixmin 函数的几个局部极小值,请输入:

% % Set the random stream to get exactly the same output
% rng(14,"twister")
gs = GlobalSearch;
opts = optimoptions(@fmincon,Algorithm="interior-point");
sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ...
    + x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);
problem = createOptimProblem("fmincon",x0=[-1,2],...
    objective=sixmin,lb=[-3,-3],ub=[3,3],...
    options=opts);
[xming,fming,flagg,outptg,manyminsg] = run(gs,problem);

运行的输出(根据随机种子而变化):

xming,fming,flagg,outptg,manyminsg
xming =
    0.0898   -0.7127

fming =
   -1.0316

flagg =
     1

outptg =

  struct with fields:

                funcCount: 2115
         localSolverTotal: 3
       localSolverSuccess: 3
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: 'GlobalSearch stopped because it analyzed all the trial po...'

manyminsg = 
  1x2 GlobalOptimSolution array with properties:

    X
    Fval
    Exitflag
    Output
    X0

使用 MultiStart 的运行的示例

要使用 50 次 fmincon 和 MultiStart 运行来查找 sixmin 函数的几个局部极小值,请输入:

% % Set the random stream to get exactly the same output
% rng(14,"twister")
ms = MultiStart;
opts = optimoptions(@fmincon,Algorithm="interior-point");
sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ...
    + x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);
problem = createOptimProblem("fmincon",x0=[-1,2],...
    objective=sixmin,lb=[-3,-3],ub=[3,3],...
    options=opts);
[xminm,fminm,flagm,outptm,manyminsm] = run(ms,problem,50);

运行的输出(根据随机种子而变化):

xminm,fminm,flagm,outptm,manyminsm
xminm =
    0.0898    -0.7127

fminm =
   -1.0316

flagm =
     1

outptm =

  struct with fields:

                funcCount: 2034
         localSolverTotal: 50
       localSolverSuccess: 50
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: 'MultiStart completed the runs from all start points.…'

manyminsm = 
  1x6 GlobalOptimSolution array with properties:

    X
    Fval
    Exitflag
    Output
    X0

在这种情况下,MultiStart 位于所有六个局部极小值,而 GlobalSearch 位于两个。有关 MultiStart 解的图片,请请参阅可视化吸引域。

另请参阅

主题