Main Content

Use Cache

Typically, at any given iteration of a pattern search, some of the mesh points might coincide with mesh points at previous iterations. By default, the pattern search recomputes the objective function at these mesh points even though it has already computed their values and found that they are not optimal. If computing the objective function takes a long time, this can make the pattern search run significantly longer.

You can eliminate these redundant computations by using a cache, that is, by storing a history of the points that the pattern search has already visited. To do so, set Cache to On in Cache options. At each poll, the pattern search checks to see whether the current mesh point is within a specified tolerance, Tolerance, of a point in the cache. If so, the search does not compute the objective function for that point, but uses the cached function value and moves on to the next point.


When Cache is set to On, the pattern search might fail to identify a point in the current mesh that improves the objective function because it is within the specified tolerance of a point in the cache. As a result, the pattern search might run for more iterations with Cache set to On than with Cache set to Off. It is generally a good idea to keep the value of Tolerance very small, especially for highly nonlinear objective functions.


Cache does not work when you run the solver in parallel.

For example, set up the problem described in Constrained Minimization Using patternsearch and Optimize Live Editor Task as follows:

  1. Enter the following at the command line:

    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];
  2. Create options to plot the best function value and function evaluations. Because the problem has linear constraints, use the 'GSSPositiveBasis2N' poll method. Turn off the display.

    opts = optimoptions('patternsearch','PollMethod','GSSPositiveBasis2N',...
  3. Run the optimization.

    [x,fval,exitflag,output] = patternsearch(@lincontest7,x0,...

After the pattern search finishes, the plots appear as shown in the following figure.

Note that the total function count is 758.

Now, set the Cache option to 'On' and run the example again.

opts.Cache = 'on';
[x2,fval2,exitflag2,output2] = patternsearch(@lincontest7,x0,...

The total function count is reduced to 735.

ans =

   758   735

See Also

Related Topics