Skip to main content

DOI

Useful Optimizer

Useful Optimizer is a dedicated set of optimization algorithms for numeric problems. It's designed to provide a comprehensive collection of optimization techniques that can be easily used and integrated into any project.

Version

The current version of Useful Optimizer is 0.2.0.

Features

  • A wide range of optimization algorithms in ten categories, each documented and benchmarked.
  • Organized into logical categories for easy discovery.
  • Easy to use and integrate.
  • Suitable for various numeric problems.
  • Having fun to play with the algorithms

Installation

To install Useful Optimizer, you can use pip:

pip install git+https://github.com/Anselmoo/useful-optimizer

Or using uv (recommended):

uv add git+https://github.com/Anselmoo/useful-optimizer

Usage

Here's a basic example of how to use Useful Optimizer:

from opt.metaheuristic import CrossEntropyMethod
from opt.benchmark.functions import shifted_ackley

optimizer = CrossEntropyMethod(
        func=shifted_ackley,
        dim=2,
        lower_bound=-12.768,
        upper_bound=+12.768,
        population_size=100,
        max_iter=1000,
    )
best_solution, best_fitness = optimizer.search()
print(f"Best solution: {best_solution}")
print(f"Best fitness: {best_fitness}")

You can also use the gradient-based optimizers:

from opt.gradient_based import SGD, AdamW
from opt.classical import BFGS
from opt.benchmark.functions import shifted_ackley

# Gradient-based optimization
sgd = SGD(func=shifted_ackley, lower_bound=-12.768, upper_bound=12.768, dim=2, learning_rate=0.01)
best_solution, best_fitness = sgd.search()

# Adam variant with weight decay
adamw = AdamW(func=shifted_ackley, lower_bound=-12.768, upper_bound=12.768, dim=2, weight_decay=0.01)
best_solution, best_fitness = adamw.search()

# Quasi-Newton method
bfgs = BFGS(func=shifted_ackley, lower_bound=-12.768, upper_bound=12.768, dim=2, num_restarts=10)
best_solution, best_fitness = bfgs.search()

Import Styles

All optimizers can be imported in two ways:

# Categorical imports (recommended for discoverability)
from opt.swarm_intelligence import ParticleSwarm
from opt.gradient_based import AdamW
from opt.classical import BFGS

# Direct imports from root (backward compatible)
from opt import ParticleSwarm, AdamW, BFGS

Quick Demo

All optimizers include a standardized demo that can be run directly or customized:

from opt.demo import run_demo
from opt.swarm_intelligence import ParticleSwarm

# Run with default settings
run_demo(ParticleSwarm)

# Or customize parameters
run_demo(
    ParticleSwarm,
    max_iter=200,
    population_size=50,
    c1=2.0,
    c2=2.0
)

You can also run demos directly from the command line:

python -m opt.swarm_intelligence.particle_swarm
python -m opt.gradient_based.adamw
python -m opt.classical.simulated_annealing

Project Structure

Optimizers are organized into categorical subfolders:

src/opt/
├── abstract/            # Base classes and history tracking
├── swarm_intelligence/  # 56 algorithms
├── evolutionary/        # 6 algorithms
├── gradient_based/      # 11 algorithms
├── classical/           # 10 algorithms
├── metaheuristic/       # 14 algorithms
├── physics_inspired/    # 4 algorithms
├── probabilistic/       # 5 algorithms
├── social_inspired/     # 4 algorithms
├── constrained/         # 5 algorithms
├── multi_objective/     # 3 algorithms
└── benchmark/           # Benchmark functions

Implemented Optimizers

Useful Optimizer includes 118 optimization algorithms in 10 categories, each implemented as a separate module. Every optimizer links to its documentation page, which carries the formulation, parameters, references and benchmark charts, and to its source code.

🦋 Swarm Intelligence (56)

Nature-inspired algorithms based on collective behavior.

🧬 Evolutionary (6)

Algorithms inspired by biological evolution.

🧠 Gradient-Based (11)

Optimization using gradient information.

🎯 Classical (10)

Traditional mathematical optimization methods.

🔧 Metaheuristic (14)

High-level problem-independent algorithms.

⚛️ Physics-Inspired (4)

Algorithms based on physical phenomena.

📊 Probabilistic (5)

Stochastic and Bayesian optimization methods.

👥 Social-Inspired (4)

Algorithms inspired by social behaviors.

🔒 Constrained (5)

Optimization with constraints.

📈 Multi-Objective (3)

Pareto-optimal solutions for multiple objectives.

Testing and Documentation

Useful Optimizer includes comprehensive doctests to ensure all examples in the documentation are correct and up-to-date. All optimizer classes and benchmark functions include working examples that can be verified automatically.

Documentation Generation

The project uses an automated documentation generator with Griffe integration to create comprehensive API documentation:

# Generate all documentation (markdown + JSON + API)
uv run python scripts/generate_docs.py --all --json --full-api --sidebar

# Or use the npm shortcut
cd docs && npm run docs:api:all

This generates:

  • Markdown pages for every optimizer in docs/algorithms/, plus category indexes and API pages
  • JSON metadata at docs/public/optimizers/optimizers.json
  • VitePress sidebar auto-configuration
  • Griffe API dump docs/api/full_api.json for the entire opt package (generated in CI, not committed)

Performance: ~6 seconds for complete generation

See docs/DOCUMENTATION_GENERATOR.md for detailed usage.

Running Tests

To run all tests including doctests:

# Using pytest with doctests
uv run pytest --doctest-modules --doctest-glob="*.md" -v

# Run only doctests for a specific module
uv run pytest opt/benchmark/functions.py --doctest-modules -v

# Run only doctests for optimizers
uv run pytest opt/swarm_intelligence/particle_swarm.py --doctest-modules -v

Example Doctest Usage

All benchmark functions include working examples in their docstrings:

import numpy as np
from opt.benchmark.functions import sphere

# At optimum
result = sphere(np.array([0.0, 0.0]))
print(float(result))  # 0.0

# Away from optimum
result = sphere(np.array([1.0, 1.0]))
print(float(result))  # 2.0

All optimizer classes include usage examples in their docstrings:

from opt.swarm_intelligence.particle_swarm import ParticleSwarm
from opt.benchmark.functions import sphere

optimizer = ParticleSwarm(
    func=sphere, dim=2, lower_bound=-5, upper_bound=5,
    max_iter=10, seed=42
)
solution, fitness = optimizer.search()
print(f"Fitness: {fitness}")  # Should be < 1.0

These examples serve as both documentation and automated tests, ensuring the code examples in docstrings always work correctly.

Contributing

Contributions to Useful Optimizer are welcome! Please read the contributing guidelines before getting started.

License


Metadata

Release files for useful-optimizer 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 useful-optimizer 0.2.0
File Size Uploaded Provenance Actions
useful_optimizer-0.2.0.tar.gz 343.7 kB With provenance Details for useful_optimizer-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 1.0 MB

Release files / useful_optimizer-0.2.0.tar.gz

Download URL useful_optimizer-0.2.0.tar.gz
Size 343.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b91160561337aa4cd05c6ee75b935506f3d1f383e20d55d5127ef94e4c83d325
BLAKE2b-256 checksum
How to use checksums
5cc9c5d5d98034191a278a79dc318824e598b5c93b9bdd8492034c2507d34462
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 4, 2026.

Transparency log

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

Download URL useful_optimizer-0.2.0-py3-none-any.whl
Size 678.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d37256141b9f64e1ac2d026dfee967178a472faca28205ce07e6af30e5d2dd4e
BLAKE2b-256 checksum
How to use checksums
b597c4266f2d4ce39e93f08281edfe4005f6b5b2b55153f8f43ee969be467c19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 4, 2026.

Transparency log

Security for useful-optimizer 0.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for useful_optimizer-0.2.0.tar.gz

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 Nov 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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