Skip to main content

Hyper-optimized CMA-ES core in Rust with a Python interface

Project description

fastcma

CI License: MIT PyPI Python Rust

Hyper-optimized CMA-ES in Rust with a first-class Python experience. SIMD, rayon, deterministic seeds, vectorized objectives, restarts, constraints, and a Rich-powered TUI — all while keeping the Rust core available for native use. Published to PyPI as fast-cmaes (module name: fastcma).

Table of contents

Why CMA-ES

  • Derivative-free, handles noisy/non-convex landscapes.
  • Adapts step size (sigma) and covariance to follow curved valleys.
  • Parallel-friendly: candidate evaluations are embarrassingly parallel.

Architecture (Mermaid)

flowchart LR
    subgraph Python API
    A[fmin / fmin_vec / CMAES class]
    B[Constraints & restarts]
    C[Naive baseline (pure Python)]
    end

    subgraph Rust Core (src/lib.rs)
    D[Ask/Tell loop]
    E[Covariance update (full/diag)]
    F[Sigma adaptation]
    G[SIMD dot + rayon fitness]
    H[Deterministic seeds]
    end

    subgraph Tests & Demos
    I[Benchmarks: sphere, rosenbrock, rastrigin, ackley, schwefel, griewank]
    J[Rich TUI demo]
    K[Python smoke]
    end

    A --> D
    A --> B
    B --> D
    D --> E --> F --> D
    D --> G
    D --> H
    I --> D
    J --> A
    K --> A
    C --> A

Features

  • Python-first API: fmin, fmin_vec, constrained, restart modes, and a CMAES class.
  • SIMD + rayon: portable_simd accelerates dot products; rayon parallelizes fitness calls.
  • Full/Diagonal covariance: switch via covariance_mode.
  • Deterministic seeds: new_with_seed + test_utils for reproducible runs and benchmarks.
  • Pure-Python baseline: fastcma.cma_es for speed comparisons.
  • Rich TUI: live, colorful CMA-ES progress view.
  • Cross-platform wheels: CI builds for Linux/macOS/Windows, Python 3.12–3.14.

Installation (Python)

Fastest path (PyPI):

python -m pip install fast-cmaes  # installs module `fastcma`

Build locally (needed only if hacking on Rust):

python -m pip install maturin
maturin develop --release

# Optional: NumPy fast paths
maturin develop --release --features numpy_support

# Optional: LAPACK eigen backend (requires a Fortran toolchain)
maturin develop --release --features eigen_lapack

# Demo extras (Rich TUI)
python -m pip install .[demo]

One-liner setup + Rich TUI demo (auto-installs nightly Rust, uv, venv, builds, runs):

./scripts/setup_and_demo.sh

Quickstart (Python)

from fastcma import fmin
from fastcma_baseline import benchmark_sphere

def sphere(x):
    return sum(v*v for v in x)

xmin, es = fmin(sphere, [0.5, -0.2, 0.8], sigma=0.3, maxfevals=4000, ftarget=1e-12)
print("xmin", xmin)

# Pure-Python baseline
print(benchmark_sphere(dim=20, iters=120))

Vectorized & Constraints

from fastcma import fmin_vec

def sphere_vec(X):
    return [sum(v*v for v in x) for x in X]

xmin, _ = fmin_vec(sphere_vec, [0.4, -0.1, 0.3], sigma=0.25, maxfevals=3000)

Constrained run:

from fastcma import fmin_constrained

def sphere(x): return sum(v*v for v in x)
constraints = {"lower_bounds": [-1,-1,-1], "upper_bounds": [1,1,1]}
xmin, _ = fmin_constrained(sphere, [0.5,0.5,0.5], 0.3, constraints)

Rust usage (library)

use fastcma::{optimize_rust, CovarianceModeKind};

let (xmin, _state) = optimize_rust(
    vec![0.5, -0.2, 0.8],
    0.3,
    None,
    Some(4000),
    Some(1e-12),
    CovarianceModeKind::Full,
    |x| x.iter().map(|v| v*v).sum()
);
println!("xmin = {:?}", xmin);

Demos & visualization

  • examples/python_quickstart.py – minimal sphere + vectorized demo.
  • examples/python_benchmarks.py – Rust vs naive Python on sphere; naive on Rastrigin.
  • examples/rich_tui_demo.py – Rich TUI streaming sigma/fbest/evals while minimizing Rosenbrock.

One-shot setup + demo runner:

./scripts/setup_and_demo.sh

What it does: ensures nightly Rust, creates a uv venv on Python 3.13, installs maturin + demo extras, builds the extension, and launches the Rich TUI.

Run the TUI with uv + Python 3.13:

uv venv --python 3.13
uv pip install .[demo]
uv run python examples/rich_tui_demo.py

Baselines & benchmarks

  • Pure Python baseline: fastcma_baseline.cma_es, fastcma_baseline.benchmark_sphere (see python/fastcma_baseline/naive_cma.py).
  • Integration benchmarks (fixed seeds): sphere, Rosenbrock, Rastrigin, Ackley, Schwefel, Griewank in tests/benchmarks.rs.
  • Rich TUI demo for live insight.

Performance considerations

  • SIMD for dot products; rayon for parallel ask/tell evaluations.
  • Lazy eigensystem updates to reduce eigen decompositions.
  • Diagonal covariance option for higher dimensions / speed.
  • Determinism: seeded RNG to make tests non-flaky and benchmarks comparable.
  • Restart helper (test_utils::run_with_restarts) to escape local minima without huge budgets.

Feature flags

  • numpy_support: NumPy array support in vectorized objectives.
  • eigen_lapack: LAPACK eigen backend.
  • test_utils: expose deterministic helpers externally.
  • demo: pulls in rich for the TUI.

Testing

  • Rust: cargo test
  • Python smoke: pytest tests/python_smoke.py
  • CI: GitHub Actions builds wheels on nightly Rust; runs smoke tests; can publish to PyPI when PYPI_API_TOKEN is set.

Contributing

  • Nightly Rust required (see rust-toolchain.toml).
  • Please include failing cases or perf comparisons in issues/PRs.

License

MIT (c) 2025 Jeffrey Emanuel

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

fast_cmaes-0.1.4-cp314-cp314-win_amd64.whl (199.4 kB view details)

Uploaded CPython 3.14Windows x86-64

fast_cmaes-0.1.4-cp314-cp314-manylinux_2_34_x86_64.whl (303.7 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

fast_cmaes-0.1.4-cp314-cp314-macosx_11_0_arm64.whl (258.1 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

fast_cmaes-0.1.4-cp313-cp313-win_amd64.whl (199.4 kB view details)

Uploaded CPython 3.13Windows x86-64

fast_cmaes-0.1.4-cp313-cp313-manylinux_2_34_x86_64.whl (303.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

fast_cmaes-0.1.4-cp313-cp313-macosx_11_0_arm64.whl (258.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

fast_cmaes-0.1.4-cp312-cp312-win_amd64.whl (199.6 kB view details)

Uploaded CPython 3.12Windows x86-64

fast_cmaes-0.1.4-cp312-cp312-manylinux_2_34_x86_64.whl (303.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

fast_cmaes-0.1.4-cp312-cp312-macosx_11_0_arm64.whl (258.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file fast_cmaes-0.1.4-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: fast_cmaes-0.1.4-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 199.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for fast_cmaes-0.1.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 b92c32bb4ecabee82cd46211261884d5ba1be8ee3036f09a15b39caa017e5ffe
MD5 8ff748c2a73b6eda52d908b7cf6ad7d2
BLAKE2b-256 df138daf3d07aa646b48e73bba51f0b2230ffa01609235cc8ed2a6e347899559

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for fast_cmaes-0.1.4-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c6b4dfe29b15b8d751171348072caf56a037cf041455ece1c59475de4e617675
MD5 6216261a832fcb2263fa6c440be7383d
BLAKE2b-256 5df0b3bc91adfdcc0cb319cc932b00ae834f8733f98b1f4ac26bb92c10742ab5

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_cmaes-0.1.4-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d07b65903c174ca89a1e6251630c378ce046345e37d89ae4a08fecf73cf7198
MD5 1e9f62a4b1fdda3a0786006ca23eff89
BLAKE2b-256 6e3a6c31dbbf07886e9ace06ac9fc1a8e2f5ca0d5a9ac4361f59bb6a0f29be99

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: fast_cmaes-0.1.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 199.4 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for fast_cmaes-0.1.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8e44f00723d364463aa09a2e3cf0709f0a645846e57bb92623c8e06bc1fb9e5b
MD5 e7e3d61bebb8835196cd255141f8b43a
BLAKE2b-256 0a62526a294e420a66c00823cf08dce104431a3de4d11f4a74caa021e2a292dc

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for fast_cmaes-0.1.4-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5ffa22caa57d96a7cbc92311f8d287ca25bd79449d7dd9b80221f38accf8b45f
MD5 940c4d4066994182343f0222f653b61d
BLAKE2b-256 15a1d31a3b04150911bac588ac708d3a3a0d77285374fddbaa029f9c89580348

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_cmaes-0.1.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e4520825d65da74821e847d76e17c8a9d343034d53543afe91252f1c50383dd5
MD5 f0a7c5b5168770dcb7670d965fcab498
BLAKE2b-256 b48b485246cf16393d70b626ddb59f849b3ae005dfed6f0683eb2bec563072fa

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: fast_cmaes-0.1.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 199.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for fast_cmaes-0.1.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8ad2f3a78731a04b797d4a4652d604092787e28e9b1d871abb19204bf939b671
MD5 148a28adeff1a4b8c0db872fcbe7961b
BLAKE2b-256 83f633cb181e6397b211038104dbb55387a9deb3ea038fa13dafd6a68c0704c8

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for fast_cmaes-0.1.4-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1cc3d0f708ecbd9011d22847bc03525b8fdd415ddc3806da8380694251a62dce
MD5 f046d204639e79619c041b2d2ae834a6
BLAKE2b-256 29fbc8b3aeee72cdd7cf691eeda944336a66b5d669575aafc3aa96278bee3980

See more details on using hashes here.

File details

Details for the file fast_cmaes-0.1.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_cmaes-0.1.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4d2969eca9052f39cd57bd3d39ee3082521bd33a8ec810e4b264d973cddcc824
MD5 a1e164ecfcb60ec70162a3952534a08e
BLAKE2b-256 939978259a04733c0267bb574c7adf879cbcd31fb548453dfb241e69dd0b9b46

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