Main Content

图案搜索选项的效果

This example shows the effects of some options for pattern search. The options include plotting, stopping criteria, and other algorithmic controls for speeding a solution.

设置一个用于模式搜索的问题

The problem to minimize is a quadratic function of six variables subject to linear equality and inequality constraints. The objective function,lincontest7,全局优化工具箱包含。

typelincontest7
函数y = lincontest7(x);%lincontest7目标函数。%y = lincontest7(x)评估输入X的y。确保x是列%向量,而目标函数获得行向量。%版权2003-2004 The Mathworks,Inc。X = X';%根据H和F定义了二次问题(来自Web未知来源)H = [36 17 19 12 8 15;17 33 18 11 7 14;19 18 43 13 8 16;12 11 13 18 6 11;8 7 8 6 9 8;15 14 16 11 8 29]; f = [ 20 15 21 18 29 24 ]'; y = 0.5*x'*H*x + f'*x;

Specify the function handle@lincontest7作为目标函数。

Objectivefcn = @lincontest7;

len的目标函数接受一个行向量gth six. Specify an initial point for the optimization.

x0 = [2 1 0 9 1 0];

Create linear constraint matrices representing the constraintsAineq*x <= Bineqandaeq*x = beq。For details, seeLinear Constraints

aineq = [-8 7 3 -4 9 0];bineq = [7];AEQ = [7 1 8 3 3 3;5 0 5 1 5 8;2 6 7 1 1 8;1 0 0 0 0 0];beq = [84 62 65 1];

跑过patternsearchsolver, and note the number of iterations and function evaluations required to reach the solution.

[x1,fval,exitflag,output] = patternsearch(ObjectiveFCN,X0,Aineq,bineq,aeq,beq);
Optimization terminated: mesh size less than options.MeshTolerance.
fprintf('The number of iterations is: %d\n',,,,Output.iterations);
迭代次数为:150
fprintf('The number of function evaluations is: %d\n',output.funccount);
The number of function evaluations is: 934
fprintf('找到的最佳功能值是:%g \ n',,,,Fval);
找到的最佳功能值是:2189.18

Add Visualization

通过指定选择两个绘图函数的选项来监视优化过程。图函数psplotbestfplots the best objective function value at every iteration, and the plot functionpsplotfuncountplots the number of times the objective function is evaluated at each iteration. Set these two plot functions in a cell array.

opts = optimoptions(@patternsearch,'PlotFcn',,,,{@psplotbestf,@psplotfuncount});

跑过patternsearch求解器,包括选择争论。因为问题没有上限或下限的约束,也没有非线性约束,请传递空数组([])第七,第八和第九个论点。

[x1,fval,exitflag,output] = patternsearch(objectivefcn,x0,aineq,bineq,...Aeq,Beq,[],[],[],opts);
Optimization terminated: mesh size less than options.MeshTolerance.

图模式搜索包含2轴对象。斧s object 1 with title Best Function Value: 2189.18 contains an object of type line. Axes object 2 with title Total Function Evaluations: 934 contains an object of type line.

Mesh Options

模式搜索涉及评估网格中点的目标函数。网格的大小会影响溶液的速度。您可以使用选项控制网格的大小。

Initial Mesh Size

The mesh at each iteration is the span of a set of search directions that are added to the current point, scaled by the current mesh size. The solver starts with an initial mesh size of1默认。要启动初始网格大小为1/2,请设置InitialMeshSize选项。

选择= optimoptions(opts,“初始测量”,1/2);

网格缩放

You can scale the mesh to improve the minimization of a poorly scaled optimization problem. Scaling rotates the pattern by some degree and scales along the search directions. TheScaleMesh选项为ON(true)默认情况下,但是如果问题范围很好,您可以将其关闭。通常,如果问题的范围很差,则将此选项设置为truecan reduce the number of function evaluations. For this problem, setScaleMesh错误的, 因为lincontest7是一个尺度良好的目标函数。

选择= optimoptions(opts,'ScaleMesh',,,,错误的);

Mesh Accelerator

Direct search methods require many function evaluations compared to derivative-based optimization methods. The pattern search algorithm can quickly find the neighborhood of an optimum point, but can be slow in detecting the minimum itself. Thepatternsearchsolver can reduce the number of function evaluations by using an accelerator. When the accelerator is on (opts.acceleratemesh = true), the solver contracts the mesh size rapidly after reaching a minimum mesh size. This option is recommended only for smooth problems; in other types of problems, you can lose some accuracy. TheAccelerateMeshoption is off (错误的) by default. For this problem, setAccelerateMeshtrue因为目标函数是平滑的。

选择= optimoptions(opts,“ Acceleratemesh”,真的);

跑过patternsearch求解器。

[x2,fval,exitflag,output] = patternsearch(objectivefcn,x0,aineq,bineq,...Aeq,Beq,[],[],[],opts);
Optimization terminated: mesh size less than options.MeshTolerance.

图模式搜索包含2轴对象。斧s object 1 with title Best Function Value: 2189.18 contains an object of type line. Axes object 2 with title Total Function Evaluations: 604 contains an object of type line.

fprintf('The number of iterations is: %d\n',,,,Output.iterations);
The number of iterations is: 68
fprintf('The number of function evaluations is: %d\n',output.funccount);
The number of function evaluations is: 604
fprintf('找到的最佳功能值是:%g \ n',,,,Fval);
找到的最佳功能值是:2189.18

网格选项设置减少了迭代次数和功能评估的数量,并且没有明显的准确性丧失。

停止标准和公差

网状耐力是对网格尺寸的公差。如果网格尺寸小于网状耐力,求解器停止。steptolerance是对下一个点的当前点变化的最小公差。函数授体是从当前点到下一个点的功能值变化的最小公差。

设置网状耐力至1e-7, which is ten times smaller than the default value. This setting can increase the number of function evaluations and iterations, and can lead to a more accurate solution.

opts.meshtolerance = 1e-7;

模式搜索中的搜索方法

The pattern search algorithm can use an additional search method at every iteration, based on the value of theSearchFcn选项。当您使用搜索方法指定SearchFcn,,,,patternsearch在网格搜索之前先执行指定的搜索。如果搜索方法成功,patternsearch跳过该迭代的网格搜索,通常称为轮询功能。如果搜索方法在改善当前点方面没有成功,patternsearchperforms the mesh search.

You can specify different search methods forSearchFcn,,,,including搜索andsearchneldermead,,,,which are optimization algorithms. Use these two search methods only for the first iteration, which is the default setting. Using either of these methods at every iteration might not improve the results and can be computationally expensive. However, you can use the搜索method, which generates Latin hypercube points, at every iteration or possibly every 10 iterations.

搜索方法的其他选择包括诸如阳性基础n+1或阳性基础2N之类的轮询方法。推荐的策略是使用正基n+1(最多需要n+1点才能创建模式)作为搜索方法和正基相2N(需要2n点才能创建模式)作为一种轮询方法。

更新要使用的选项结构positivebasisnp1as the search method. Because positive basis 2N is the default for thePollFcn选项,请勿设置该选项。

opts.searchfcn = @stustrybasisnp1;

跑过patternsearch求解器。

[X5,Fval,ExitFlag,Output] = patternsearch(objectiveFcn,x0,Aineq,Bineq,Aeq,Beq,...[],[],[],opts);
Optimization terminated: change in X less than options.StepTolerance.

图模式搜索包含2轴对象。斧s object 1 with title Best Function Value: 2189.18 contains an object of type line. Axes object 2 with title Total Function Evaluations: 558 contains an object of type line.

fprintf('The number of iterations is: %d\n',,,,Output.iterations);
The number of iterations is: 44
fprintf('The number of function evaluations is: %d\n',output.funccount);
功能评估的数量为:558
fprintf('找到的最佳功能值是:%g \ n',,,,Fval);
找到的最佳功能值是:2189.18

The total number of iterations and function evaluations decreases, even though the mesh tolerance is smaller than its previous value and is the stopping criterion that halts the solver.

Related Topics