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

Optimisation

To optimise is to find the best value of something: the parameters of a model that fit data most closely, the design of a structure that is lightest for its strength, the schedule that costs least, the policy that earns most. Mathematically, it is to find a minimum (or, by changing sign, a maximum) of an objective function , perhaps subject to constraints on the variables:

Optimisation is closely related to root finding: at a smooth unconstrained minimum the gradient vanishes, so minimisation is root finding for . But there is more structure to exploit (the Hessian is symmetric, and progress can be measured by the value of ) and more ways to fail (a stationary point may be a maximum or a saddle).

Background: what a minimum looks like

Every method in this chapter searches for a point that satisfies some condition. It helps to state the conditions once. Say is a local minimum of if for all near , and a global minimum if the inequality holds for all . Take twice continuously differentiable, with gradient (the vector of partial derivatives) and Hessian (the symmetric matrix of second derivatives). Taylor’s theorem says that a small step changes by

First-order necessary condition. At a local minimum, . If the gradient were not zero, the step with small would change by about , a decrease, so would not be a minimum. A point with is stationary. Minimising is root finding for , but stationary points include maxima and saddles too.

Second-order conditions. At a stationary point the linear term vanishes and the quadratic term decides. If is a minimum then must be positive semidefinite ( for every : no direction curves downward). If instead is positive definite ( for all , equivalently all eigenvalues positive), then is certainly a strict local minimum. The gap between the two statements is real:

FunctionStationary pointHessian thereNature
origin, positive definiteminimum
origin, indefinitesaddle: a minimum along , a maximum along
, semidefiniteminimum, though the test is inconclusive
, semidefiniteneither: an inflection point

The eigenvalues of the Hessian also describe the shape of the valley around the minimum: long, narrow valleys have eigenvalues of very different sizes, and their ratio (the condition number) governs how fast gradient descent converges. Newton’s method divides the gradient by the Hessian, which is why it needs positive definite for the step to be a descent direction.

Convexity makes local global. A function is convex if for all : the graph lies above every tangent plane (equivalently, is positive semidefinite everywhere). Then a stationary point is a global minimum, in one line: put in the inequality to get for every . The leading example is the quadratic with symmetric positive definite: its gradient is , so the unique global minimum solves the linear system . That is the link between optimisation, least squares and the iterative linear solvers of conjugate gradients.

What a stopping test certifies. Methods stop when is small. That shows only that is nearly stationary. If in addition with (the function is strongly convex), then , so a small gradient does bound the remaining error, but only in terms of the curvature , which a flat valley makes small. Without convexity a small gradient may mean a saddle or a local minimum.

With constraints the gradient need not vanish at the solution: the constraint can be what stops the descent. The first-order condition becomes the balance of against the gradients of the active constraints, which is the KKT system of constrained optimisation.

The central questions

  • What information is available? Only values of (direct search, stochastic methods), also the gradient (gradient descent, conjugate gradients, quasi-Newton methods), or also the Hessian (Newton’s method). Each extra derivative buys faster convergence, at the cost of computing it.
  • Local or global? Most methods find a local minimum, the bottom of the valley they start in. If is convex (every chord lies above the graph), every local minimum is global, and the problem is in a sense easy. Otherwise finding the global minimum is hard in general, and the stochastic methods trade many evaluations for a better chance of success.
  • Are there constraints? Bounds on the variables, linear constraints (and a linear objective: linear programming), or general nonlinear constraints (constrained optimisation).
  • Is it a sum of squares? Fitting a model to data minimises , and nonlinear least squares methods exploit that structure.
  • Is it a sequence of decisions? Then dynamic programming may break it into stages and solve it exactly.

The Rosenbrock function , with its long curved valley and minimum at , is the standard test of a local method; it appears throughout the chapter.

Figure 5.1. Methods compared on the Rosenbrock function.

Choosing a method

Your problemRecommendedEntry
One variable, an interval containing the minimumGolden section searchOne-dimensional minimisation
Smooth, gradient available, moderate BFGSNewton and quasi-Newton methods
Smooth, gradient available, large L-BFGS, or nonlinear conjugate gradientsL-BFGS, conjugate gradients
Smooth, Hessian available, good starting pointNewton’s methodNewton’s method
No derivatives, few variablesNelder-Mead or PowellDirect search
A sum of squares (fitting a model to data)Levenberg-MarquardtNonlinear least squares
Simple bounds on the variables; outliers in the data; linear least squares with bounds or L-BFGS-B, trust-region reflective with a robust loss, BVLS, NNLSBound-constrained optimisation
Nonlinear constraintsSQP, or the augmented Lagrangian methodConstrained optimisation
Linear objective and constraintsThe simplex methodLinear programming
Discrete stages or a Markov decision processDynamic programmingDynamic programming
Many local minima, a bounded boxDifferential evolution or particle swarmStochastic global optimisation
Many local minima: deterministic, or annealing with local search, or hopping between minimadirect, dual_annealing, basinhoppingAssignment, trust regions and global optimisation
A smooth function with a Hessian (possibly indefinite)minimize_trust_region (trust-ncg, dogleg, trust-exact)Assignment, trust regions and global optimisation
Match rows to columns at least total costlinear_sum_assignmentAssignment, trust regions and global optimisation

Testing against SciPy

The references come from SciPy 1.17.1, written by book/generator/data/make_optimization_refs.py. For the unconstrained problems the table gives each solution’s distance from the true minimiser, for the library’s method and the closest SciPy method, both run to tight tolerances:

Table 5.1. Unconstrained optimisers against SciPy.

problemmethod (library / SciPy)library: distance from the minimiserSciPy: distance from the minimiser
Rosenbrock, 2 variablesNewton / Newton-CG07e-10
BFGS4e-162e-15
L-BFGS (5 pairs) / L-BFGS-B03e-11
conjugate gradient (PR+) / CG02e-12
Nelder-Mead1e-72e-13
Powell7e-154e-14
Rosenbrock, 5 variablesNewton / Newton-CG2e02e0
BFGS06e-15
L-BFGS (5 pairs) / L-BFGS-B04e-10
conjugate gradient (PR+) / CG4e-142e-12
Nelder-Mead4e-81e-13
Powell01e-11
can of least area, 1 variablegolden section / golden, Brent7e-96e-9, 4e-11

Both Newton implementations stop at the five-variable Rosenbrock function’s other local minimum, near , from this starting point. Nelder-Mead’s distance reflects its stopping rule, which tests the spread of function values, so near a quadratic minimum it places to about the square root of the tolerance; SciPy’s adaptive variant also requires the simplex itself to shrink. Golden-section search is limited the same way, and Brent’s parabolic steps do better.

Table 5.2. Other optimisation routines against SciPy.

problem (SciPy reference)result
Rastrigin’s function, 3 variables (differential_evolution)library f = 7.1e-15 at the global minimum; SciPy f = 0.9950, a local minimum (seed 1)
exponential decay fit, weighted (curve_fit)parameters 2e-9, standard errors 4e-7 (relative)
exponential decay fit, unweighted (curve_fit)parameters 1e-8, standard errors 8e-7 (relative)
diet problem, 3 variables (linprog, HiGHS)solution 4e-16, cost 0
random program, 10 variables, 8 constraints (linprog, HiGHS)solution 9e-15, objective 4e-15
largest box of surface area 6 (minimize, SLSQP)SQP 2e-8, augmented Lagrangian 2e-8 in the solution; objective 5e-12
Hock-Schittkowski 71 (minimize, SLSQP)SQP 2e-8, augmented Lagrangian 7e-8 in the solution; objective 5e-9
0/1 knapsack, 10 items (milp, HiGHS)value 309 (SciPy 309), same items: true

The curve fits agree to the convergence tolerance of MINPACK’s Levenberg-Marquardt, and the standard errors to the accuracy of the finite-difference Jacobians from which both compute the covariance. The linear programs match HiGHS to rounding, and the knapsack matches HiGHS’s branch and bound on the same items. Differential evolution is stochastic. With this seed SciPy’s run ended in a local minimum of Rastrigin’s function while the library’s reached the global one, which says more about the seeds than the implementations.

In the library

The optimisation crate is lib_jsl_optimization (API reference). Most minimisers share a small set of traits from optimization_traits:

  • the problem implements ObjectiveFunction (and, as the method requires, GradientFunction and HessianFunction);
  • the method implements MinimizationControls (maximum iterations, tolerance and optional box bounds, each Optional with a method-specific default) and one of GradientFreeMinimizationEngine, GradientBasedMinimizationEngine or HessianBasedMinimizationEngine;
  • the result is an OptimizationResult with the parameters, the objective value, the number of iterations and a converged flag.

The meaning of “iteration” and of the tolerance differs between methods, and is documented with each. Least squares, constrained optimisation, linear and dynamic programming have their own interfaces, described in their entries.