from spotoptim.function import ackley
import numpy as np
X = np.array([0.0, 0.0, 0.0])
ackley(X)array([4.4408921e-16])
function.so
Analytical single-objective test functions for optimization benchmarking.
This module provides well-known analytical test functions commonly used for evaluating and benchmarking optimization algorithms.
| Name | Description |
|---|---|
| ackley | N-dimensional Ackley function. |
| lennard_jones | Lennard-Jones Atomic Cluster Potential Energy. |
| michalewicz | N-dimensional Michalewicz function. |
| noisy_sphere | N-dimensional Sphere function with noise. |
| robot_arm_hard | 10-Link Robot Arm with Maze-Like Hard Constraints. |
| robot_arm_obstacle | 10-Link Planar Robot Arm Inverse Kinematics with Obstacle Avoidance. |
| rosenbrock | N-dimensional Rosenbrock function. |
| sphere | N-dimensional Sphere function. |
| wingwt | Aircraft Wing Weight function. |
N-dimensional Ackley function.
The Ackley function is a widely used test function for optimization algorithms. It is characterized by a nearly flat outer region and a large hole at the center. The function is multimodal with many local minima but only one global minimum.
f(X) = -a * exp(-b * sqrt(sum(x_i^2) / n)) - exp(sum(cos(c * x_i)) / n) + a + e
| Name | Type | Description | Default |
|---|---|---|---|
| X | array - like |
Input points with shape (n_samples, n_features) or (n_features,). Can be a 1D array for a single point or 2D array for multiple points. | required |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Function values at the input points with shape (n_samples,). |
Single point evaluation at global minimum:
array([4.4408921e-16])
Multiple points evaluation:
Ackley, D. H. (1987). “A connectionist machine for genetic hillclimbing”. Kluwer Academic Publishers.
Lennard-Jones Atomic Cluster Potential Energy.
Calculates the potential energy of a cluster of N atoms interacting via the Lennard-Jones potential. The optimization problem involves finding the atomic coordinates that minimize the total potential energy. This is a classic benchmark problem known for its high difficulty due to the exponential growth of local minima with N.
The function accepts inputs in the range [0, 1] and internally maps them to the search domain [-2, 2] for each coordinate.
| Name | Type | Description | Default |
|---|---|---|---|
| X | np.ndarray | Input points. - Shape (n_samples, 3 * n_atoms) for batch evaluation. - Shape (3 * n_atoms,) for single evaluation. The input represents the flattened [x1, y1, z1, x2, y2, z2, …] coordinates. | required |
| n_atoms | int | Number of atoms in the cluster. Defaults to 13. | 13 |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Potential energy values. Shape (n_samples,). |
| Name | Type | Description |
|---|---|---|
| ValueError | If input dimensions do not match 3 * n_atoms. |
Single point evaluation (random configuration):
array([74.35227958])
Batch evaluation:
Wales, D. J., & Doye, J. P. (1997). Global optimization by basin-hopping and the lowest energy structures of Lennard-Jones clusters containing up to 110 atoms. The Journal of Physical Chemistry A, 101(28), 5111-5116.
N-dimensional Michalewicz function.
The Michalewicz function is a multimodal test function with steep ridges and valleys. The parameter m defines the steepness of the valleys and ridges. Larger values of m result in more difficult search problems. The number of local minima increases exponentially with the dimension.
f(X) = -sum_{i=1}^{n} sin(x_i) * [sin(i * x_i^2 / π)]^(2m)
| Name | Type | Description | Default |
|---|---|---|---|
| X | array - like |
Input points with shape (n_samples, n_features) or (n_features,). Can be a 1D array for a single point or 2D array for multiple points. | required |
| m | int | Steepness parameter. Higher values make the function more difficult to optimize. Defaults to 10. | 10 |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Function values at the input points with shape (n_samples,). |
Single point evaluation:
np.float64(-1.801140718473825)
Multiple points evaluation:
array([-1.80114072e+00, -2.55738728e-05])
Using different steepness parameter:
Michalewicz, Z. (1996). “Genetic Algorithms + Data Structures = Evolution Programs”. Springer-Verlag.
N-dimensional Sphere function with noise.
| Name | Type | Description | Default |
|---|---|---|---|
| X | array - like |
Input points with shape (n_samples, n_features) or (n_features,). Can be a 1D array for a single point or 2D array for multiple points. | required |
| sigma | float | Standard deviation of the noise. Defaults to 0.1. | 0.1 |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Function values at the input points with shape (n_samples,). |
| Name | Type | Description |
|---|---|---|
| ValueError | If X has fewer than 1 dimension. |
array([1.92996718])
10-Link Robot Arm with Maze-Like Hard Constraints.
A challenging constrained optimization problem where a 10-link planar robot arm must reach a target point (5.0, 5.0) while avoiding multiple obstacles forming a maze-like environment. This function tests an optimizer’s ability to handle hard constraints and navigate complex feasible regions.
The problem features three main difficulty factors: 1. ‘The Great Wall’: A vertical barrier at x=2.5 blocking direct paths 2. ‘The Ceiling’: A horizontal bar at y=8.5 preventing high loop strategies 3. ‘The Target Trap’: Obstacles surrounding the target, requiring precise approach
f(X) = distance_cost + constraint_penalty + energy_regularization
| Name | Type | Description | Default |
|---|---|---|---|
| X | np.ndarray | Input points with shape (n_samples, 10) or (10,). Each sample contains 10 joint angles normalized to [0, 1], which are internally mapped to [-1.2π, 1.2π] to allow looping strategies. Can be a 1D array for a single point or 2D array for multiple points. | required |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Function values at the input points with shape (n_samples,). Lower values indicate better solutions (closer to target with fewer constraint violations). |
Single point evaluation with random configuration:
(1,)
Multiple points evaluation:
array([269.77734305, 144.65586174])
Evaluating a straight configuration (all angles = 0.5, mapped to 0 radians):
This function is inspired by robot motion planning problems with obstacles, commonly studied in:
10-Link Planar Robot Arm Inverse Kinematics with Obstacle Avoidance.
The goal is to minimize the distance of the end-effector to a target point while avoiding collision with a set of circular obstacles. This problem mimics a real-world inverse kinematics solver for a redundant manipulator.
The function accepts inputs in the range [0, 1] and internally maps them to the search domain [-pi, pi] for each joint angle (radians).
| Name | Type | Description | Default |
|---|---|---|---|
| X | np.ndarray | Input angles (normalized). - Shape (n_samples, 10). The input contains the normalized relative angles for the 10 links. | required |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Cost values (Weighted sum of Distance + Penalty). Shape (n_samples,). |
| Name | Type | Description |
|---|---|---|
| ValueError | If input dimensions do not match 10. |
Single point evaluation:
array([60.85746763])
Batch evaluation:
N-dimensional Rosenbrock function.
The Rosenbrock function is a classic test function for optimization algorithms. It is characterized by a long, narrow, parabolic-shaped valley. The global minimum is inside the valley and is hard to find for many algorithms.
f(x, y) = (1 - x)^2 + 100 * (y - x2)2
f(X) = sum_{i=1}^{N-1} [100 * (x_{i+1} - x_i2)2 + (1 - x_i)^2]
| Name | Type | Description | Default |
|---|---|---|---|
| X | array - like |
Input points with shape (n_samples, n_features) or (n_features,). Can be a 1D array for a single point or 2D array for multiple points. | required |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Function values at the input points with shape (n_samples,). |
| Name | Type | Description |
|---|---|---|
| ValueError | If X has fewer than 2 dimensions. |
Single point evaluation:
array([0.])
Multiple points evaluation:
Rosenbrock, H.H. (1960). “An automatic method for finding the greatest or least value of a function”. The Computer Journal. 3 (3): 175–184.
N-dimensional Sphere function.
The Sphere function is a simple test function for optimization algorithms. It is characterized by a parabolic-shaped valley. The global minimum is inside the valley and is easy to find for many algorithms.
f(x, y) = x^2 + y^2
f(X) = sum_{i=1}^{N} x_i^2
| Name | Type | Description | Default |
|---|---|---|---|
| X | array - like |
Input points with shape (n_samples, n_features) or (n_features,). Can be a 1D array for a single point or 2D array for multiple points. | required |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Function values at the input points with shape (n_samples,). |
| Name | Type | Description |
|---|---|---|
| ValueError | If X has fewer than 1 dimension. |
array([2.])
Aircraft Wing Weight function.
The example models the weight of an unpainted light aircraft wing. The function accepts inputs in the unit cube [0,1]^9 and returns the wing weight.
| Name | Type | Description | Default |
|---|---|---|---|
| X | array - like |
Input points with shape (n_samples, 9) or (9,) or (10,) or (n_samples, 10). Input variables order: [Sw, Wfw, A, L, q, l, Rtc, Nz, Wdg, Wp(optional)] | required |
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: Wing weight values at the input points with shape (n_samples,). |
Single point evaluation (Baseline Cessna C172 - Unpainted):
array([233.90840542])
Batch evaluation:
Forrester, A., Sobester, A., & Keane, A. (2008). Engineering design via surrogate modelling: a practical guide. John Wiley & Sons.