pattern search climbs mount washington -凯发k8网页登录
this example shows visually how pattern search optimizes a function. the function is the height of the terrain near mount washington, as a function of the x-y location. in order to find the top of mount washington, we minimize the objective function that is the negative of the height. (the mount washington in this example is the highest peak in the northeastern united states.)
the us geological survey provides data on the height of the terrain as a function of the x-y location on a grid. in order to be able to evaluate the height at an arbitrary point, the objective function interpolates the height from nearby grid points.
it would be faster, of course, to simply find the maximum value of the height as specified on the grid, using the max
function. the point of this example is to show how the pattern search algorithm operates; it works on functions defined over continuous regions, not just grid points. also, if it is computationally expensive to evaluate the objective function, then performing this evaluation on a complete grid, as required by the max
function, will be much less efficient than using the pattern search algorithm, which samples a small subset of grid points.
how pattern search works
pattern search finds a local minimum of an objective function by the following method, called polling. in this description, words describing pattern search quantities are in bold. the search starts at an initial point, which is taken as the current point in the first step:
1. generate a pattern of points, typically plus and minus the coordinate directions, times a mesh size, and center this pattern on the current point.
2. evaluate the objective function at every point in the pattern.
3. if the minimum objective in the pattern is lower than the value at the current point, then the poll is successful, and the following happens:
3a. the minimum point found becomes the current point.
3b. the mesh size is doubled.
3c. the algorithm proceeds to step 1.
4. if the poll is not successful, then the following happens:
4a. the mesh size is halved.
4b. if the mesh size is below a threshold, the iterations stop.
4c. otherwise, the current point is retained, and the algorithm proceeds at step 1.
this simple algorithm, with some minor modifications, provides a robust and straightforward method for optimization. it requires no gradients of the objective function. it lends itself to constraints, too, but this example and description deal only with unconstrained problems.
preparing the pattern search
to prepare the pattern search, load the data in mtwashington.mat
, which contains the usgs data on a 472-by-345 grid. the elevation, z, is given in feet. the vectors x and y contain the base values of the grid spacing in the east and north directions respectively. the data file also contains the starting point for the search, x0.
load mtwashington
there are three matlab® files that perform the calculation of the objective function, and the plotting routines. they are:
1. terrainfun
, which evaluates the negative of height at any x-y position. terrainfun
uses the matlab function interp2
to perform two-dimensional linear interpolation. it takes the z data and enables evaluation of the negative of the height at all x-y points.
2. psoutputwashington
, which draws a 3-d rendering of mt. washington. in addition, as the run progresses, it draws spheres around each point that is better (higher) than previously-visited points.
3. psplotwashington
, which draws a contour map of mt. washington, and monitors a slider that controls the speed of the run. it shows where the pattern search algorithm looks for optima by drawing signs at those points. it also draws spheres around each point that is better than previously-visited points.
in the example, patternsearch
uses terrainfun
as its objective function, psoutputwashington
as an output function, and psplotwashington
as a plot function. we prepare the functions to be passed to patternsearch
in anonymous function syntax:
mtwashobjectivefcn = @(xx) terrainfun(xx, x, y, z); mtwashoutputfcn = @(xx,arg1,arg2) psoutputwashington(xx,arg1,arg2, x, y, z); mtwashplotfcn = @(xx,arg1) psplotwashington(xx,arg1, x, y, z);
pattern search options settings
next, we create options for pattern search. this set of options has the algorithm halt when the mesh size shrinks below 1, keeps the mesh unscaled (the same size in each direction), sets the initial mesh size to 10, and sets the output function and plot function:
options = optimoptions(@patternsearch,'meshtolerance',1,'scalemesh',false, ... 'initialmeshsize',10,'usecompletepoll',true,'plotfcn',mtwashplotfcn, ... 'outputfcn',mtwashoutputfcn,'usevectorized',true);
observing the progress of pattern search
when you run this example you see two windows. one shows the points the pattern search algorithm chooses on a two-dimensional contour map of mount washington. this window has a slider that controls the delay between iterations of the algorithm (when it returns to step 1 in the description of how pattern search works). set the slider to a low position to speed the run, or to a high position to slow the run.
the other window shows a three-dimensional plot of mount washington, along with the steps the pattern search algorithm makes. you can rotate this plot while the run progresses to get different views.
[xfinal ffinal] = patternsearch(mtwashobjectivefcn,x0,[],[],[],[],[], ...
[],[],options)
optimization terminated: mesh size less than options.meshtolerance.
xfinal = 1×2
316130 4904295
ffinal = -6280
the final point, xfinal
, shows where the pattern search algorithm finished; this is the x-y location of the top of mount washington. the final objective function, ffinal
, is the negative of the height of mount washington, 6280 feet. (this should be 6288 feet according to the mount washington observatory).
examine the files terrainfun.m
, psoutputwashington.m
, and psplotwashington.m
to see how the interpolation and graphics work.
there are many options available for the pattern search algorithm. for example, the algorithm can take the first point it finds that is an improvement, rather than polling all the points in the pattern. it can poll the points in various orders. and it can use different patterns for the poll, both deterministic and random. consult the global optimization toolbox user's guide for details.