Benchmark Functions
This page documents the benchmark functions available in Useful Optimizer for testing and comparing optimization algorithms.
Overview
All benchmark functions are located in opt.benchmark.functions and share the same interface:
from opt.benchmark.functions import sphere, rosenbrock, ackley
# Each function takes a numpy array and returns a scalar
import numpy as np
x = np.array([1.0, 2.0, 3.0])
fitness = sphere(x) # Returns: 14.0Unimodal Functions
Sphere Function
The simplest test function - a sum of squares.
| Property | Value |
|---|---|
| Optimum | |
| Bounds | |
| Modality | Unimodal |
| Separability | Separable |
from opt.benchmark.functions import sphere
import numpy as np
x = np.zeros(10)
print(sphere(x)) # 0.0Rosenbrock Function
A classic ill-conditioned function with a narrow valley.
| Property | Value |
|---|---|
| Optimum | |
| Bounds | |
| Modality | Unimodal |
| Separability | Non-separable |
from opt.benchmark.functions import rosenbrock
import numpy as np
x = np.ones(10)
print(rosenbrock(x)) # 0.0Multi-Modal Functions
Rastrigin Function
Highly multi-modal with regular local minima distribution.
| Property | Value |
|---|---|
| Optimum | |
| Bounds | |
| Modality | Multi-modal (~ |
| Separability | Separable |
from opt.benchmark.functions import rastrigin
import numpy as np
x = np.zeros(10)
print(rastrigin(x)) # 0.0Ackley Function
Multi-modal with a nearly flat outer region and central funnel.
| Property | Value |
|---|---|
| Optimum | |
| Bounds | |
| Modality | Multi-modal |
| Separability | Non-separable |
from opt.benchmark.functions import ackley
import numpy as np
x = np.zeros(10)
print(ackley(x)) # ≈ 0.0 (4.44e-16)Shifted Ackley Function
A shifted version of Ackley with non-centered optimum.
from opt.benchmark.functions import shifted_ackley
import numpy as np
# Optimum is shifted from origin
x = np.array([1.0, 1.0]) # Example shift
print(shifted_ackley(x))Griewank Function
Many regularly distributed local minima but increasingly flat in higher dimensions.
| Property | Value |
|---|---|
| Optimum | |
| Bounds | |
| Modality | Multi-modal |
| Separability | Non-separable |
from opt.benchmark.functions import griewank
import numpy as np
x = np.zeros(10)
print(griewank(x)) # 0.0Function Characteristics Summary
| Function | Unimodal | Separable | Difficulty |
|---|---|---|---|
| Sphere | ✅ | ✅ | Easy |
| Rosenbrock | ✅ | ❌ | Medium |
| Rastrigin | ❌ | ✅ | Hard |
| Ackley | ❌ | ❌ | Hard |
| Griewank | ❌ | ❌ | Medium |
Usage in Benchmarks
from opt.swarm_intelligence import ParticleSwarm
from opt.benchmark.functions import (
sphere,
rosenbrock,
rastrigin,
ackley,
griewank
)
functions = {
'sphere': {'func': sphere, 'bounds': (-5.12, 5.12)},
'rosenbrock': {'func': rosenbrock, 'bounds': (-5, 10)},
'rastrigin': {'func': rastrigin, 'bounds': (-5.12, 5.12)},
'ackley': {'func': ackley, 'bounds': (-32.768, 32.768)},
'griewank': {'func': griewank, 'bounds': (-600, 600)}
}
for name, config in functions.items():
optimizer = ParticleSwarm(
func=config['func'],
lower_bound=config['bounds'][0],
upper_bound=config['bounds'][1],
dim=10,
max_iter=100
)
_, fitness = optimizer.search()
print(f"{name}: {fitness:.6e}")Creating Custom Functions
You can create custom objective functions for your specific problems:
import numpy as np
from opt.swarm_intelligence import ParticleSwarm
def my_function(x: np.ndarray) -> float:
"""Custom objective function.
Args:
x: Input vector of shape (n,)
Returns:
Scalar fitness value (lower is better)
"""
return np.sum(x**2) + 10 * np.sin(np.sum(x))
optimizer = ParticleSwarm(
func=my_function,
lower_bound=-10.0,
upper_bound=10.0,
dim=10,
max_iter=100
)
best_solution, best_fitness = optimizer.search()References
Jamil, M., & Yang, X. S. (2013). A literature survey of benchmark functions for global optimization problems. IJMMNO, 4(2), 150-194.
Suganthan, P. N., et al. (2005). Problem definitions and evaluation criteria for the CEC 2005 special session on real-parameter optimization. KanGAL Report, 2005.