搜索和轮询
除了轮询网格点之外,模式搜索算法还可以在每次迭代时执行一个可选步骤,称为搜索。在每次迭代中,搜索步骤都会将另一种优化方法应用于当前点。如果此次搜索不能改善当前点,则执行轮询步骤。运行此示例时,目标函数 lincontest7
可用。
使用轮询方法搜索
下面的例子说明了如何利用搜索方法解决使用 patternsearch 和 Optimize 实时编辑器任务进行约束最小化中描述的问题。在这种情况下,搜索方法是 GSS Positive Basis 2N 轮询。为了进行比较,首先运行不使用搜索方法的问题。
x0 = [2 1 0 9 1 0]; 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 9; 1 -1 2 -2 3 -3]; beq = [84 62 65 1]; options = optimoptions('patternsearch',... 'PlotFcn',{@psplotbestf,@psplotfuncount}); [x,fval,exitflag,output] = patternsearch(@lincontest7,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);
patternsearch stopped because the mesh size was less than options.MeshTolerance.
要使用 GSS Positive Basis 2N 轮询作为搜索方法,请更改 SearchFcn
选项。
rng default % For reproducibility options.SearchFcn = @GSSPositiveBasis2N; [x2,fval2,exitflag2,output2] = patternsearch(@lincontest7,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);
patternsearch stopped because the mesh size was less than options.MeshTolerance.
两种优化都达到了相同的目标函数值。使用搜索方法可以减少函数计算的次数和迭代的次数。
table([output.funccount;output2.funccount],[output.iterations;output2.iterations],... 'VariableNames',["Function Evaluations" "Iterations"],... 'RowNames',["Without Search" "With Search"])
ans=2×2 table
Function Evaluations Iterations
____________________ __________
Without Search 758 84
With Search 667 93
使用不同的求解器进行搜索
patternsearch
需要很长时间来最小化罗森布洛克函数。函数是
罗森布洛克函数在 使用优化实时编辑器任务或求解器的有约束非线性问题 中描述并绘制出来。罗森布洛克函数的最小值为 0,在点 [1,1]
处达到。因为 patternsearch
在最小化该函数时效率不高,所以使用不同的搜索方法来提供帮助。
创建目标函数。
dejong2fcn = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
两个变量的模式搜索的默认最大迭代次数为 200,函数计算的默认最大次数为 4000。将这些值增加到 MaxFunctionEvaluations
= 5000 和 MaxIterations
= 2000。
opts = optimoptions('patternsearch','MaxFunctionEvaluations',5000,'MaxIterations',2000);
从 [-1.9 2]
开始运行 patternsearch。
[x,feval,eflag,output] = patternsearch(dejong2fcn,...
[-1.9,2],[],[],[],[],[],[],[],opts);
patternsearch stopped because it exceeded options.MaxFunctionEvaluations.
disp(feval)
0.8560
disp(output.funccount)
5000
即使经过 5000 次函数计算后,优化仍未完成,因此结果不是很接近最佳值 0。
设置选项以使用 fminsearch
作为搜索方法,使用默认的函数计算和迭代次数。
opts = optimoptions('patternsearch',opts,'SearchFcn',@searchneldermead);
重新运行优化。
[x2,feval2,eflag2,output2] = patternsearch(dejong2fcn,...
[-1.9,2],[],[],[],[],[],[],[],opts);
patternsearch stopped because the mesh size was less than options.MeshTolerance.
disp(feval2)
4.0686e-10
disp(output2.funccount)
291
使用该搜索方法时,解处的目标函数值要好得多(更低),并且函数计算的次数要少得多。fminsearch
更有效地接近罗森布洛克函数的最小值。