B Test Functions
Researchers in optimization use several test functions to evaluate optimization
algorithms. This section covers several test functions used throughout this book.

B.1

Ackley’s Function

Ackley’s function (figure B.1) is used to test a method’s susceptibility to getting
stuck in local minima. It is comprised of two primary components—a sinusoidalcomponent that produces a multitude of local minima and an exponential bell
curve centered at the origin, which establishes the function’s global minimum.
Figure B.1. The two-dimensionalversion of Ackley’s function. Theglobal minimum is at the origin.

3020x2100−10−20−30−30−20−100x1102030

426 a p p e n d i x b. te st fu n c ti on sAckley’s function is defined for any number of dimensions d:
 v!
u dd
u11t
2xi  − expcos(cxi ) + a + exp(1)f (x) = − a exp−bd i∑d i∑=1=1(B.1)

with a global minimum at the origin with an optimal value of zero. Typically,a = 20, b = 0.2, and c = 2π. Ackley’s function is implemented in algorithm B.1.
function ackley(x, a=20, b=0.2, c=2π)
d = length(x)return -a*exp(-b*sqrt(sum(x.^2)/d)) exp(sum(cos.(c*xi) for xi in x)/d) + a + exp(1)endAlgorithm B.1. Ackley’s function
with d-dimensional input vector x
and three optional parameters.

B.2 Booth’s Function
Booth’s function (figure B.2) is a two-dimensional quadratic function.
Figure B.2. Booth’s function witha global minimum at [1, 3].

10x250−5−10−10−50x1510© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com

b. 3. bra n i n fu n c ti on 427

Its equation is given byf (x) = ( x1 + 2x2 − 7)2 + (2x1 + x2 − 5)2(B.2)

with a global minimum at [1, 3] with an optimal value of zero. It is implemented
in algorithm B.2.
Algorithm B.2. Booth’s function
with two-dimensional input vectorx.

booth(x) = (x[1]+2x[2]-7)^2 + (2x[1]+x[2]-5)^2

B.3 Branin Function
The Branin function (figure B.3) is a two-dimensional function,f (x) = a( x2 − bx12 + cx1 − r )2 + s(1 − t) cos( x1 ) + s(B.3)

with recommended values a = 1, b = 5.1/(4π 2 ), c = 5/π, r = 6, s = 10, andt = 1/(8π ).
Figure B.3. The Branin function,with four global minima.

20x2151050−5051015x1© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com

428 a p p e n d i x b. te st fu n c ti on s

It has no local minima aside from global minima with x1 = π + 2πm for
integral m. Four of these minima are:
#)
# "
# "
# "
("5π3ππ−π(B.4),,,12.875
2.475
2.275
12.275
with f (x∗ ) ≈ 0.397887. It is implemented in algorithm B.3.
function branin(x; a=1, b=5.1/(4π^2), c=5/π, r=6, s=10, t=1/(8π))
return a*(x[2]-b*x[1]^2+c*x[1]-r)^2 + s*(1-t)*cos(x[1]) + send

Algorithm B.3. The Branin function with two-dimensional input
vector x and six optional parameters.

B.4 Flower Function
The flower function (figure B.4) is a two-dimensional test function whose contour
function has flower-like petals originating from the origin.
Figure B.4. The flower function.

32x2

10−1−2−3−3−2−10x1

123The equation isf (x) = akxk + b sin(c tan−1 ( x2 , x1 ))(B.5)© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com

b. 5 . mi c ha l e w i c z fu n c ti on 429

with its parameters typically set to a = 1, b = 1, and c = 4.
The flower function is minimized near the origin but does not have a global
minimum due to atan being undefined at [0, 0]. It is implemented in algorithm B.4.
Algorithm B.4. The flower function
with two-dimensional input vector
x and three optional parameters.

function flower(x; a=1, b=1, c=4)
return a*norm(x) + b*sin(c*atan(x[2], x[1]))end

B.5 Michalewicz Function
The Michalewicz function (figure B.5) is a d-dimensional optimization function
with several steep valleys.
Figure B.5. The Michalewicz function.

4x2

32

1002x1

13Its equation isdf (x) = − ∑ sin( xi ) sin
i =12mixi2π4!

(B.6)© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com

430 a p p e n d i x b. te st fu n c ti on s

where the parameter m, typically 10, controls the steepness. The global minimum
depends on the number of dimensions. In two dimensions the minimum is at approximately [2.20, 1.57] with f (x∗ ) = −1.8011. It is implemented in algorithm B.5.
Algorithm B.5. The Michalewicz
function with input vector x andoptional steepness parameter m.

function michalewicz(x; m=10)
return -sum(sin(v)*sin(i*v^2/π)^(2m) for(i,v) in enumerate(x))
end

B.6 Rosenbrock’s Banana Function
The Rosenbrock function (figure B.6), also called Rosenbrock’s valley or Rosenbrock’s banana function, is a well-known unconstrained test function developedby Rosenbrock in 1960.1 It has a global minimum inside a long, curved valley. Most
optimization algorithms have no problem finding the valley but have difficulties
traversing along the valley to the global minimum.

H. H. Rosenbrock, “An Automatic
Method for Finding the Greatestor Least Value of a Function,”
The Computer Journal, vol. 3, no. 3,
pp. 175–184, 1960.
1Figure B.6. The Rosenbrock function with a = 1 and b = 5. Theglobal minimum is at [1, 1].

2x2

10−1−2−2−10x1

12© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com

b. 7. w he e l e r’s ri d g e

431

The Rosenbrock function is(B.7)

f (x) = ( a − x1 )2 + b( x2 − x12 )2

with a global minimum at [ a, a2 ] at which f (x∗ ) = 0. This text uses a = 1 andb = 5. The Rosenbrock function is implemented in algorithm B.6.
Algorithm B.6. The Rosenbrockfunction with two-dimensional input vector x and two optional parameters.

rosenbrock(x; a=1, b=5) = (a-x[1])^2 + b*(x[2] - x[1]^2)^2

B.7 Wheeler’s Ridge
Wheeler’s ridge (figure B.7) is a two-dimensional function with a single global
minimum in a deep curved peak. The function has two ridges, one along thepositive and one along the negative first coordinate axis. A gradient descentmethod will diverge along the negative axis ridge. The function is very flat awayfrom the optimum and the ridge.
Figure B.7. Wheeler’s ridge showing the two ridges and the peak
containing the global minimum.

6x2420−205

10152025

x1

The function is given byf (x) = − exp(−( x1 x2 − a)2 − ( x2 − a)2 )(B.8)

with a typically equal to 1.5, for which the global optimum of −1 is at [1, 3/2].
wheeler(x, a=1.5) = -exp(-(x[1]*x[2] - a)^2 -(x[2]-a)^2)

Wheeler’s ridge has a smooth contour plot (figure B.8) when evaluated overx1 ∈ [0, 3] and x2 ∈ [0, 3]. It is implemented in algorithm B.7.

x22−5002x1

Figure B.8. A contour plot of theminimal region of Wheeler’s ridge.
Algorithm B.7. Wheeler’s ridge,
which takes in a two-dimensional
design point x and an optional
scalar parameter a.

© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com

432 a p p e n d i x b. te st fu n c ti on s

B.8 Circle Function
The circle function (algorithm B.8) is a simple multiobjective test function given by"#1 − r cos(θ )f(x) =(B.9)
1 − r sin(θ )where θ = x1 and r is obtained by passing x2 through!
1 12x2
r= +2 2 1 + x22(B.10)

The Pareto frontier has r = 1 and mod (θ, 2π ) ∈ [0, π/2] or r = −1 andmod (θ, 2π ) ∈ [π, 3π/2].
function circle(x)θ = x[1]r = 0.5 + 0.5*(2x[2]/(1+x[2]^2))
y1 = 1 - r*cos(θ)
y2 = 1 - r*sin(θ)
return [y1, y2]
end

Algorithm B.8. The circle function,
which takes in a two-dimensional
design point x and produces a twodimensional objective value.

© 2019 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-22 00:25:57-07:00, revision 47fd495, comments to bugs@algorithmsbook.com