## Polling Types

### Using a Complete Poll in a Generalized Pattern Search

As an example, consider the following function.

The following figure shows a plot of the function.

The global minimum of the function occurs at (0, 0), where its value is -25. However, the function also has a local minimum at (0, 9), where its value is -16.

To create a file that computes the function, copy and paste the following code into a new file in the MATLAB® Editor.

```function z = poll_example(x) if x(1)^2 + x(2)^2 <= 25 z = x(1)^2 + x(2)^2 - 25; elseif x(1)^2 + (x(2) - 9)^2 <= 16 z = x(1)^2 + (x(2) - 9)^2 - 16; else z = 0; end```

Save the file as `poll_example.m` in a folder on the MATLAB path.

To run a pattern search on the function, enter the following.

```options = optimoptions('patternsearch','Display','iter'); [x,fval] = patternsearch(@poll_example,[0,5],... [],[],[],[],[],[],[],options)```

MATLAB returns a table of iterations and the solution.

```x = 0 9 fval = -16```

The algorithm begins by a=evaluating the function at the initial point, f(0, 5) = 0. The poll evaluates the following during its first iterations.

f((0, 5) + (1, 0)) = f(1, 5) = 0

f((0, 5) + (0, 1)) = f(0, 6) = -7

As soon as the search polls the mesh point (0, 6), at which the objective function value is less than at the initial point, it stops polling the current mesh and sets the current point at the next iteration to (0, 6). Consequently, the search moves toward the local minimum at (0, 9) at the first iteration. You see this by looking at the first two lines of the command line display.

```Iter f-count f(x) MeshSize Method 0 1 0 1 1 3 -7 2 Successful Poll```

Note that the pattern search performs only two evaluations of the objective function at the first iteration, increasing the total function count from 1 to 3.

Next, set `UseCompletePoll` to `true` and rerun the optimization.

```options.UseCompletePoll = true; [x,fval] = patternsearch(@poll_example,[0,5],... [],[],[],[],[],[],[],options);```

This time, the pattern search finds the global minimum at (0, 0). The difference between this run and the previous one is that with `UseCompletePoll` set to `true`, at the first iteration the pattern search polls all four mesh points.

f((0, 5) + (1, 0)) = f(1, 5) = 0

f((0, 5) + (0, 1)) = f(0, 6) = -6

f((0, 5) + (-1, 0)) = f(-1, 5) = 0

f((0, 5) + (0, -1)) = f(0, 4) = -9

Because the last mesh point has the lowest objective function value, the pattern search selects it as the current point at the next iteration. The first two lines of the command-line display show this.

```Iter f-count f(x) MeshSize Method 0 1 0 1 1 5 -9 2 Successful Poll```

In this case, the objective function is evaluated four times at the first iteration. As a result, the pattern search moves toward the global minimum at (0, 0).

The following figure compares the sequence of points returned when Complete poll is set to `Off` with the sequence when Complete poll is `On`.

### Compare the Efficiency of Poll Options

This example shows how several poll options interact in terms of iterations and total function evaluations. The main results are:

• GSS is more efficient than GPS or MADS for linearly constrained problems.

• Whether setting `UseCompletePoll` to `true` increases efficiency or decreases efficiency is unclear, although it affects the number of iterations.

• Similarly, whether having a `2N` poll is more or less efficient than having an `Np1` poll is also unclear. The most efficient poll is `GSS Positive Basis Np1` with Complete poll set to `on`. The least efficient is ```MADS Positive Basis Np1``` with Complete poll set to `on`.

Note

The efficiency of an algorithm depends on the problem. GSS is efficient for linearly constrained problems. However, predicting the efficiency implications of the other poll options is difficult, as is knowing which poll type works best with other constraints.

#### Problem setup

The problem is the same as in Solve Using patternsearch in Optimize Live Editor Task. This linearly constrained problem uses the `lincontest7` file that comes with the toolbox:

1. Enter the following into your MATLAB workspace.

```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. Set the initial options and objective function.

```options = optimoptions('patternsearch',... 'PollMethod','GPSPositiveBasis2N',... 'PollOrderAlgorithm','consecutive',... 'UseCompletePoll',false); fun = @lincontest7;```
3. Run the optimization, naming the `output` structure `outputgps2noff`.

```[x,fval,exitflag,outputgps2noff] = patternsearch(fun,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);```
4. Set options to use a complete poll.

`options.UseCompletePoll = true;`
5. Run the optimization, naming the `output` structure `outputgps2non`.

```[x,fval,exitflag,outputgps2non] = patternsearch(fun,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);```
6. Continue in a like manner to create output structures for the other poll methods with `UseCompletePoll` set `true` and `false`: `outputgss2noff`, `outputgss2non`, `outputgssnp1off`, `outputgssnp1on`, `outputmads2noff`, `outputmads2non`, `outputmadsnp1off`, and `outputmadsnp1on`.

#### Examine the Results

You have the results of 12 optimization runs. The following table shows the efficiency of the runs, measured in total function counts and in iterations. Your MADS results could differ, since MADS is a stochastic algorithm.

AlgorithmFunction CountIterations
GPS2N, complete poll off1462136
GPS2N, complete poll on139696
GPSNp1, complete poll off864118
GPSNp1, complete poll on1007104
GSS2N, complete poll off75884
GSS2N, complete poll on88974
GSSNp1, complete poll off53394
GSSNp1, complete poll on49170

To obtain, say, the first row in the table, enter `gps2noff.output.funccount` and `gps2noff.output.iterations`. You can also examine options in the Variables editor by double-clicking the options in the Workspace Browser, and then double-clicking the `output` structure.

Alternatively, you can access the data from the output structures.

`[outputgps2noff.funccount,outputgps2noff.iterations]`
```ans = 1462 136```

The main results gleaned from the table are:

• Setting `UseCompletePoll` to `true` generally lowers the number of iterations for GPS and GSS, but the change in number of function evaluations is unpredictable.

• Setting `UseCompletePoll` to `true` does not necessarily change the number of iterations for MADS, but substantially increases the number of function evaluations.

• The most efficient algorithm/options settings, with efficiency meaning lowest function count:

1. `'GSSPositiveBasisNp1'` with `UseCompletePoll` set to `true` (function count 491)

2. `'GSSPositiveBasisNp1'` with `UseCompletePoll` set to `false` (function count 533)

3. `'GSSPositiveBasis2N'` with `UseCompletePoll` set to `false` (function count 758)

4. `'GSSPositiveBasis2N'` with `UseCompletePoll` set to `true` (function count 889)

The other poll methods had function counts exceeding 900.

• For this problem, the most efficient poll is `'GSSPositiveBasisNp1'` with `UseCompletePoll` set to `true`, although the `UseCompletePoll` setting makes only a small difference. The least efficient poll is `'MADSPositiveBasis2N'` with `UseCompletePoll` set to `true`. In this case, the `UseCompletePoll` setting makes a substantial difference.