swarmtorch 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- swarmtorch-0.1.0/PKG-INFO +25 -0
- swarmtorch-0.1.0/README.md +196 -0
- swarmtorch-0.1.0/pyproject.toml +50 -0
- swarmtorch-0.1.0/setup.cfg +4 -0
- swarmtorch-0.1.0/swarmtorch.egg-info/PKG-INFO +25 -0
- swarmtorch-0.1.0/swarmtorch.egg-info/SOURCES.txt +8 -0
- swarmtorch-0.1.0/swarmtorch.egg-info/dependency_links.txt +1 -0
- swarmtorch-0.1.0/swarmtorch.egg-info/requires.txt +10 -0
- swarmtorch-0.1.0/swarmtorch.egg-info/top_level.txt +1 -0
- swarmtorch-0.1.0/tests/test_generic_search.py +31 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: swarmtorch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Professional PyTorch library for 120+ metaheuristic optimization algorithms (Swarm, Evolutionary, Physics, Hybrid).
|
|
5
|
+
Author-email: Halleluyah Darasimi Oludele <hallelx2@gmail.com>
|
|
6
|
+
Keywords: pytorch,metaheuristics,swarm-intelligence,evolutionary-algorithms,hpo,gradient-free
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Science/Research
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
Requires-Dist: torch>=2.0.0
|
|
18
|
+
Requires-Dist: numpy>=1.21.0
|
|
19
|
+
Requires-Dist: pandas>=1.3.0
|
|
20
|
+
Requires-Dist: matplotlib>=3.5.0
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pyright>=1.1.0; extra == "dev"
|
|
25
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# SwarmTorch 🐝🔥: Professional Metaheuristic Optimization for PyTorch
|
|
2
|
+
|
|
3
|
+
**SwarmTorch** is a production-grade, high-performance library designed to bridge the gap between metaheuristic optimization and deep learning. It provides a massive collection of **over 120 algorithms** (59 Model Weight Optimizers and 59 Hyperparameter Tuners) that are fully compatible with the PyTorch ecosystem.
|
|
4
|
+
|
|
5
|
+
Whether you are performing **Gradient-Free Neural Network Training** or **Automated Hyperparameter Optimization (HPO)**, SwarmTorch offers a unified interface to the world's most powerful swarm and evolutionary algorithms.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 🏛️ Project Architecture & Taxonomy
|
|
10
|
+
|
|
11
|
+
The library is logically structured into 6 core categories, each reflecting a unique mathematical or biological inspiration. This organization allows researchers to easily compare different families of algorithms.
|
|
12
|
+
|
|
13
|
+
### 1. Swarm Intelligence (`swarmtorch.swarm`)
|
|
14
|
+
|
|
15
|
+
*The flagship category, modeling decentralized, self-organized collective behaviors.*
|
|
16
|
+
|
|
17
|
+
- **Top Algorithms:** PSO (Particle Swarm), GWO (Grey Wolf), HHO (Harris Hawks), SSA (Salp Swarm).
|
|
18
|
+
- **Best For:** Global search in high-dimensional spaces.
|
|
19
|
+
|
|
20
|
+
### 2. Evolutionary Algorithms (`swarmtorch.evolutionary`)
|
|
21
|
+
|
|
22
|
+
*Based on the mechanisms of natural selection and biological evolution.*
|
|
23
|
+
|
|
24
|
+
- **Top Algorithms:** GA (Genetic Algorithm), DE (Differential Evolution), CEM (Cross-Entropy Method).
|
|
25
|
+
- **Best For:** Robust, mutation-driven exploration.
|
|
26
|
+
|
|
27
|
+
### 3. Physics-Based (`swarmtorch.physics`)
|
|
28
|
+
|
|
29
|
+
*Algorithms derived from the fundamental laws of the physical world.*
|
|
30
|
+
|
|
31
|
+
- **Top Algorithms:** SA (Simulated Annealing), GSA (Gravitational Search), FPA (Flower Pollination).
|
|
32
|
+
- **Best For:** Problems with well-defined energy or force landscapes.
|
|
33
|
+
|
|
34
|
+
### 4. Human-Based (`swarmtorch.human_based`)
|
|
35
|
+
|
|
36
|
+
*Simulates human social interactions, teaching, and learning processes.*
|
|
37
|
+
|
|
38
|
+
- **Top Algorithms:** TLBO (Teaching-Learning-Based), Harmony Search.
|
|
39
|
+
- **Best For:** Knowledge-sharing-driven convergence.
|
|
40
|
+
|
|
41
|
+
### 5. Bio-Inspired (`swarmtorch.bio_inspired`)
|
|
42
|
+
|
|
43
|
+
*General biological models and life-cycle simulations.*
|
|
44
|
+
|
|
45
|
+
- **Top Algorithms:** ALO (Ant Lion), BBO (Biogeography-Based), MVO (Multi-Verse).
|
|
46
|
+
- **Best For:** Specialized niche optimization tasks.
|
|
47
|
+
|
|
48
|
+
### 6. Hybrid Algorithms (`swarmtorch.hybrid`)
|
|
49
|
+
|
|
50
|
+
*Advanced optimizers that combine multiple strategies for superior convergence.*
|
|
51
|
+
|
|
52
|
+
- **Top Algorithms:** SMA (Slime Mold), Gorilla Optimizer, Cat Swarm.
|
|
53
|
+
- **Best For:** Complex, multimodal surfaces where single-strategy algorithms might stall.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 🚀 Installation & Integration
|
|
58
|
+
|
|
59
|
+
### Setup
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# Clone and install as a development package
|
|
63
|
+
git clone https://github.com/your-repo/swarmtorch.git
|
|
64
|
+
cd swarmtorch
|
|
65
|
+
pip install -e .
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 1. Model Weight Optimization (Gradient-Free)
|
|
69
|
+
|
|
70
|
+
Train any PyTorch model without using `loss.backward()`. This is ideal for non-differentiable objectives or when exploring global landscapes.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import torch
|
|
74
|
+
import torch.nn as nn
|
|
75
|
+
from swarmtorch.swarm.model_training import PSO
|
|
76
|
+
|
|
77
|
+
# Define your standard PyTorch model
|
|
78
|
+
model = nn.Sequential(
|
|
79
|
+
nn.Linear(10, 64),
|
|
80
|
+
nn.ReLU(),
|
|
81
|
+
nn.Linear(64, 1)
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# Initialize a SwarmTorch Optimizer
|
|
85
|
+
# Note: uses_gradients = False
|
|
86
|
+
optimizer = PSO(model.parameters(), swarm_size=30)
|
|
87
|
+
criterion = nn.BCEWithLogitsLoss()
|
|
88
|
+
|
|
89
|
+
def closure():
|
|
90
|
+
optimizer.zero_grad()
|
|
91
|
+
outputs = model(inputs)
|
|
92
|
+
loss = criterion(outputs, targets)
|
|
93
|
+
# NO loss.backward() needed!
|
|
94
|
+
return loss
|
|
95
|
+
|
|
96
|
+
# Perform an optimization step
|
|
97
|
+
optimizer.step(closure)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 2. Hyperparameter Optimization (HPO)
|
|
101
|
+
|
|
102
|
+
Optimize architectural choices and training parameters (learning rates, layer sizes) using intelligent searchers instead of grid or random search.
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from swarmtorch.swarm.hyperparameter_tuning import PSOSearch
|
|
106
|
+
|
|
107
|
+
def build_model(params):
|
|
108
|
+
return nn.Linear(10, int(params['hidden_dim']))
|
|
109
|
+
|
|
110
|
+
def train_fn(model, params):
|
|
111
|
+
# Train and return validation loss/error to minimize
|
|
112
|
+
# Higher performance = Lower return value
|
|
113
|
+
return validation_error
|
|
114
|
+
|
|
115
|
+
param_space = {
|
|
116
|
+
'lr': (0.001, 0.1), # Continuous
|
|
117
|
+
'hidden_dim': [32, 64, 128], # Discrete
|
|
118
|
+
'dropout': (0.0, 0.5) # Continuous
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
searcher = PSOSearch(
|
|
122
|
+
model_fn=build_model,
|
|
123
|
+
param_space=param_space,
|
|
124
|
+
train_fn=train_fn,
|
|
125
|
+
iterations=50,
|
|
126
|
+
swarm_size=20
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
best_params = searcher.search()
|
|
130
|
+
print(f"Optimal Configuration: {best_params}")
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## 📈 Research & Benchmarking Results
|
|
136
|
+
|
|
137
|
+
Our library has undergone a massive-scale rigorous evaluation of **118 algorithms** to benchmark their performance across various deep learning tasks.
|
|
138
|
+
|
|
139
|
+
### 1. Model Training Benchmarks (Weight Optimization)
|
|
140
|
+
SwarmTorch enables the training of neural networks without gradients. Our tests on non-linear classification tasks show that several metaheuristics can achieve convergence comparable to standard gradient-based methods.
|
|
141
|
+
|
|
142
|
+

|
|
143
|
+
*Figure 1: Convergence history comparing Swarm Optimizers (PSO, HHO, etc.) against Adam and SGD. Many swarm algorithms exhibit highly stable descent.*
|
|
144
|
+
|
|
145
|
+

|
|
146
|
+
*Figure 2: The Top 25 most effective weight optimizers ranked by final loss. Hybrid and Swarm Intelligence algorithms show the strongest performance.*
|
|
147
|
+
|
|
148
|
+
### 2. High-Density Distribution Analysis
|
|
149
|
+
We analyzed the reliability of each category. Swarm and Hybrid categories demonstrated the highest stability and lowest variance across multiple trials.
|
|
150
|
+
|
|
151
|
+

|
|
152
|
+
*Figure 3: Statistical distribution of final loss across categories. Lower loss and tighter boxes indicate superior and more reliable optimization.*
|
|
153
|
+
|
|
154
|
+
### 3. Hyperparameter Optimization (HPO) Benchmarks
|
|
155
|
+
Our metaheuristic searchers are designed to replace Random Search with more intelligent exploration strategies.
|
|
156
|
+
|
|
157
|
+

|
|
158
|
+
*Figure 4: Success rate of metaheuristic searchers vs. the Random Search baseline. Over 94% of our algorithms outperformed Random Search.*
|
|
159
|
+
|
|
160
|
+
### 4. The "Generalist" Frontier
|
|
161
|
+
By mapping Training Robustness against HPO Accuracy, we identified the most versatile algorithms in the library—those that excel in both weight optimization and hyperparameter tuning.
|
|
162
|
+
|
|
163
|
+

|
|
164
|
+
*Figure 5: Scatter plot mapping Training Efficiency vs. HPO Accuracy. Elite generalist algorithms occupy the top-right quadrant.*
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## 📁 Repository Structure
|
|
169
|
+
|
|
170
|
+
- `swarmtorch/`: The root package.
|
|
171
|
+
- `base/`: Core abstract classes and shared logic for all optimizers.
|
|
172
|
+
- `swarm/`, `evolutionary/`, `physics/`, etc.: Implementation of ~120 algorithms.
|
|
173
|
+
- **`benchmarks/`**: A self-contained research folder containing:
|
|
174
|
+
- `master_benchmark.py`: The full experimental suite.
|
|
175
|
+
- `COMPREHENSIVE_EXPERIMENT_REPORT.md`: Detailed per-algorithm results.
|
|
176
|
+
- All high-resolution visualization assets (.png).
|
|
177
|
+
- `tests/`: Automated unit tests for stability and regression.
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## 🤝 Acknowledgments & References
|
|
182
|
+
|
|
183
|
+
This library was developed with reference to the **[pyMetaheuristic](https://github.com/mariosv/pyMetaheuristic)** library. We are grateful for their contributions to the metaheuristic optimization community, which served as a foundational resource for the algorithmic implementations in **SwarmTorch**.
|
|
184
|
+
|
|
185
|
+
## 📝 Citation
|
|
186
|
+
|
|
187
|
+
If you use **SwarmTorch** in your research, please cite it as follows:
|
|
188
|
+
|
|
189
|
+
```bibtex
|
|
190
|
+
@software{swarmtorch2026,
|
|
191
|
+
author = {Halleluyah Darasimi Oludele},
|
|
192
|
+
title = {SwarmTorch: A PyTorch Library for 120+ Metaheuristic Optimization Algorithms},
|
|
193
|
+
year = {2026},
|
|
194
|
+
url = {https://github.com/hallelx2/swarmtorch}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "swarmtorch"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Professional PyTorch library for 120+ metaheuristic optimization algorithms (Swarm, Evolutionary, Physics, Hybrid)."
|
|
5
|
+
readme = "swarmtorch/README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "Halleluyah Darasimi Oludele", email = "hallelx2@gmail.com" }
|
|
9
|
+
]
|
|
10
|
+
keywords = ["pytorch", "metaheuristics", "swarm-intelligence", "evolutionary-algorithms", "hpo", "gradient-free"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Intended Audience :: Science/Research",
|
|
14
|
+
"Programming Language :: Python :: 3.10",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"torch>=2.0.0",
|
|
23
|
+
"numpy>=1.21.0",
|
|
24
|
+
"pandas>=1.3.0",
|
|
25
|
+
"matplotlib>=3.5.0",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
dev = [
|
|
30
|
+
"pytest>=7.0.0",
|
|
31
|
+
"ruff>=0.1.0",
|
|
32
|
+
"pyright>=1.1.0",
|
|
33
|
+
"build>=1.0.0",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["setuptools>=65.0", "wheel"]
|
|
38
|
+
build-backend = "setuptools.build_meta"
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.packages.find]
|
|
41
|
+
where = ["."]
|
|
42
|
+
include = ["swarmtorch*"]
|
|
43
|
+
|
|
44
|
+
[tool.ruff]
|
|
45
|
+
line-length = 88
|
|
46
|
+
target-version = "py310"
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
testpaths = ["tests"]
|
|
50
|
+
python_files = "test_*.py"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: swarmtorch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Professional PyTorch library for 120+ metaheuristic optimization algorithms (Swarm, Evolutionary, Physics, Hybrid).
|
|
5
|
+
Author-email: Halleluyah Darasimi Oludele <hallelx2@gmail.com>
|
|
6
|
+
Keywords: pytorch,metaheuristics,swarm-intelligence,evolutionary-algorithms,hpo,gradient-free
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Science/Research
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
Requires-Dist: torch>=2.0.0
|
|
18
|
+
Requires-Dist: numpy>=1.21.0
|
|
19
|
+
Requires-Dist: pandas>=1.3.0
|
|
20
|
+
Requires-Dist: matplotlib>=3.5.0
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pyright>=1.1.0; extra == "dev"
|
|
25
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from torch import nn
|
|
3
|
+
from swarmtorch.base.generic_search import GenericSwarmSearch
|
|
4
|
+
from swarmtorch.swarm.model_training.pso import PSO
|
|
5
|
+
|
|
6
|
+
def test_generic_search():
|
|
7
|
+
def build_model(params):
|
|
8
|
+
return nn.Linear(params['in_features'], 2)
|
|
9
|
+
|
|
10
|
+
param_space = {
|
|
11
|
+
'in_features': [10, 20, 30],
|
|
12
|
+
'lr': (0.001, 0.1)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
def train_fn(model, params):
|
|
16
|
+
return abs(params['lr'] - 0.05) + abs(params['in_features'] - 20)
|
|
17
|
+
|
|
18
|
+
search = GenericSwarmSearch(
|
|
19
|
+
optimizer_class=PSO,
|
|
20
|
+
model_fn=build_model,
|
|
21
|
+
param_space=param_space,
|
|
22
|
+
train_fn=train_fn,
|
|
23
|
+
iterations=5,
|
|
24
|
+
swarm_size=5,
|
|
25
|
+
device="cpu",
|
|
26
|
+
verbose=False
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
best_params = search.search()
|
|
30
|
+
assert 'lr' in best_params
|
|
31
|
+
assert 'in_features' in best_params
|