Skip to main content

pymetaheuristics

Continuous Integration Coverage Status PyPI version

Metaheuristics for optimization problems in plain Python, with no dependencies. Describe the problem as a Problem: how to generate a solution, how to evaluate it, which solutions are feasible, and whether to minimize or maximize. Then pass it to a heuristic: a Genetic Algorithm or Simulated Annealing. Every heuristic returns the same OptimizationResult.

Documentation: https://igormcsouza.github.io/pymetaheuristics/ (changelog).

Install

Requires Python 3.12+.

pip install pymetaheuristics
# or
uv add pymetaheuristics

Quickstart

from pymetaheuristics.core import Direction, Problem, max_iterations
from pymetaheuristics.genetic_algorithm import genetic_algorithm
from pymetaheuristics.simulated_annealing import (
    bit_flip_neighbor, simulated_annealing)

VALUES, WEIGHTS, CAPACITY = [60, 100, 120], [10, 20, 30], 50

knapsack = Problem(
    generate=lambda rng: [rng.randint(0, 1) for _ in VALUES],
    evaluate=lambda s: sum(v for v, bit in zip(VALUES, s) if bit),
    feasible=lambda s: sum(w for w, bit in zip(WEIGHTS, s) if bit)
    <= CAPACITY,
    direction=Direction.MAXIMIZE,
)

ga = genetic_algorithm(knapsack, stop=max_iterations(20), rng=42)
sa = simulated_annealing(knapsack, stop=max_iterations(200), rng=42,
                         neighbor=bit_flip_neighbor)
print(ga.best_solution, ga.best_value)  # [0, 1, 1] 220
print(sa.best_solution, sa.best_value)  # [0, 1, 1] 220

How well does it work?

Every heuristic against random search on the benchmark suite, with the same budget of 2000 evaluations over 20 seeds (details):

Gap closed versus random search Time per run

On TSP all three heuristics find the optimum on every seed and on the sphere the GA and SA close over 99.7% of random search's gap, while random search stays far off. Rastrigin is hard for all of them with this budget. A full run takes about 10-30 ms.

Documentation

The documentation lives in docs/. Start with docs/index.md, or build the site locally with uv run --group docs mkdocs serve. It covers:

Development

uv sync                    # dev tools
uv run pre-commit install
uv run ruff check .
sh scripts/test.sh         # pytest with coverage, including the doc snippets
uv run --group docs mkdocs build --strict

Contributions are welcome. Open an issue or a pull request.

Metadata

Release files for pymetaheuristics 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymetaheuristics 0.3.0
File Size Uploaded
pymetaheuristics-0.3.0.tar.gz 395.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymetaheuristics 0.3.0
File Interpreter ABI Platform
pymetaheuristics-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 421.1 kB

Release files / pymetaheuristics-0.3.0.tar.gz

Download URL pymetaheuristics-0.3.0.tar.gz
Size 395.9 kB
Tags Source
SHA-256 checksum
How to use checksums
516328320f7ba2a773d59a53f409e83b4da3df0ee34291e5006148eb5c22438d
BLAKE2b-256 checksum
How to use checksums
1e3c3d47a1fbc4d382263c8f1773758af044b915341bd7a5ed9a3da522dcc5ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Release files / pymetaheuristics-0.3.0-py3-none-any.whl

Download URL pymetaheuristics-0.3.0-py3-none-any.whl
Size 25.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80012e426f87097e6453f8adc391dd97b95ba92bb4656e923d91a1acfd29211c
BLAKE2b-256 checksum
How to use checksums
c4e7e497c6a0acb2c6d66ddfa866e53e20d341be5bd6196d2c478299aebc9ab3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page