fbench#

Submodules#

Package Contents#

Functions#

ackley(x, /)

Ackley function.

beale(x, /)

Beale function.

check_vector(x, /, *, n_min=1, n_max=np.inf)

Validate \(n\)-vector.

get_optima(n, /, func)

Retrieve optima for defined functions.

peaks(x, /)

Peaks function.

rastrigin(x, /)

Rastrigin function.

rosenbrock(x, /)

Rosenbrock function.

schwefel(x, /)

Schwefel function.

sinc(x, /)

Sinc function.

sphere(x, /)

Sphere function.

Attributes#

fbench.__version__#
fbench.ackley(x, /)[source]#

Ackley function.

A function \(f\colon \mathbb{R}^{n} \rightarrow \mathbb{R}\) that takes an \(n\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = -20 \exp \left( -0.2 \sqrt{ \frac{1}{n} \sum_{i=1}^{n} x_i^2 } \right) - \exp \left( \frac{1}{n} \sum_{i=1}^{n} \cos(2 \pi x_i) \right) + 20 + e\]
Parameters

x (array_like) – The \(n\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Test functions for optimization”, Wikipedia, https://en.wikipedia.org/wiki/Test_functions_for_optimization

Examples

>>> import fbench
>>> round(fbench.ackley([0, 0]), 4)
0.0
>>> round(fbench.ackley([1, 2]), 4)
5.4221
>>> round(fbench.ackley([1, 2, 3]), 4)
7.0165
fbench.beale(x, /)[source]#

Beale function.

A function \(f\colon \mathbb{R}^{2} \rightarrow \mathbb{R}\) that takes a \(2\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = \left( 1.5 - x_{1} + x_{1} x_{2} \right)^{2} + \left( 2.25 - x_{1} + x_{1} x_{2}^{2} \right)^{2} + \left( 2.625 - x_{1} + x_{1} x_{2}^{3}\right)^{2}\]
Parameters

x (array_like) – The \(2\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Test functions for optimization”, Wikipedia, https://en.wikipedia.org/wiki/Test_functions_for_optimization

2

“Beale function”, Virtual Library of Simulation Experiments: Test Functions and Datasets, https://www.sfu.ca/~ssurjano/beale.html

Examples

>>> import fbench
>>> fbench.beale([3, 0.5])
0.0
>>> round(fbench.beale([0, 0]), 4)
14.2031
>>> round(fbench.beale([1, 1]), 4)
14.2031
>>> round(fbench.beale([2, 2]), 4)
356.7031
fbench.check_vector(x, /, *, n_min=1, n_max=np.inf)[source]#

Validate \(n\)-vector.

Parameters
  • x (array_like) – The input object to be validated to represent an \(n\)-vector.

  • n_min (int, default=1) – Specify the minimum number of \(n\).

  • n_max (int, default=inf) – Specify the maximum number of \(n\).

Returns

The \(n\)-vector.

Return type

np.ndarray

Raises

TypeError

  • If x is not vector-like.

  • If n is not between n_min and n_max.

Examples

>>> import fbench
>>> fbench.check_vector([0, 0])
array([0, 0])
fbench.get_optima(n, /, func)[source]#

Retrieve optima for defined functions.

Parameters
  • n (int) – Specify the number of dimensions \(n\).

  • func (callable) – A fBench function to retrieve its optima. None is returned if no optima is defined.

Returns

Optima with specified dimension for fBench function if defined.

Return type

Optional[list[Optimum]]]

Notes

  • Function is curried.

  • Optima are defined for the following functions:
    • ackley

    • beale

    • peaks

    • rastrigin

    • rosenbrock

    • schwefel

    • sinc

    • sphere

Examples

>>> import fbench
>>> optima = fbench.get_optima(5, fbench.sphere)
>>> optima
[Optimum(x=array([0, 0, 0, 0, 0]), fx=0)]
>>> optimum = optima[0]
>>> optimum.n
5
fbench.peaks(x, /)[source]#

Peaks function.

A function \(f\colon \mathbb{R}^{2} \rightarrow \mathbb{R}\) that takes a \(2\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = 3 (1 - x_{1})^{2} \exp\left( - x_{1}^{2} - (x_{2} + 1)^{2} \right) - 10 \left( \frac{x_{1}}{5} - x_{1}^{3} - x_{2}^{5} \right) \exp\left( - x_{1}^{2} - x_{2}^{2} \right) - \frac{1}{3} \exp\left( - (x_{1} + 1)^{2} - x_{2}^{2} \right)\]
Parameters

x (array_like) – The \(2\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

Examples

>>> import fbench
>>> round(fbench.peaks([0, 0]), 4)
0.981
fbench.rastrigin(x, /)[source]#

Rastrigin function.

A function \(f\colon \mathbb{R}^{n} \rightarrow \mathbb{R}\) that takes an \(n\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = 10n + \sum_{i=1}^{n} \left( x_i^2 - 10 \cos(2 \pi x_i) \right)\]
Parameters

x (array_like) – The \(n\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Test functions for optimization”, Wikipedia, https://en.wikipedia.org/wiki/Test_functions_for_optimization

Examples

>>> import fbench
>>> round(fbench.rastrigin([0, 0]), 4)
0.0
>>> round(fbench.rastrigin([1, 2]), 4)
5.0
>>> round(fbench.rastrigin([4.5, 4.5]), 4)
80.5
>>> round(fbench.rastrigin([1, 2, 3]), 4)
14.0
fbench.rosenbrock(x, /)[source]#

Rosenbrock function.

A function \(f\colon \mathbb{R}^{n} \rightarrow \mathbb{R}\) that takes an \(n\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = \sum_{i=1}^{n-1} \left( 100 (x_{i+1} - x_i^2)^2 + (1 - x_i)^2 \right)\]
Parameters

x (array_like) – The \(n\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Test functions for optimization”, Wikipedia, https://en.wikipedia.org/wiki/Test_functions_for_optimization

Examples

>>> import fbench
>>> round(fbench.rosenbrock([0, 0]), 4)
1.0
>>> round(fbench.rosenbrock([1, 1]), 4)
0.0
>>> round(fbench.rosenbrock([1, 1, 1]), 4)
0.0
>>> round(fbench.rosenbrock([1, 2, 3]), 4)
201.0
>>> round(fbench.rosenbrock([3, 3]), 4)
3604.0
fbench.schwefel(x, /)[source]#

Schwefel function.

A function \(f\colon \mathbb{R}^{n} \rightarrow \mathbb{R}\) that takes an \(n\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = 418.9829 n - \sum_{i=1}^{n} x_{i} \sin\left( \sqrt{|x_{i}|} \right)\]
Parameters

x (array_like) – The \(n\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Schwefel function”, Virtual Library of Simulation Experiments: Test Functions and Datasets, https://www.sfu.ca/~ssurjano/schwef.html

Examples

>>> import fbench
>>> round(fbench.schwefel([420.9687]), 4)
0.0
>>> round(fbench.schwefel([0, 0]), 4)
837.9658
>>> round(fbench.schwefel([1, 2]), 4)
835.1488
>>> round(fbench.schwefel([1, 2, 3]), 4)
1251.1706
fbench.sinc(x, /)[source]#

Sinc function.

A function \(f\colon \mathbb{R}^{1} \rightarrow \mathbb{R}\) that takes an \(1\)-vector as input and returns a scalar value.

\[\begin{split}f(\mathbf{x}) = \begin{cases} \frac{\sin(x)}{x} & \text{ if } x \neq 0 \\ 1 & \text{ if } x = 0 \end{cases}\end{split}\]
Parameters

x (array_like) – The \(1\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Sinc Function”, Wolfram MathWorld, https://mathworld.wolfram.com/SincFunction.html

Examples

>>> import fbench
>>> fbench.sinc([0])
1.0
>>> round(fbench.sinc([1]), 4)
0.8415
fbench.sphere(x, /)[source]#

Sphere function.

A function \(f\colon \mathbb{R}^{n} \rightarrow \mathbb{R}\) that takes an \(n\)-vector as input and returns a scalar value.

\[f(\mathbf{x}) = \sum_{i=1}^{n} x_i^2\]
Parameters

x (array_like) – The \(n\)-vector.

Returns

Function value at \(\mathbf{x}\).

Return type

float

References

1

“Test functions for optimization”, Wikipedia, https://en.wikipedia.org/wiki/Test_functions_for_optimization

Examples

>>> import fbench
>>> fbench.sphere([0, 0])
0.0
>>> fbench.sphere([1, 1])
2.0
>>> fbench.sphere([1, 2, 3])
14.0