effects of pattern search options -凯发k8网页登录
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. for a list of available options for patternsearch
algorithms, see options table for pattern search algorithms.
set up a problem for pattern search
the problem to minimize is a quadratic function of six variables subject to linear equality and inequality constraints. the objective function, lincontest7
, is available when you run this example.
type lincontest7
function y = lincontest7(x) %lincontest7 objective function. % y = lincontest7(x) evaluates y for the input x. make sure that x is a column % vector, whereas objective function gets a row vector. % 凯发官网入口首页 copyright 2003-2017 the mathworks, inc. x = x(:); �fine a quadratic problem in terms of h and f 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
as the objective function.
objectivefcn = @lincontest7;
the objective function accepts a row vector of length six. specify an initial point for the optimization.
x0 = [2 1 0 9 1 0];
create linear constraint matrices representing the constraints aineq*x <= bineq
and aeq*x = beq
. for details, see .
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];
run the patternsearch
solver, 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);
the number of iterations is: 80
fprintf('the number of function evaluations is: %d\n', output.funccount);
the number of function evaluations is: 737
fprintf('the best function value found is: %g\n', fval);
the best function value found is: 2189.18
add visualization
monitor the optimization process by specifying options that select two plot functions. the plot function psplotbestf
plots the best objective function value at every iteration, and the plot function psplotfuncount
plots 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});
run the patternsearch
solver, including the opts
argument. because the problem has no upper or lower bound constraints and no nonlinear constraints, pass empty arrays ([]
) for the seventh, eighth, and ninth arguments.
[x1,fval,exitflag,output] = patternsearch(objectivefcn,x0,aineq,bineq, ...
aeq,beq,[],[],[],opts);
optimization terminated: mesh size less than options.meshtolerance.
mesh options
pattern search involves evaluating the objective function at points in a mesh. the size of the mesh can influence the speed of the solution. you can control the size of the mesh using 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 of 1
by default. to start the initial mesh size at 1/2, set the initialmeshsize
option.
opts = optimoptions(opts,'initialmeshsize',1/2);
mesh scaling
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. the scalemesh
option is on (true
) by default, but you can turn it off if the problem is well scaled. in general, if the problem is poorly scaled, setting this option to true
can reduce the number of function evaluations. for this problem, set scalemesh
to false
, because lincontest7
is a well-scaled objective function.
opts = optimoptions(opts,'scalemesh',false);
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. the patternsearch
solver 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. the acceleratemesh
option is off (false
) by default. for this problem, set acceleratemesh
to true
because the objective function is smooth.
opts = optimoptions(opts,'acceleratemesh',true);
run the patternsearch
solver.
[x2,fval,exitflag,output] = patternsearch(objectivefcn,x0,aineq,bineq, ...
aeq,beq,[],[],[],opts);
optimization terminated: mesh size less than options.meshtolerance.
fprintf('the number of iterations is: %d\n', output.iterations);
the number of iterations is: 146
fprintf('the number of function evaluations is: %d\n', output.funccount);
the number of function evaluations is: 560
fprintf('the best function value found is: %g\n', fval);
the best function value found is: 2189.18
the mesh option settings reduce the number of iterations and the number of function evaluations, and with no apparent loss of accuracy.
stopping criteria and tolerances
meshtolerance
is a tolerance on the mesh size. if the mesh size is less than meshtolerance
, the solver stops. steptolerance
is the minimum tolerance on the change in the current point to the next point. functiontolerance
is the minimum tolerance on the change in the function value from the current point to the next point.
set the meshtolerance
to 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;
search methods in pattern search
the pattern search algorithm can use an additional search method at every iteration, based on the value of the searchfcn
option. when you specify a search method using searchfcn
, patternsearch
performs the specified search first, before the mesh search. if the search method is successful, patternsearch
skips the mesh search, commonly called the poll function, for that iteration. if the search method is unsuccessful in improving the current point, patternsearch
performs the mesh search.
you can specify different search methods for searchfcn
, including searchga
and searchneldermead
, 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 searchlhs
method, which generates latin hypercube points, at every iteration or possibly every 10 iterations.
other choices for search methods include poll methods such as positive basis n 1 or positive basis 2n. a recommended strategy is to use positive basis n 1 (which requires at most n 1 points to create a pattern) as a search method and positive basis 2n (which requires 2n points to create a pattern) as a poll method.
update the options structure to use positivebasisnp1
as the search method. because positive basis 2n is the default for the pollfcn
option, do not set that option.
opts.searchfcn = @positivebasisnp1;
run the patternsearch
solver.
[x5,fval,exitflag,output] = patternsearch(objectivefcn,x0,aineq,bineq,aeq,beq, ...
[],[],[],opts);
optimization terminated: change in x less than options.steptolerance.
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);
the number of function evaluations is: 562
fprintf('the best function value found is: %g\n', fval);
the best function value found is: 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.