Particle Swarm Optimization with a Rust core
Project description
turboswarm
Particle Swarm Optimization with a compute core in Rust and an API in Python. Focused on visualization, variant comparison and clear code. Supports real, integer, binary and mixed variables, constraints and multi-objective optimization.
Installation
From source (development), with maturin:
pip install maturin
python -m venv .venv && source .venv/bin/activate
maturin develop --release # compiles the Rust core and installs it
Usage
import turboswarm as pso
# Native benchmark (fast, in Rust, without the GIL)
r = pso.minimize("rastrigin", bounds=(-5.12, 5.12), dim=2, seed=42)
# Your own function in Python
r = pso.minimize(lambda x: sum(xi**2 for xi in x), bounds=(-5, 5), dim=3)
# Integer variables
r = pso.minimize(f, bounds=(-10, 10), dim=2, integer=True)
# Variant and topology by name
r = pso.minimize("ackley", bounds=(-32.768, 32.768), dim=2,
velocity="fips", topology="ring", seed=1)
print(r.best_position, r.best_value)
Parameters of minimize
| Parameter | Default | Description |
|---|---|---|
objective |
— | callable f(list)->float, or name of a native benchmark |
bounds |
— | list of (min, max) per dimension |
integer / binary |
False |
optimize over integers / {0,1} |
var_types |
None |
per-dimension "real"/"integer"/"binary" (mixed) |
n_particles |
30 |
swarm size |
max_iter |
100 |
iterations |
w, c1, c2 |
0.729, 1.494, 1.494 |
inertia, cognitive, social |
velocity |
"inertia" |
"inertia", "constriction", "fips" |
topology |
"global" |
"global", "ring", "vonneumann", "random" |
bounds_handling |
"clamp" |
"clamp", "reflect", "wrap", "reinit" |
seed |
None |
seed (fix it for reproducibility) |
record_history |
True |
store the trace for visualization |
v_max |
None |
clamp each velocity component to [-v_max, v_max] |
patience / tol |
0 / 0.0 |
stop after patience iters without >tol improvement |
max_evals / target / max_time |
None |
stop on evaluation / value / time budget |
constraints / penalty |
None / 1e6 |
inequality constraints g(x)<=0 via penalty |
callback |
None |
callback(iteration, best_value); return False to stop |
vectorized |
False |
objective receives the whole swarm as a NumPy array |
Native benchmarks: sphere, rastrigin, rosenbrock, ackley,
griewank, schwefel. Their metadata (recommended bound and optimum) are in
pso.benchmark_info(name) -> (bound, optimum).
FIPS performs better with local topologies (
"ring","vonneumann"). The"constriction"and"fips"variants derive their factor fromc1 + c2.
Result (PsoResult)
best_position— list of floats (whole-valued for integer/binary dims)best_value— floatconvergence— best value per iteration (convergence curve)history—history[iter][particle][dim](empty ifrecord_history=False)evaluations— number of objective evaluations performedstop_reason—"max_iterations","target","max_evaluations","stagnation","max_time"or"callback"
Multi-objective (MOPSO)
minimize_multi returns a ParetoFront (.positions, .objectives):
front = pso.minimize_multi(
lambda x: [sum(xi**2 for xi in x), sum((xi - 2) ** 2 for xi in x)],
bounds=[(-5, 5)] * 2, seed=42,
)
print(len(front)) # non-dominated solutions
Visualization
import matplotlib.pyplot as plt
pso.viz.plot_convergence(r); plt.show()
pso.viz.compare({"inertia": rA, "fips": rB}); plt.show()
pso.viz.plot_pareto(front); plt.show() # objective space of a Pareto front
anim = pso.viz.animate_swarm(r, pso.benchmarks.rastrigin, [(-5.12, 5.12)] * 2)
# in a notebook: from IPython.display import HTML; HTML(anim.to_jshtml())
animate_swarm only supports 2D problems and requires record_history=True.
Documentation
A navigable documentation portal (narrative guide + API reference) is built with MkDocs Material:
pip install -e ".[docs]"
./scripts/build-docs.sh --serve # http://127.0.0.1:8000
License
MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file turboswarm-0.4.0.tar.gz.
File metadata
- Download URL: turboswarm-0.4.0.tar.gz
- Upload date:
- Size: 68.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6d130d5640e58191fd009b1810d11de08deb788b8c0838bb00c905ec2adae35
|
|
| MD5 |
7b8018a20a19d67b7c8fd56f6ffde2a8
|
|
| BLAKE2b-256 |
0f99b252b6cfcdc68564467af48f1c95d96235ddde0b32463c74e57a35aa857e
|
File details
Details for the file turboswarm-0.4.0-cp39-abi3-win_amd64.whl.
File metadata
- Download URL: turboswarm-0.4.0-cp39-abi3-win_amd64.whl
- Upload date:
- Size: 325.6 kB
- Tags: CPython 3.9+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62379120a3c0703413554495afb3cf2a7e2f8d96f55edb6f274df2fb72f0169c
|
|
| MD5 |
5a4d1c49d4d313155bac75a8833919fd
|
|
| BLAKE2b-256 |
0985594b45187a22dcf42808310c518885383763953e25ee16af09ead78c1c3b
|
File details
Details for the file turboswarm-0.4.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: turboswarm-0.4.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 477.1 kB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ededc992a8693147f016572d788bb4874ea5e562dae6ff8f7588bd0ce9d33b52
|
|
| MD5 |
246cbef834b7196f8c6e167e04025a61
|
|
| BLAKE2b-256 |
787061a267cfbf2c411601274e959e0887fe98eb038a7de24e5ea420ef143eb7
|
File details
Details for the file turboswarm-0.4.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: turboswarm-0.4.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 466.4 kB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3925ebdb48d873dfe3f6d60b3ebccf1a4c7ebcb70e18e9c825b05cd3054860c7
|
|
| MD5 |
b3c7a4aae3b78b43b4027eda9cc799c9
|
|
| BLAKE2b-256 |
42592ac4a479712ecbce6f28cc2b7ef0da9f141f9ca20b5f6d3f242b1f076c2a
|
File details
Details for the file turboswarm-0.4.0-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: turboswarm-0.4.0-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 419.2 kB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4d3c5de9aeed03459e9b51b843bf30840f71c168cef1c1a9688b5958f4a947f
|
|
| MD5 |
57d337315232694b41b032551a6f3504
|
|
| BLAKE2b-256 |
c98a0f9861f2e7a1f9b0d062813ccef47d3c378fadf98f70009036c79ea9e294
|
File details
Details for the file turboswarm-0.4.0-cp39-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: turboswarm-0.4.0-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 418.8 kB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7b53d710286ea5b35dde18369a9c79735fde6b784e56822ca5ac0600f919ec4
|
|
| MD5 |
3114892bb7db4ac7a7a39f67cb91f2a1
|
|
| BLAKE2b-256 |
b9b153cfd8d1c02b773232a244c3fc789d87a4fc75ae7f5057470ccc127d8078
|