pyswarms.utils.functions package

The mod:pyswarms.utils.functions module implements various test functions for optimization.

pyswarms.utils.functions.single_obj module

single_obj.py: collection of single-objective functions

All objective functions obj_func() must accept a (numpy.ndarray) with shape (n_particles, dimensions). Thus, each row represents a particle, and each column represents its position on a specific dimension of the search-space.

In this context, obj_func() must return an array j of size (n_particles, ) that contains all the computed fitness for each particle.

Whenever you make changes to this file via an implementation of a new objective function, be sure to perform unittesting in order to check if all functions implemented adheres to the design pattern stated above.

Function list: - Ackley’s, ackley - Beale, beale - Booth, booth - Bukin’s No 6, bukin6 - Cross-in-Tray, crossintray - Easom, easom - Eggholder, eggholder - Goldstein, goldstein - Himmelblau’s, himmelblau - Holder Table, holdertable - Levi, levi - Matyas, matyas - Rastrigin, rastrigin - Rosenbrock, rosenbrock - Schaffer No 2, schaffer2 - Sphere, sphere - Three Hump Camel, threehump

pyswarms.utils.functions.single_obj.ackley(x)[source]

Ackley’s objective function.

Has a global minimum of 0 at f(0,0,...,0) with a search domain of [-32, 32]

Parameters:x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)
Returns:computed cost of size (n_particles, )
Return type:numpy.ndarray
ValueError
When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.beale(x)[source]

Beale objective function.

Only takes two dimensions and has a global minimum of 0 at f([3,0.5]) Its domain is bounded between [-4.5, 4.5]

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.booth(x)[source]

Booth’s objective function.

Only takes two dimensions and has a global minimum of 0 at f([1,3]). Its domain is bounded between [-10, 10]

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.bukin6(x)[source]

Bukin N. 6 Objective Function

Only takes two dimensions and has a global minimum of 0 at f([-10,1]). Its coordinates are bounded by:

  • x[:,0] must be within [-15, -5]
  • x[:,1] must be within [-3, 3]
Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.crossintray(x)[source]

Cross-in-tray objective function.

Only takes two dimensions and has a four equal global minimums
of -2.06261 at f([1.34941, -1.34941]), f([1.34941, 1.34941]), f([-1.34941, 1.34941]), and f([-1.34941, -1.34941]).

Its coordinates are bounded within [-10,10].

Best visualized in the full domain and a range of [-2.0, -0.5].

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.easom(x)[source]

Easom objective function.

Only takes two dimensions and has a global minimum of -1 at f([pi, pi]). Its coordinates are bounded within [-100,100].

Best visualized in the domain of [-5, 5] and a range of [-1, 0.2].

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.eggholder(x)[source]

Eggholder objective function.

Only takes two dimensions and has a global minimum of -959.6407 at f([512, 404.3219]). Its coordinates are bounded within [-512, 512].

Best visualized in the full domain and a range of [-1000, 1000].

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.goldstein(x)[source]

Goldstein-Price’s objective function.

Only takes two dimensions and has a global minimum at f([0,-1]). Its domain is bounded between [-2, 2]

Best visualized in the domain of [-1.3,1.3] and range [-1,8000]

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.himmelblau(x)[source]

Himmelblau’s objective function

Only takes two dimensions and has a four equal global minimums
of zero at f([3.0,2.0]), f([-2.805118,3.131312]), f([-3.779310,-3.283186]), and f([3.584428,-1.848126]).

Its coordinates are bounded within [-5,5].

Best visualized with the full domain and a range of [0,1000]

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.holdertable(x)[source]

Holder Table objective function

Only takes two dimensions and has a four equal global minimums
of -19.2085 at f([8.05502, 9.66459]), f([-8.05502, 9.66459]), f([8.05502, -9.66459]), and f([-8.05502, -9.66459]).

Its coordinates are bounded within [-10, 10].

Best visualized with the full domain and a range of [-20, 0]

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.levi(x)[source]

Levi objective function

Only takes two dimensions and has a global minimum at f([1,1]). Its coordinates are bounded within [-10,10].

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.matyas(x)[source]

Matyas objective function

Only takes two dimensions and has a global minimum at f([0,0]). Its coordinates are bounded within [-10,10].

Parameters:x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)
Returns:
Return type:numpy.ndarray
pyswarms.utils.functions.single_obj.rastrigin(x)[source]

Rastrigin objective function.

Has a global minimum at f(0,0,...,0) with a search domain of [-5.12, 5.12]

Parameters:x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)
Returns:computed cost of size (n_particles, )
Return type:numpy.ndarray
Raises:ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.rosenbrock(x)[source]

Rosenbrock objective function.

Also known as the Rosenbrock’s valley or Rosenbrock’s banana function. Has a global minimum of np.ones(dimensions) where dimensions is x.shape[1]. The search domain is [-inf, inf].

Parameters:x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)
Returns:computed cost of size (n_particles, )
Return type:numpy.ndarray
pyswarms.utils.functions.single_obj.schaffer2(x)[source]

Schaffer N.2 objective function

Only takes two dimensions and has a global minimum at f([0,0]). Its coordinates are bounded within [-100,100].

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain
pyswarms.utils.functions.single_obj.sphere(x)[source]

Sphere objective function.

Has a global minimum at 0 and with a search domain of
[-inf, inf]
Parameters:x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)
Returns:computed cost of size (n_particles, )
Return type:numpy.ndarray
pyswarms.utils.functions.single_obj.threehump(x)[source]

Three-hump camel objective function

Only takes two dimensions and has a global minimum of 0 at f([0, 0]). Its coordinates are bounded within [-5, 5].

Best visualized in the full domin and a range of [0, 2000].

Parameters:

x (numpy.ndarray) – set of inputs of shape (n_particles, dimensions)

Returns:

computed cost of size (n_particles, )

Return type:

numpy.ndarray

Raises:
  • IndexError – When the input dimensions is greater than what the function allows
  • ValueError – When the input is out of bounds with respect to the function domain