search and poll -凯发k8网页登录
in addition to polling the mesh points, the pattern search algorithm can perform an optional step at every iteration, called search. at each iteration, the search step applies another optimization method to the current point. if this search does not improve the current point, the poll step is performed. the objective function, lincontest7
, is available when you run this example.
search using a poll method
the following example illustrates the use of a search method on the problem described in . in this case, the search method is the gss positive basis 2n poll. for comparison, first run the problem without a search method.
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);
optimization terminated: mesh size less than options.meshtolerance.
to use the gss positive basis 2n poll as a search method, change the searchfcn
option.
rng default % for reproducibility options.searchfcn = @gsspositivebasis2n; [x2,fval2,exitflag2,output2] = patternsearch(@lincontest7,x0,... aineq,bineq,aeq,beq,[],[],[],options);
optimization terminated: mesh size less than options.meshtolerance.
both optimizations reached the same objective function value. using the search method reduces the number of function evaluations and the number of iterations.
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
search using a different solver
patternsearch
takes a long time to minimize rosenbrock's function. the function is
rosenbrock's function is described and plotted in constrained nonlinear problem using optimize live editor task or solver. the minimum of rosenbrock's function is 0, attained at the point [1,1]
. because patternsearch
is not efficient at minimizing this function, use a different search method to help.
create the objective function.
dejong2fcn = @(x)100*(x(2)-x(1)^2)^2 (1-x(1))^2;
the default maximum number of iterations for patternsearch with two variables is 200, and the default maximum number of function evaluations is 4000. increase these values to maxfunctionevaluations
= 5000 and maxiterations
= 2000.
opts = optimoptions('patternsearch','maxfunctionevaluations',5000,'maxiterations',2000);
run patternsearch starting from [-1.9 2]
.
[x,feval,eflag,output] = patternsearch(dejong2fcn,...
[-1.9,2],[],[],[],[],[],[],[],opts);
maximum number of function evaluations exceeded: increase options.maxfunctionevaluations.
disp(feval)
0.8560
disp(output.funccount)
5000
the optimization did not complete even after 5000 function evaluations, and so the result is not very close to the optimal value of 0.
set the options to use fminsearch
as the search method, using the default number of function evaluations and iterations.
opts = optimoptions('patternsearch',opts,'searchfcn',@searchneldermead);
rerun the optimization.
[x2,feval2,eflag2,output2] = patternsearch(dejong2fcn,...
[-1.9,2],[],[],[],[],[],[],[],opts);
optimization terminated: mesh size less than options.meshtolerance.
disp(feval2)
4.0686e-10
disp(output2.funccount)
291
the objective function value at the solution is much better (lower) when using this search method, and the number of function evaluations is much lower. fminsearch
is more efficient at getting close to the minimum of rosenbrock's function.