Gravitational Search Algorithm β
Physics-Inspired
Gravitational Search Algorithm (GSA) optimization algorithm.
Algorithm Overview β
This module implements the Gravitational Search Algorithm, a physics-inspired metaheuristic based on Newton's law of gravity and laws of motion.
Objects (solutions) attract each other with gravitational forces proportional to their mass (fitness) and inversely proportional to distance. Heavier masses (better solutions) attract lighter masses (worse solutions).
Usage β
from opt.physics_inspired.gravitational_search import GravitationalSearchOptimizer
from opt.benchmark.functions import sphere
optimizer = GravitationalSearchOptimizer(
func=sphere,
lower_bound=-5.12,
upper_bound=5.12,
dim=10,
max_iter=500,
population_size=50,
)
best_solution, best_fitness = optimizer.search()
print(f"Best solution: {best_solution}")
print(f"Best fitness: {best_fitness:.6e}")Parameters β
| Parameter | Type | Default | Description |
|---|---|---|---|
func | Callable | Required | Objective function to minimize. |
lower_bound | float | Required | Lower bound of search space. |
upper_bound | float | Required | Upper bound of search space. |
dim | int | Required | Problem dimensionality. |
max_iter | int | 1000 | Maximum iterations. |
seed | int | None | None | Random seed for reproducibility. |
population_size | int | 100 | Population size (number of agents). |
g0 | float | _GRAVITATIONAL_CONSTANT_INITIAL | Initial gravitational constant |
alpha | float | _GRAVITATIONAL_DECAY_RATE | Decay rate |
Algorithm Metadata β
| Property | Value |
|---|---|
| Algorithm Name | Gravitational Search Algorithm |
| Acronym | GSA |
| Year Introduced | 2009 |
| Authors | Rashedi, Esmat; Nezamabadi-Pour, Hossein; Saryazdi, Saeid |
| Algorithm Class | Physics-Inspired |
| Complexity | O(NΒ² |
| Properties | Population-based, Derivative-free, Stochastic |
| Implementation | Python 3.10+ |
| COCO Compatible | Yes |
Mathematical Formulation β
GSA is based on Newton's law of gravity and laws of motion. Each agent (solution) has a mass proportional to its fitness, and agents attract each other through gravitational forces.
Gravitational constant (time-dependent decay):
Mass calculation (fitness-based):
Gravitational force from agent
Total force on agent
Acceleration (Newton's second law:
Velocity update:
Position update:
where:
is the gravitational constant at iteration is the initial gravitational constant (default: 100.0) is the decay rate (default: 20.0) is the mass of agent at iteration is the fitness of agent is the Euclidean distance is a small constant to avoid division by zero ( ) is the set of K best agents (decreases over time) are random numbers in is the dimension index
Constraint handling:
- Boundary conditions: Clamping to
- Feasibility enforcement: Solutions violating bounds are projected back to the nearest boundary using
np.clip
Hyperparameters β
| Parameter | Default | BBOB Recommended | Description |
|---|---|---|---|
| population_size | 100 | 10*dim | Number of agents (candidate solutions) in population |
| max_iter | 1000 | 10000 | Maximum number of iterations for optimization |
| g0 | 100.0 | 100.0 | Initial gravitational constant controlling force strength |
| alpha | 20.0 | 20.0 | Exponential decay rate for gravitational constant G(t) |
Sensitivity Analysis:
population_size: Medium impact on convergence. Larger populations provide better exploration but increase computational cost.g0: Low impact. Controls initial attraction strength.alpha: High impact. Controls exploration-exploitation balance. Higher values decay gravity faster (more exploitation).- Recommended tuning ranges:
,
COCO/BBOB Benchmark Settings β
Search Space:
- Dimensions tested:
2, 3, 5, 10, 20, 40 - Bounds: Function-specific (typically
[-5, 5]or[-100, 100]) - Instances: 15 per function (BBOB standard)
Evaluation Budget:
- Budget:
function evaluations - Independent runs: 15 (for statistical significance)
- Seeds:
0-14(reproducibility requirement)
Performance Metrics:
- Target precision:
1e-8(BBOB default) - Success rate at precision thresholds:
[1e-8, 1e-6, 1e-4, 1e-2] - Expected Running Time (ERT) tracking
Raises β
ValueError: If search space is invalid or function evaluation fails.
Notes β
- Modifies self.history if track_history=True
- Uses self.seed for all random number generation
- BBOB: Returns final best solution after max_iter or convergence
Computational Complexity:
- Time per iteration:
due to pairwise force calculations between all agents - Space complexity:
for population storage - BBOB budget usage: Typically uses 60-80% of dim
10000 budget for convergence
BBOB Performance Characteristics:
- Best function classes: Unimodal, Separable functions (Sphere, Ellipsoid)
- Weak function classes: Highly multimodal functions with many local optima, Ill-conditioned problems (Rosenbrock, Rastrigin)
- Typical success rate at 1e-8 precision: 45-55% (dim=5)
- Expected Running Time (ERT): Moderate to high compared to gradient-based methods
Convergence Properties:
- Convergence rate: Exponential in early iterations, slows to linear
- Local vs Global: Tendency for global exploration early, local exploitation late
- Premature convergence risk: Medium - Kbest mechanism helps maintain diversity
Reproducibility:
- Deterministic: Yes - Same seed guarantees same results
- BBOB compliance: seed parameter required for 15 independent runs
- Initialization: Uniform random sampling in
[lower_bound, upper_bound] - RNG usage:
numpy.random.default_rng(self.seed)throughout
Implementation Details:
- Parallelization: Not supported in current implementation
- Constraint handling: Clamping to bounds using
np.clip - Numerical stability: Uses epsilon (
) to avoid division by zero in distance and mass calculations
Known Limitations:
complexity makes it slow for large populations- Performance degrades on ill-conditioned and highly multimodal functions
- BBOB known issues: May require many iterations on rotated/shifted functions
Version History:
- v0.1.0: Initial implementation
- v0.1.2: Added BBOB compliance and improved docstrings
References β
[1] Rashedi, E., Nezamabadi-Pour, H., & Saryazdi, S. (2009). "GSA: A Gravitational Search Algorithm." Information Sciences, 179(13), 2232-2248. https://doi.org/10.1016/j.ins.2009.03.004
[2] Hansen, N., Auger, A., Ros, R., Mersmann, O., TuΕ‘ar, T., Brockhoff, D. (2021). "COCO: A platform for comparing continuous optimizers in a black-box setting." Optimization Methods and Software, 36(1), 114-144. https://doi.org/10.1080/10556788.2020.1808977
COCO Data Archive:
- Benchmark results: https://coco-platform.org/testsuites/bbob/data-archive.html
- Algorithm data: Not yet available in COCO archive
- Code repository: https://github.com/Anselmoo/useful-optimizer
Implementation:
- Original paper code: Not publicly available
- This implementation: Based on [1] with modifications for BBOB compliance
See Also β
EquilibriumOptimizer: Another physics-inspired algorithm based on mass balance BBOB Comparison: Generally faster on unimodal functions
AtomSearchOptimizer: Molecular dynamics-based algorithm using Lennard-Jones BBOB Comparison: Similar performance on multimodal functions
AbstractOptimizer: Base class for all optimizers opt.benchmark.functions: BBOB-compatible test functions
Related BBOB Algorithm Classes:
- Physics: EquilibriumOptimizer, AtomSearchOptimizer, RIMEOptimizer
- Swarm: ParticleSwarm, AntColony
- Evolutionary: GeneticAlgorithm, DifferentialEvolution
Benchmark Performance β
Results of the benchmark suite for this optimizer, on the same evaluation budget as every other optimizer and compared with a random search baseline. See the methodology for the protocol and statistics.
Related Pages β
Source Code
View the implementation: gravitational_search.py