Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Direct search: the Nelder-Mead and Powell methods

ProblemMinimise a function of a few variables using only its values: no derivatives, which may be unavailable, expensive or meaningless (noisy or non-smooth )
Devised bySimplex search: William Spendley, George Hext and Francis Himsworth (1962); the adaptive simplex: John Nelder and Roger Mead (1965). Conjugate directions without derivatives: Michael Powell (1964)
AccuracyNelder-Mead: no general convergence guarantee, but usually reaches a local minimum to the tolerance on function values. Powell: converges on smooth functions, finitely on quadratics with exact line searches
CostNelder-Mead: one or two evaluations per iteration, for a shrink. Powell: line searches per iteration. Both are practical for up to about 10 to 20 variables
Read firstBackground: what a minimum looks like
APINelderMeadMethod, PowellsMethod

Overview

Sometimes the gradient is not available: is the output of a simulation, a laboratory measurement or a black-box program; or it is not differentiable, with kinks, steps or noise. Direct search methods minimise using only comparisons and values of .

The Nelder-Mead method moves a simplex of points (a triangle in two dimensions, a tetrahedron in three) through the space, reflecting its worst point through the others, stretching along promising directions and shrinking around the best point when nothing else works. It is simple, robust in practice and very widely used; MATLAB’s fminsearch and SciPy’s default derivative-free method are Nelder-Mead. Powell’s method performs one-dimensional minimisations along a set of directions, updating the set so that it becomes conjugate. On smooth functions it can be much more accurate.

Where it is used

Direct search methods need only function values, so they work where derivatives do not exist.

  • Simulation-based design. Tuning parameters of a simulator or an experiment, where each evaluation is a run and no gradient is available.
  • Noisy or non-smooth objectives. Controller gains tuned on measured performance, and hyperparameters of machine-learning models.
  • Quick fits. Nelder-Mead is the method many scientists reach for first to fit a small model to data.

History

William Spendley, George Hext and Francis Himsworth, statisticians at Imperial Chemical Industries, proposed in 1962 to optimise industrial processes by moving a regular simplex of experimental conditions, reflecting the worst. John Nelder and Roger Mead, at the National Vegetable Research Station in Wellesbourne, England, let the simplex change shape, expanding and contracting to adapt to the landscape, in a 1965 paper that became one of the most cited in numerical computing. Its theory lagged far behind its popularity: Jeffrey Lagarias, James Reeds, Margaret and Paul Wright (1998) proved convergence in one and two dimensions for strictly convex functions, and Ken McKinnon (1998) gave a smooth convex function in two dimensions on which it converges to a point that is not a minimum.

Michael Powell, at the UK Atomic Energy Research Establishment at Harwell, published his method in 1964, showing how to generate conjugate directions without derivatives. He later developed a long line of derivative-free methods based on quadratic models (COBYLA, NEWUOA, BOBYQA).

The idea

The Nelder-Mead simplex method

A simplex in dimensions is the figure with corners: a triangle in the plane, a tetrahedron in space. The Nelder-Mead method keeps the value of at each corner (vertex) and repeatedly replaces the worst vertex by a better point. No gradient is ever computed; the vertices themselves show which way is downhill, since the function is higher at the worst vertex than at the others.

At the start of each iteration, order the vertices from best to worst, , and let be the centroid (average) of all but the worst. The line from the worst vertex through points away from the high ground. The iteration tries points along this line, in order:

  • Reflect. Mirror the worst vertex through the centroid: . If is better than the second-worst vertex but not better than the best, accept it. The simplex has flipped over, away from the worst point.
  • Expand. If is the best point so far, the direction is a good one, so try going twice as far, , and keep the better of and . The simplex stretches along the promising direction.
  • Contract. If is no better than the second worst, the step was too long. Try a point halfway between and (an outside contraction, if at least improved on the worst vertex) or halfway between and (an inside contraction, if it did not).
  • Shrink. If the contraction fails too, the simplex is probably straddling a minimum or a narrow valley. Move every vertex halfway towards the best one.

Each iteration normally costs one or two evaluations of ; only a shrink costs . Through these moves the simplex rolls downhill, stretches out along valleys, squeezes through narrow passages and finally contracts onto a minimum.

The method stops when the function values at the vertices agree to the tolerance (their standard deviation falls below it). The initial simplex is built by stepping 5% along each coordinate from the starting point, as fminsearch does.

Figure 5.5. Direct search on the Rosenbrock function.

Powell’s method

The most obvious derivative-free strategy is to minimise along each coordinate axis in turn and repeat. It works, but in a tilted valley it zigzags in tiny steps, for the same reason as gradient descent. Powell’s method starts this way and then improves the set of directions as it learns the shape of the function.

It keeps a set of directions, initially the coordinate axes. One iteration does the following.

  1. Starting from , minimise along each direction in turn by a one-dimensional search (bracketing, then Brent’s parabolic interpolation), arriving at .
  2. Take the overall displacement of the iteration, , as a candidate new direction. It summarises the net progress of small zigzag steps, so it tends to point along the valley.
  3. Replace one of the old directions by the new one, and minimise along it.

On a quadratic, the directions generated this way become mutually conjugate, so that after iterations the minimum is reached exactly, without a single derivative.

There is one hazard. If the new directions are added carelessly, the set can become linearly dependent, and the search is then confined to a subspace that may not contain the minimum. To prevent this, the new direction replaces the old one along which decreased most (the one it most resembles), and only when a test shows the replacement is worthwhile.

Algorithm

Nelder-Mead. Input: and a starting point, from which the initial simplex is built. The moves use the standard coefficients: 1 for reflection, 2 for expansion, ½ for contraction and ½ for shrinking. Every trial point is projected onto the bounds, if any are set. Repeat:

  1. Sort the vertices so that .
  2. Test. If the standard deviation of the function values is below the tolerance, return the best vertex.
  3. Reflect. Compute the centroid of the best vertices and the reflected point , and evaluate .
  4. Choose a move by comparing with the values at the vertices:
    • better than the best: evaluate the expanded point and keep the better of the two;
    • between the best and the second worst: keep ;
    • worse than the second worst: evaluate the contracted point (outside if beats the worst vertex, inside otherwise) and keep it if it improves on the point it was contracted from;
    • if the contraction fails: shrink every vertex halfway towards the best, and evaluate at the moved vertices.
  5. In the first three cases the chosen point replaces the worst vertex. Go to 1.

Powell’s method. Input: and a starting point; the directions start as the coordinate axes. One iteration:

  1. Remember the starting point and .
  2. For each direction in turn, minimise along it from the current point by a one-dimensional search, and move there. Note which direction gave the largest decrease. Call the final point .
  3. Test. If the iteration reduced by less than the relative tolerance, return .
  4. Consider a new direction. Evaluate at the extrapolated point , a further step of the same size along the iteration’s overall displacement. A test on that value decides whether the displacement is worth adopting: it must still lead downhill, and the decrease of this iteration must not have been mostly due to a single direction.
  5. If the test passes, minimise along , and let it replace the direction of largest decrease noted in step 2.

Implementation

            let centroid = centroid_excluding_worst(&simplex);
            let worst = &simplex[dimension];
            let second_worst_value = values[dimension - 1];
            let worst_value = values[dimension];

            let reflected = combine(&centroid, worst, 1.0 + alpha, -alpha);
            let reflected = project_to_bounds(&reflected, bounds.as_deref());
            let reflected_value = objective_function.evaluate(&reflected);

            if reflected_value < values[0] {
                let expanded = combine(&centroid, &reflected, 1.0 - gamma, gamma);
                let expanded = project_to_bounds(&expanded, bounds.as_deref());
                let expanded_value = objective_function.evaluate(&expanded);
                if expanded_value < reflected_value {
                    simplex[dimension] = expanded;
                    values[dimension] = expanded_value;
                } else {
                    simplex[dimension] = reflected;
                    values[dimension] = reflected_value;
                }
                continue;
            }

            if reflected_value < second_worst_value {
                simplex[dimension] = reflected;
                values[dimension] = reflected_value;
                continue;
            }

            if reflected_value < worst_value {
                let outside_contracted = combine(&centroid, &reflected, 1.0 - rho, rho);
                let outside_contracted = project_to_bounds(&outside_contracted, bounds.as_deref());
                let outside_contracted_value = objective_function.evaluate(&outside_contracted);
                if outside_contracted_value <= reflected_value {
                    simplex[dimension] = outside_contracted;
                    values[dimension] = outside_contracted_value;
                    continue;
                }
            } else {
                let inside_contracted = combine(&centroid, worst, 1.0 - rho, rho);
                let inside_contracted = project_to_bounds(&inside_contracted, bounds.as_deref());
                let inside_contracted_value = objective_function.evaluate(&inside_contracted);
                if inside_contracted_value < worst_value {
                    simplex[dimension] = inside_contracted;
                    values[dimension] = inside_contracted_value;
                    continue;
                }
            }

            let best = simplex[0].clone();
            for i in 1..=dimension {
                simplex[i] = combine(&best, &simplex[i], 1.0 - sigma, sigma);
                simplex[i] = project_to_bounds(&simplex[i], bounds.as_deref());
                values[i] = objective_function.evaluate(&simplex[i]);
            }
            let (p0, f0) = (p.clone(), fp);
            let (mut biggest, mut biggest_i) = (0.0, 0);
            for (i, dir) in dirs.iter().enumerate() {
                let before = fp;
                let (x, fx) = minimise_along(&p, dir);
                if fx < fp {
                    (p, fp) = (x, fx);
                }
                if before - fp > biggest {
                    biggest = before - fp;
                    biggest_i = i;
                }
            }
            if 2.0 * (f0 - fp) <= tol * (f0.abs() + fp.abs()) + 1e-300 {
                converged = true;
                break;
            }
            // Extrapolated point and the net displacement direction.
            let net: Vec<f64> = p.iter().zip(&p0).map(|(a, b)| a - b).collect();
            let mut extrapolated: Vec<f64> = p.iter().zip(&net).map(|(a, d)| a + d).collect();
            clamp_into(&mut extrapolated, &bounds);
            let fe = objective_function.evaluate(&extrapolated);
            if fe < f0 {
                let t = 2.0 * (f0 - 2.0 * fp + fe) * (f0 - fp - biggest).powi(2) - biggest * (f0 - fe).powi(2);
                if t < 0.0 && net.iter().any(|&d| d != 0.0) {
                    let (x, fx) = minimise_along(&p, &net);
                    if fx < fp {
                        (p, fp) = (x, fx);
                    }
                    let last = dirs.len() - 1;
                    dirs.swap(biggest_i, last);
                    dirs[last] = net;
                }
            }
  • Bounds are enforced by projecting every trial point onto the box. Projection can flatten a simplex against a face: a start on an upper bound once had its initial step projected straight back onto the bound, so the simplex never left that face and returned a wrong point as converged. The initial step now points inward in that case.

On the Rosenbrock function (evaluations to convergence, and the final value):

Table 5.5. Direct search against BFGS.

problemNelder-MeadPowellBFGS (exact gradient)
Rosenbrock, 216 ev., 684 ev., 48 ev. of and ,
Rosenbrock, 661 ev., 2486 ev., 62 ev. of and ,
Rosenbrock, 5156 ev., 31756 ev., 94 ev. of and ,

Nelder-Mead stops when its vertices’ values agree to , so its answers are accurate only to about that level; Powell’s method keeps going to the bottom, at a higher price. A gradient method, when a gradient is available, is far cheaper.

In practice

//! Derivative-free minimisation: Nelder-Mead and Powell's method.
//!
//! Run with `cargo run -p lib_jsl_optimization --example optimization_direct_search`.

use lib_jsl_optimization::nelder_mead_method::NelderMeadMethod;
use lib_jsl_optimization::optimization_traits::{GradientFreeMinimizationEngine, MinimizationControls, ObjectiveFunction};
use lib_jsl_optimization::powells_method::PowellsMethod;

/// The total distance from a point to three towns, minimised at the Fermat point; it is not
/// differentiable at the towns themselves, which bothers neither method.
struct Distances;
const TOWNS: [(f64, f64); 3] = [(0.0, 0.0), (4.0, 0.0), (1.0, 3.0)];
impl ObjectiveFunction for Distances {
    fn evaluate(&self, p: &[f64]) -> f64 {
        TOWNS.iter().map(|(x, y)| ((p[0] - x).powi(2) + (p[1] - y).powi(2)).sqrt()).sum()
    }
}

fn main() {
    let r = NelderMeadMethod::new().gradient_free_minimize(&Distances, vec![3.0, 3.0]).unwrap();
    println!("Nelder-Mead: {:?}, total distance {:.8}", r.parameters, r.objective_value);
    let r = PowellsMethod::new().gradient_free_minimize(&Distances, vec![3.0, 3.0]).unwrap();
    println!("Powell:      {:?}, total distance {:.8}", r.parameters, r.objective_value);

    // Bounds are handled by projection: keep the point in the box x ≥ 2.
    let mut bounded = NelderMeadMethod::new();
    bounded.set_bounds(Some(vec![(2.0, 10.0), (-10.0, 10.0)]));
    let r = bounded.gradient_free_minimize(&Distances, vec![3.0, 3.0]).unwrap();
    println!("with x ≥ 2:  {:?}, total distance {:.8}", r.parameters, r.objective_value);
}

Strengths

  • Only function values are needed, and need not be smooth.
  • Nelder-Mead is robust and makes rapid early progress on small problems; Powell’s method is accurate on smooth ones.

Weaknesses

  • Slow and unreliable beyond about 10 to 20 variables.
  • Nelder-Mead has no convergence guarantee and can stall at non-minima (McKinnon’s example); restarting from the solution is a cheap check.
  • Both find local minima only.

Exercises

  1. Run Nelder-Mead on from and draw the first ten simplices.
  2. Show that if and are minima of a quadratic along two parallel lines with direction , then is conjugate to . This is the principle of Powell’s method.
  3. Minimise (non-differentiable at the minimum) with Nelder-Mead, Powell and BFGS.
  4. Implement McKinnon’s function and observe Nelder-Mead converging to a non-stationary point.
  5. Add noise of size to the Rosenbrock function and compare the accuracy reached by Nelder-Mead and by BFGS with finite-difference gradients.
Hints and answers
  1. The simplex reflects its worst vertex through the centroid of the others, so early steps move towards the origin, expand if the reflection is a big improvement, and contract as it nears the minimum; sketch the triangles from the printed vertices.
  2. Minimising along from means , and likewise at . For a quadratic , so .
  3. The minimiser is the kink at the origin. Nelder-Mead and Powell use only function values and approach it; BFGS relies on a gradient that is discontinuous there, so expect its line searches to stall at modest accuracy.
  4. Use the parameters given in the entry. Nelder-Mead’s simplex repeatedly contracts onto a line that does not contain the minimum and stalls at a point where the gradient is not zero.
  5. Nelder-Mead sees only noisy function values and reaches an within the noise but locates only to about the square root of the noise, . BFGS with finite-difference gradients needs steps well above the noise level and loses accuracy quickly as the noise grows.

Further reading

  • J. A. Nelder and R. Mead, “A simplex method for function minimization”, The Computer Journal 7 (1965) 308–313.
  • M. J. D. Powell, “An efficient method for finding the minimum of a function of several variables without calculating derivatives”, The Computer Journal 7 (1964) 155–162.
  • J. C. Lagarias, J. A. Reeds, M. H. Wright and P. E. Wright, “Convergence properties of the Nelder-Mead simplex method in low dimensions”, SIAM Journal on Optimization 9 (1998) 112–147.
  • K. I. M. McKinnon, “Convergence of the Nelder-Mead simplex method to a nonstationary point”, SIAM Journal on Optimization 9 (1998) 148–158.
  • A. R. Conn, K. Scheinberg and L. N. Vicente, Introduction to Derivative-Free Optimization, SIAM, 2009.