Skip to main content

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, mixed and grey/interval variables, constraints and multi-objective optimization, with first-class integrations for the scientific-Python stack.

Installation

pip install turboswarm

Optional integration extras: turboswarm[scipy], [sklearn], [optuna], [pandas], [parallel], [agents], or [all].

From source (development), with maturin:

python -m venv .venv && source .venv/bin/activate
pip install maturin
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 from c1 + c2.

Result (PsoResult)

  • best_position — list of floats (whole-valued for integer/binary dims)
  • best_value — float
  • convergence — best value per iteration (convergence curve)
  • historyhistory[iter][particle][dim] (empty if record_history=False)
  • evaluations — number of objective evaluations performed
  • stop_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())

# 3D landscape + animated 3D swarm:
pso.viz.plot_surface(pso.benchmarks.rastrigin, [(-5.12, 5.12)] * 2,
                     points=r.history[-1]); plt.show()
anim3d = pso.viz.animate_swarm_3d(r, pso.benchmarks.rastrigin, [(-5.12, 5.12)] * 2)

animate_swarm / animate_swarm_3d support 2D problems and require record_history=True.

Integrations

Optional, lazily-imported helpers under turboswarm.integrations (install the matching extra):

# SciPy drop-in (scipy.optimize.minimize signature)
from turboswarm.integrations import scipy as ts_scipy
res = ts_scipy.minimize(fun, bounds=[(-5, 5)] * 3)        # -> OptimizeResult

# scikit-learn hyperparameter search (like GridSearchCV)
from turboswarm.integrations.sklearn import PSOSearchCV

# PSO as an Optuna sampler
from turboswarm.integrations.optuna import TurboswarmSampler

# Optuna/pandas/Joblib-Dask/LangChain-agent tool also available

See the Integrations guide.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

turboswarm-0.6.1.tar.gz (73.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

turboswarm-0.6.1-cp39-abi3-win_amd64.whl (331.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

turboswarm-0.6.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (482.9 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

turboswarm-0.6.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (471.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

turboswarm-0.6.1-cp39-abi3-macosx_11_0_arm64.whl (424.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

turboswarm-0.6.1-cp39-abi3-macosx_10_12_x86_64.whl (425.1 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file turboswarm-0.6.1.tar.gz.

File metadata

  • Download URL: turboswarm-0.6.1.tar.gz
  • Upload date:
  • Size: 73.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for turboswarm-0.6.1.tar.gz
Algorithm Hash digest
SHA256 50f717975ae34900b7f25db4cd50a7834ee6d9589254ac972650202b8e4a3f69
MD5 84514acfe43a43fc44e06c3dad6be09d
BLAKE2b-256 dc651bfccc9cf565e3c470c46798eeafc348c53085c28c459616738898c01eca

See more details on using hashes here.

File details

Details for the file turboswarm-0.6.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: turboswarm-0.6.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 331.2 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

Hashes for turboswarm-0.6.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 397ec85ee8a008e2d337bc3f52da131dfafd102c0b4ac0ab23bcb6255ce5b3bc
MD5 e95506bb33f96fd3d4d506c015d9b591
BLAKE2b-256 c73a927916c469cb1da4be4d30ff76c041a034a7faf142e78044a4fa135ebf0d

See more details on using hashes here.

File details

Details for the file turboswarm-0.6.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for turboswarm-0.6.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4611f82a8eb125428f4d1777ed3189081f75a8e89bfdd9d13233fb32ed7b9803
MD5 c1f532b8fabcc2b685d6ea9f0050a300
BLAKE2b-256 0e61b340c8db9c5c5185e3817f8f57519a502d7aebe150c71ef81ff8dfc854f8

See more details on using hashes here.

File details

Details for the file turboswarm-0.6.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for turboswarm-0.6.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 956e102ef7a1f74336d83b2e8629f253fd37178970ff947953ae1192178b3d6b
MD5 6e78151542016ec1fdc833d5d9ac13c5
BLAKE2b-256 cdeddf644b6c0c6860970b915366be596db684c277a9d7235327798d47eff8ee

See more details on using hashes here.

File details

Details for the file turboswarm-0.6.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for turboswarm-0.6.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e98a43833e71f3a2846a5a63473898b0b5f0505d56254c359dae5281b1ed31fd
MD5 00128b9884a7e08e2c5c575142652da1
BLAKE2b-256 0056b26844b51692a0b1b8a88878355bd805539fc564a65315bd8a5c99cb0cd2

See more details on using hashes here.

File details

Details for the file turboswarm-0.6.1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for turboswarm-0.6.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2ccc690853c3507264fce6b095b5f1be15b43aa86c1d54da636a5b4d51269e17
MD5 07dfbff3722c6f2ef24a52b5743612a7
BLAKE2b-256 b59ecbef9dab7e8e32991f81750470851b65001b3222ff1e4175eb0f13cf57bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page