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 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 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())

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


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.5.0.tar.gz (71.8 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.5.0-cp39-abi3-win_amd64.whl (330.1 kB view details)

Uploaded CPython 3.9+Windows x86-64

turboswarm-0.5.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (481.3 kB view details)

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

turboswarm-0.5.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (470.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

turboswarm-0.5.0-cp39-abi3-macosx_11_0_arm64.whl (423.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

turboswarm-0.5.0-cp39-abi3-macosx_10_12_x86_64.whl (423.6 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for turboswarm-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e147297d2d2eac60f036acefbafab5fbfb16759bf993ff491cb9c6cbbde976d1
MD5 c833adbf3a416546cf25b43b4453a8d6
BLAKE2b-256 94f0b264225b133314f0bb24c821b8ab359bb0f86a1f0d0acc912dbf721c8e07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: turboswarm-0.5.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 330.1 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.5.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 758f27a1166b133e1d8b77c9694b17bffb556ae4b522d0dc61691ecff0924ff1
MD5 781cce82e740350fd4bb1833b5e13cc7
BLAKE2b-256 84d91b63c9f2989128bce817dc700159064a6545797653f99ac5a90d78104d31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.5.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 40f00ac3bee258cd36924aeff1a76cda569bc55a0e62070e5232e274397e8d4a
MD5 32dd60dc9ef76bf3c85cab4710f2bf45
BLAKE2b-256 85bf2ec1dfd5140107a17f5cc2c7224d42afe6ee48394d02b9cea5f3a9ea0981

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.5.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e7cea0ff880ebc2876bac91757d108c2e9283800d4c9c457adf699ff1c3d8de9
MD5 e29b53e6e74421f20e88dd84d8659157
BLAKE2b-256 912f48e04db732e2a6207e325d01a09ce109fdc1cc52540b128f303d6b858712

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.5.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 25ce2a0b1d0951d85656a65e193ab188b8d925e2c1b8095b39d18f6dfd2bd4eb
MD5 bfdb41273dffee410d624a9e7e65afdb
BLAKE2b-256 a16fdda1af16bc827c3a6fc99314a3fca77e90bd3fae42a8dadcc7872d70cad1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.5.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b9c0730c1c7e4b0d5cff08ac729cafd31b699c9173b21797ae0f53c635980660
MD5 71b5aa534957f4a18bca812f0a4b30ef
BLAKE2b-256 a1d319aac0051ea624e151e7e82a8a2e40bdd117981904e1422e42e8a71822be

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