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 random import Random

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
seeded = Random(0)

knapsack = Problem(
    generate=lambda: [seeded.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

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.2.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.2.0
File Size Uploaded
pymetaheuristics-0.2.0.tar.gz 293.3 kB Details

Built distribution (wheel)

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

Total release size: 320.6 kB

Release files / pymetaheuristics-0.2.0.tar.gz

Download URL pymetaheuristics-0.2.0.tar.gz
Size 293.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d6a80a654fe3452cd05955696bd565a56ff3b8258be70e6b60dc2199cc022397
BLAKE2b-256 checksum
How to use checksums
aa2aac27123840da79c47197ce008dee314e61d94604e518045c8f08e9127bc8
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 4, 2026.

Transparency log

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

Download URL pymetaheuristics-0.2.0-py3-none-any.whl
Size 27.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5db1c69e3a2094c3815594b841868c2cb518d7f5bebbdf33e2f34b32cac6bb82
BLAKE2b-256 checksum
How to use checksums
ec4fb6d80a528f744bab5982c1743c97f2e85e49312ab5478a1e9de2217e23dc
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 4, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.0

2 release files

This release

0.2.0 This release

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