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.3.0.tar.gz (65.3 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.3.0-cp39-abi3-win_amd64.whl (320.9 kB view details)

Uploaded CPython 3.9+Windows x86-64

turboswarm-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (472.5 kB view details)

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

turboswarm-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (461.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

turboswarm-0.3.0-cp39-abi3-macosx_11_0_arm64.whl (414.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

turboswarm-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl (415.0 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for turboswarm-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bcaaade1c24137460a74f9e92ff184890d67322502c66cac251557f735a92f39
MD5 e9870bf712bf62d489352d7f2d590b8f
BLAKE2b-256 6177a03cc483929acee55679b009a83df6db94446b70cbe3e56ee30e6e3eb8e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: turboswarm-0.3.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 320.9 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.3.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 15f1d9a08a12dca65728bae5189334517fc0e88d7c9b5a0e4025c496245b502d
MD5 6d8e82846005b2952e0ba8a2a81bbd1c
BLAKE2b-256 00c12a2253e38e25cf1200923d8e779f2784206ec2184c733bd1fa9ff7d73747

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 37c8b28048cbe10ed46aaec4d35846ccaa702b7319ad7fc3c6ec32a62049312a
MD5 0e97c2242b153a7c4a33bd40d194d782
BLAKE2b-256 435cf19266bd6eafe5e1540f9c3b5104fb929bbc2aa2d9847509de9673847762

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 eb99a0e7529b38f17f2a8169d9eff165123bcc23c3ca4de1bfcb1ab4690b3f52
MD5 adce17e5773ebe17b78c46ad11c5d4a6
BLAKE2b-256 0676b045a93618feb3618e787a167bb1438324a344813ef45982fe951c490edd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.3.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 870700a8998fe8164519d7b94838bcad707d715452f670372d8bee7e1db644d9
MD5 2ad8bc3564218531edd345e387bd354f
BLAKE2b-256 71c04cb4e94c7e91c4a419323fb8f73a91e81be8fa5733f4cb5aa83e9caf445a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c0108eb099bf1702c11451599eb63ab07ac525276df46aa10ea9ba4413592297
MD5 176d5da82b0b4a4328201cd53fd2bee8
BLAKE2b-256 2911d96ac97c679d68b356bb804d1dcc61f368a134d8356a38dcd2007980efd0

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