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.

Note

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.

Note

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];
    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 ]';
     
    F = @(x)0.5*x'*H*x + f'*x;
  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',...
        'PlotFcn',{@psplotbestf,@psplotfuncount},'Display','none');
  3. Run the optimization.

    [x,fval,exitflag,output] = patternsearch(F,x0,...
        Aineq,bineq,Aeq,beq,[],[],[],opts);

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(F,x0,...
    Aineq,bineq,Aeq,beq,[],[],[],opts);

The total function count is reduced to 734.

[output.funccount,output2.funccount]
ans =

   758   734

See Also

Related Topics