A Gallery of Test Functions for Optimization

Vahid Mirjalili, Data Scientist

A gallery of test functions for optimization is provided in python-numpy, as well as their MATLAB and R implementations.

In [3]:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

%matplotlib inline

1. Ackley

$ f(x) = -a \exp \left(-b \sqrt{\frac{1}{d} \sum_{i=1}^{d}x_i^2}\right) - exp\left(\frac{1}{d}\sum_{i=1}^{d} \cos{c x_i}\right) + a + \exp(1) $

Recomended parameters: $a = 20 $, $ b = 0.1 $, $ c = 2$

Range of each dimension: $ x_i \in \left[ -32.768 , 32.768 \right] $

In [2]:
def ackley_fun(x, a=20, b=0.2, c=2*np.pi):
    d = x.shape[0]
    s = sum(x**2)
    return(s)

print(ackley_fun(np.array([1,2,3])))
14

In [3]:
def ackley2d(x, y, a=20, b=0.2, c=2*np.pi):
    d = 2
    sum1 = x**2 + y**2
    sum2 = np.cos(c*x) + np.cos(c*y)
    term1 = -a * np.exp(-b*np.sqrt(sum1/d))
    term2 = -np.exp(sum2/d)
    s = term1 + term2 + a + np.exp(1)
    return(s)


fig = plt.figure(figsize=(10, 7))
ax = fig.gca(projection='3d')
x = np.linspace(-20, 20, 200)
y = x
X,Y = np.meshgrid(x, y)
Z = ackley2d(X, Y)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm,
        linewidth=0, antialiased=False)

#ax.set_zlim(0, 0.2)

ax.zaxis.set_major_locator(plt.LinearLocator(10))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%.01f'))

fig.colorbar(surf, shrink=0.5, aspect=7, cmap=plt.cm.coolwarm)

plt.show()

2. Styblinski-Tang Function

$ f(x) = 1/2 \sum_{i=1}^{d} (x_i^4 - 16x_i^2 + 5x_i) $

  • dimension: \(d\)

  • range $ -5 < x_i < 5 $

  • global minimum: $f^* = -39.16 $ at $x^* = (2.903, ..., 2.903) $

In [4]:
def styblinski(x, y):
    return(1.0/2 * (x**4 - 16*x**2 + 5*x + y**4 - 16*y**2 + 5*y))

fig = plt.figure(figsize=(10, 7))
ax = fig.gca(projection='3d')
x = np.linspace(-5, 5, 200)
y = x
X,Y = np.meshgrid(x, y)
Z = styblinski(X, Y)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm,
        linewidth=0, antialiased=False)

#ax.set_zlim(0, 0.2)

ax.zaxis.set_major_locator(plt.LinearLocator(10))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%.02f'))

plt.tick_params(axis='both', which='major', labelsize=8)
ax.tick_params(labelsize=8) 

fig.colorbar(surf, shrink=0.5, aspect=7, cmap=plt.cm.coolwarm)

plt.show()

3. Eggholder Function

$ f(x) = -(x_2 + 47) \sin\left(\sqrt{|x_2 + \frac{x_1}{2} + 47|}\right) - x_1 \sin{\left(\sqrt{|x_1 - (x_2 + 47)|}\right)} $

Range of dimensions: $ x_i \in \left[ -512, 512\right] $

In [6]:
def eggholder(x, y):
    return(-(y + 47)*np.sin(np.sqrt(np.abs(y+x/2+47))) - x*np.sin(np.sqrt(np.abs(x - (y+47)))))

fig = plt.figure(figsize=(10, 7))
ax = fig.gca(projection='3d')
x = np.linspace(-256, 256, 200)
y = x
X,Y = np.meshgrid(x, y)
Z = eggholder(X, Y)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm,
        linewidth=0, antialiased=False)

#ax.set_zlim(0, 0.2)

ax.zaxis.set_major_locator(plt.LinearLocator(10))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%.02f'))

plt.tick_params(axis='both', which='major', labelsize=8)
ax.tick_params(labelsize=8) 

fig.colorbar(surf, shrink=0.5, aspect=7, cmap=plt.cm.coolwarm)

plt.show()

4. Misc. Functions

$ f(x) = -\frac{x_1^2 + 3 x_2^2}{0.1 + x_1^2 + x_2^2} - \left(x_1^2 + 5 x_2^2\right)\frac{\exp{\left(1 - x_1^2 - x_2^2\right)}}{2} $

In [4]:
def myfunc(x, y):
    r2 = x**2 + y**2
    return(-(x**2 + 3*y**2)/(0.1 + r2) - (x**2 + 5*y**2)*np.exp(1-r2)/2)

fig = plt.figure(figsize=(10, 7))
ax = fig.gca(projection='3d')
x = np.linspace(-5, 5, 200)
y = x
X,Y = np.meshgrid(x, y)
Z = myfunc(X, Y)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm,
        linewidth=0, antialiased=False)

#ax.set_zlim(0, 0.2)

ax.zaxis.set_major_locator(plt.LinearLocator(10))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%.02f'))

plt.tick_params(axis='both', which='major', labelsize=8)
ax.tick_params(labelsize=8) 

fig.colorbar(surf, shrink=0.5, aspect=7, cmap=plt.cm.coolwarm)

plt.show()
In []: