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)] * 2, seed=42)

# Your own function in Python
r = pso.minimize(lambda x: sum(xi**2 for xi in x), bounds=[(-5, 5)] * 3)

# Integer variables
r = pso.minimize(f, bounds=[(-10, 10)] * 2, integer=True)

# Variant and topology by name
r = pso.minimize("ackley", bounds=[(-32.768, 32.768)] * 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.1.0.tar.gz (44.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.1.0-cp39-abi3-win_amd64.whl (274.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

turboswarm-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.0 kB view details)

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

turboswarm-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (414.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

turboswarm-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (369.3 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

turboswarm-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (371.2 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: turboswarm-0.1.0.tar.gz
  • Upload date:
  • Size: 44.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.1.0.tar.gz
Algorithm Hash digest
SHA256 deebf322d2dc836c8a7b0669bbc3f431fe1cd8a63438911db45a8ae9be13cba7
MD5 f382b209418a58bbcfe5fad68ffe0281
BLAKE2b-256 773551a0401507de5723363f3c80ac0c0005a4d87e638a997ea251a7a10146e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: turboswarm-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 274.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.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2a3e56da18613ccde95bd99a37fd2ce268306c6371fada50aba563774b506569
MD5 ee2276990364533d23ce8ebe2f72f89c
BLAKE2b-256 a82c30da39f7e1c7cd1976e1490fbd07bec49ab7165c41db4723875935a036c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c2f877ee4bd7c7842145d19c5f3b2b3e18457a38c8248f61e3a4e119560d3063
MD5 2b888a267d595a25d679f8edbac07153
BLAKE2b-256 8fce050e72bca24fe349bf791dec27e1f50c72db49bd3390bbdbed0bd3ee5362

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 21fdc0ab0167c56c9573a8fae6f772ddf287c156808a31dcb4be203ae3046b7d
MD5 fed72038a119fb769d8fb65068d5914c
BLAKE2b-256 66bef9e741016ed2deb1c64a79f02a2eae12e462a32ee92c1a741f92a83efafe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9e0370a9ed178e60d516e8b4f577bac5a76f16177484b7a4031f80abf2a0be00
MD5 24f6f3401e7988f36bc3dbe38e7b3262
BLAKE2b-256 e0d834a9f0a126fe69531da93bd308a70429fc3041dc09a6ebfd8d9c28f8de77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turboswarm-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 82ca4b67080a9f7f35e2b225ef68ec741c62eff6586497b552fba27fc4720ffb
MD5 8805e2d4624afd37195dbf19e6e944c0
BLAKE2b-256 56c5e038e8a3ef00d2335d7d2b8575d1289b1ae5ac2fd1ba054a8088db54c727

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