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.7.0.tar.gz (75.2 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.7.0-cp39-abi3-win_amd64.whl (333.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

turboswarm-0.7.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (484.6 kB view details)

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

turboswarm-0.7.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (473.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

turboswarm-0.7.0-cp39-abi3-macosx_11_0_arm64.whl (426.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

turboswarm-0.7.0-cp39-abi3-macosx_10_12_x86_64.whl (426.7 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for turboswarm-0.7.0.tar.gz
Algorithm Hash digest
SHA256 68a4d26485cb8af4401dbe01ae7ec9937f19818ae534b25eee4d91b18d59a6b3
MD5 c4a1250864b80d26a9be4609d05c0fba
BLAKE2b-256 d9490f6a559ec0ac598718be1df1d024de50935e0e26c4de97a6d5fcb60bf00d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: turboswarm-0.7.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 333.4 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.7.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 44b78a96b4a818ab5ef6e23633212d2775853694378e3eaded975919f969c611
MD5 e7bea00cff8351ccde01aeb3c6f801d1
BLAKE2b-256 7d0b01a98d970abbc5b3360385a347701283a4d867380c0959606bf2d9313aeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.7.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98fe2cd9a094968dd452533d3aab8bfd5ec58f7235d6c37b903b1e81e7bf04c5
MD5 723fb7431a73f59b976612ea15769be5
BLAKE2b-256 f7e801e971fb176b064c86b495f7f61be4da1960f1b0cc0d058e61f209770d3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.7.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2c4364bf687ffadecf9f59ad08adc200e2dac82c4c24d1a2cf7c236028124c3a
MD5 a44fb351ef171d541f4ebab157c4d121
BLAKE2b-256 b893ea95d3ee2064caf9db4ae93c7412acbe686d864fef5a74646bf9bf6bd250

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.7.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e68b80f21075826f5000be654cfa95583cdb5f45afa5ca59deda2ac316b45931
MD5 afe60f1b16d3062292e485bd1a7eb894
BLAKE2b-256 c8616ebf357e48cf699ee3b1c7b40187be6e644e51f68bd7c2f74b887a5861cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.7.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a8f267c6861954f2d776e62d1656395ed3bde45cc4b632fea87c4cf3f5b9b240
MD5 65a8f61282ba6f9a9393db9e4dca1801
BLAKE2b-256 1008b6c2a1bc0c89d8600077df8ad040be633e34058970c67d94edcd6ec4272e

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