Main Content

求解成功后

当求解器成功求解时,可能会存在什么问题?

求解器可能报告最小化成功,但给出的解未必正确。举一个比较简单的示例 - 在 x 位于 - 2 和 2 之间时最小化函数 f(x) = x3,从点 1/3 开始求解:

options = optimoptions('fmincon','Algorithm','active-set');
ffun = @(x)x^3;
xfinal = fmincon(ffun,1/3,[],[],[],[],-2,2,[],options)         

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is
non-decreasing in feasible directions, to within the default  
valueof the function tolerance, and constraints were satisfied 
to within the default value of the constraint tolerance.

No active inequalities.

xfinal =
 -1.5056e-008

真正的最小值出现在 x = -2 处。fmincon 给出以上报告,是因为函数 f(x) 在 x = 0 附近非常平坦。

另一个常见问题是求解器找到局部最小值,但您可能需要全局最小值。有关详细信息,请参阅局部最优与全局最优

经验提示:请检查您的结果,即使求解器报告它“找到”局部最小值,或方程“已求解”。

本节给出了验证结果的方法。

1.更改初始点

初始点对求解有很大影响。如果您从不同初始点获得的解和原来的解一样或比它更差,您会对原来的解更有信心。

例如,从 1/4 点开始最小化 f(x) = x3 + x4

ffun = @(x)x^3 + x^4;
options = optimoptions('fminunc','Algorithm','quasi-newton');
[xfinal fval] = fminunc(ffun,1/4,options)

Local minimum found.

Optimization completed because the size of the gradient 
is less than the default value of the function tolerance.

x =
 -1.6764e-008

fval =
 -4.7111e-024

稍微更改一下初始点,求解器会找到更好的解:

[xfinal fval] = fminunc(ffun,1/4+.001,options)

Local minimum found.

Optimization completed because the size of the gradient 
is less than the default value of the function tolerance.

xfinal =
   -0.7500

fval =
   -0.1055

x = -0.75 是全局解;从其他点开始无法改进解。

有关详细信息,请参阅局部最优与全局最优

2.检查附近的点

要查看是否有比给出的解更好的值,请在附近各点处计算您的目标函数和约束。

例如,使用 当求解器成功求解时,可能会存在什么问题? 中的目标函数 ffun 以及终点 xfinal = -1.5056e-008,针对某个 Δ 计算 ffun(xfinal±Δ)

delta = .1;
[ffun(xfinal),ffun(xfinal+delta),ffun(xfinal-delta)]

ans =
   -0.0000    0.0011   -0.0009

目标函数在 ffun(xfinal-Δ) 处较低,因此求解器给出的解不正确。

再举一个复杂一点的示例:

options = optimoptions(@fmincon,'Algorithm','active-set');
lb = [0,-1]; ub = [1,1];
ffun = @(x)(x(1)-(x(1)-x(2))^2);
[x fval exitflag] = fmincon(ffun,[1/2 1/3],[],[],[],[],...
                           lb,ub,[],options)

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is
non-decreasing in feasible directions, to within the default  
valueof the function tolerance, and constraints were satisfied 
to within the default value of the constraint tolerance.

Active inequalities (to within options.ConstraintTolerance = 1e-006):
  lower      upper     ineqlin   ineqnonlin
    1                                 

x =
  1.0e-007 *
         0    0.1614

fval =
 -2.6059e-016

exitflag =
     1

在附近的可行点计算 ffun 表明解 x 不是真正的最小值:

[ffun([0,.001]),ffun([0,-.001]),...
    ffun([.001,-.001]),ffun([.001,.001])]

ans =
  1.0e-003 *
   -0.0010   -0.0010    0.9960    1.0000

列出的前两个值小于已计算出的最小值 fval

如果您拥有 Global Optimization Toolbox 许可证,则可以使用 patternsearch (Global Optimization Toolbox) 函数来检查附近的点。

3.检查目标函数和约束函数

仔细检查您的目标函数和约束函数,确保它们适用于您要求解的问题。建议:

  • 在几个点上检查您的目标函数的计算。

  • 检查每个不等式约束的符号是否正确。

  • 如果您执行了最大化,请记住要取所得解的负值。(此建议假设您通过最小化目标的负值来最大化函数。)例如,要最大化 f(x) = x – x2,请最小化 g(x) = –x + x2

    options = optimoptions('fminunc','Algorithm','quasi-newton');
    [x fval] = fminunc(@(x)-x+x^2,0,options)
    
    Local minimum found.
    
    Optimization completed because the size of the gradient is 
    less than the default value of the function tolerance.
    
    x =
        0.5000
    
    fval =
       -0.2500

    f 的最大值为 0.25,它是 fval 的负数。

  • 检查以确保不可行点不会导致函数中出现错误;请参阅迭代可能违反约束