主要内容

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

使用基于求解器的模式搜索进行约束最小化

此示例说明如何使用模式搜索最小化受非线性不等式约束和边界的目标函数。有关此示例的基于问题的版本,请参阅 基于问题的模式搜索约束最小化

约束最小化问题

对于这个问题,要最小化的目标函数是二维变量 x 的简单函数。

simple_objective(x) = (4 - 2.1*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-4 + 4*x(2)^2)*x(2)^2;

该函数被称为 "cam",如 LCW Dixon 和 GP Szego [1] 所述。

此外,该问题具有非线性约束和边界。

   x(1)*x(2) + x(1) - x(2) + 1.5 <= 0  (nonlinear constraint)
   10 - x(1)*x(2) <= 0                 (nonlinear constraint)
   0 <= x(1) <= 1                      (bound)
   0 <= x(2) <= 13                     (bound)

编写目标函数代码

创建一个名为 simple_objective.m 的 MATLAB® 文件,其中包含以下代码:

type simple_objective
function y = simple_objective(x)
%SIMPLE_OBJECTIVE Objective function for PATTERNSEARCH solver

%   Copyright 2004 The MathWorks, Inc.  

x1 = x(1);
x2 = x(2);
y = (4-2.1.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-4+4.*x2.^2).*x2.^2;

诸如 patternsearch 之类的求解器接受单个输入 x,其中 x 的元素数量与问题中的变量数量一样多。目标函数计算目标函数的标量值并在其单个输出参量 y 中返回它。

编写约束函数

创建一个名为 simple_constraint.m 的 MATLAB 文件,其中包含以下代码:

type simple_constraint
function [c, ceq] = simple_constraint(x)
%SIMPLE_CONSTRAINT Nonlinear inequality constraints.

%   Copyright 2005-2007 The MathWorks, Inc.

c = [1.5 + x(1)*x(2) + x(1) - x(2); 
     -x(1)*x(2) + 10];

% No nonlinear equality constraints:
ceq = [];

约束函数计算所有不等式和等式约束的值,并分别返回向量 cceqc 的值表示求解器试图使其小于或等于零的非线性不等式约束。ceq 的值表示求解器试图使其等于零的非线性等式约束。这个例子没有非线性等式约束,所以 ceq = []。有关详细信息,请参阅非线性约束

尽量减少使用 patternsearch

将目标函数指定为函数句柄。

ObjectiveFunction = @simple_objective;

指定问题边界。

lb = [0 0];   % Lower bounds
ub = [1 13];  % Upper bounds

将非线性约束函数指定为函数句柄。

ConstraintFunction = @simple_constraint;

为求解器指定一个初始点。

x0 = [0.5 0.5];   % Starting point

调用求解器,请求最优点 x 和最优点 fval 处的函数值。

[x,fval] = patternsearch(ObjectiveFunction,x0,[],[],[],[],lb,ub, ...
    ConstraintFunction)
Optimization finished: mesh size less than options.MeshTolerance 
and constraint violation is less than options.ConstraintTolerance.
x = 1×2

    0.8122   12.3122

fval = 
9.1324e+04

添加可视化

要观察求解器的进度,请指定选择两个绘图函数的选项。绘图函数 psplotbestf 绘制每次迭代中的最佳目标函数值,绘图函数 psplotmaxconstr 绘制每次迭代中的最大约束违反。在元胞数组中设置这两个绘图函数。另外,通过将 Display 选项设置为 'iter',在命令窗口中显示有关求解器进度的信息。

options = optimoptions(@patternsearch,'PlotFcn',{@psplotbestf,@psplotmaxconstr}, ...
                                      'Display','iter');

运行求解器,包括 options 参量。

[x,fval] = patternsearch(ObjectiveFunction,x0,[],[],[],[],lb,ub, ...
    ConstraintFunction,options)
                                      Max
  Iter   Func-count       f(x)      Constraint   MeshSize      Method
    0         1     0.373958         9.75       0.9086    
    1        18       113581    1.617e-10        0.001   Increase penalty
    2       147        92267            0        1e-05   Increase penalty
    3       373      91333.2            0        1e-07   Increase penalty
    4       638        91324            0        1e-09   Increase penalty
Optimization finished: mesh size less than options.MeshTolerance 
and constraint violation is less than options.ConstraintTolerance.

Figure Pattern Search contains 2 axes objects. Axes object 1 with title Best Function Value: 91324, xlabel Iteration, ylabel Function value contains an object of type scatter. Axes object 2 with title Maximum Constraint Violation: 0, xlabel Iteration, ylabel Constraint violation contains an object of type scatter.

x = 1×2

    0.8122   12.3122

fval = 
9.1324e+04

非线性约束导致 patternsearch 在每次迭代时解决许多子问题。从绘图和迭代显示可以看出,解过程只有几次迭代。但是,迭代显示中的 Func-count 列显示每次迭代有许多函数计算。绘图和迭代显示均表明初始点是不可行的,并且目标函数在初始点处较低。在解过程中,目标函数值先增大,然后减小,直至最终值。

参考资料

[1] Dixon, L. C. W., and G .P. Szego (eds.).Towards Global Optimisation 2.North-Holland:Elsevier Science Ltd., Amsterdam, 1978.

另请参阅

主题