Skip to main content

pyBlindOpt logo pyBlindOpt

PyPI - Version PyPI - Python Version GitHub License GitHub Actions Workflow Status GitHub last commit

pyBlindOpt is a library that implements several derivative-free optimization algorithms (including genetic and evolutionary methods).

Currently, it implements thirteen different algorithms:

  1. Random Search (RS): A baseline optimization method that iteratively generates candidate solutions from the search space according to a specified probability distribution (usually uniform) and records the best solution found. It serves as a benchmark for comparing the performance of more complex algorithms.
  2. Hill Climbing (HC): A mathematical optimization technique belonging to the family of local search algorithms. It is an iterative method that starts with an arbitrary solution and attempts to find a better one by making incremental changes to the current solution.
  3. Simulated Annealing (SA): A probabilistic technique for approximating the global optimum of a given function. It is a metaheuristic designed to escape local optima by allowing "uphill" moves (worse solutions) with a probability that decreases over time (simulating the cooling process of metallurgy).
  4. Genetic Algorithm (GA): A metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). GA generates high-quality solutions by relying on biologically inspired operators such as mutation, crossover, and selection.
  5. Differential Evolution (DE): A population-based method that optimizes a problem by iteratively improving a candidate solution with regard to a given measure of quality. It makes few to no assumptions about the problem being optimized and is effective for searching very large spaces of candidate solutions.
  6. Particle Swarm Optimization (PSO): A computational method that optimizes a problem by iteratively improving a candidate solution (particle) with regard to a given measure of quality. Particles move around the search space according to simple mathematical formulas involving their position and velocity. Each particle's movement is guided by its local best-known position and the global best-known position in the search space.
  7. Grey Wolf Optimization (GWO): A population-based metaheuristic algorithm that simulates the leadership hierarchy (Alpha, Beta, Delta, and Omega) and hunting mechanism of grey wolves in nature.
  8. Enhanced Grey Wolf Optimization (EGWO): An advanced variant of the standard GWO that incorporates mechanisms to better balance exploration and exploitation. This modification helps prevent the algorithm from stagnating in local optima, improving convergence speed and solution quality in complex landscapes.
  9. Artificial Bee Colony (ABC): Simulates the foraging behavior of honey bees. The colony consists of employed bees (who exploit food sources), onlooker bees (who select sources based on quality), and scout bees (who find new random sources).
  10. Firefly Algorithm (FA): Inspired by the flashing behavior of fireflies. Fireflies are attracted to each other based on brightness (fitness), but the attractiveness decreases with distance, simulating light absorption.
  11. Harris Hawks Optimization (HHO): Mimics the cooperative hunting behavior of Harris' hawks, featuring distinct exploration and exploitation phases (like soft and hard besieges) controlled by the prey's escaping energy.
  12. Cuckoo Search (CS): Based on the brood parasitism of cuckoos. It uses Lévy flights for global exploration to generate new eggs and simulates nest abandonment to avoid local optima.
  13. Honey Badger Algorithm (HBA): Mimics the intelligent foraging behavior of honey badgers, switching between a "digging" phase (using smell intensity) and a "honey" phase (following a guide bird).

All algorithms take advantage of the joblib library to parallelize objective function evaluations and cache results for improved performance.

Note: The code has been optimized to a certain degree but was primarily created for educational purposes. Please consider libraries like pymoo or SciPy if you require a production-grade implementation. Regardless, reported issues will be fixed whenever possible..

Installation

The library can be installed directly from GitHub by adding the following line to your requirements.txt file:

git+[https://github.com/mariolpantunes/pyBlindOpt@main#egg=pyBlindOpt](https://github.com/mariolpantunes/pyBlindOpt@main#egg=pyBlindOpt)

Alternatively, you can install a specific version from PyPI:

pyBlindOpt>=0.2.0

Examples

Simple Example

This example demonstrates how to run a basic optimization using Simulated Annealing on the Sphere function.

import numpy as np
import pyBlindOpt

# 1. Define the search space (2 Dimensions, range -5.0 to 5.0)
bounds = np.array([[-5.0, 5.0]] * 2)

# 2. Run the optimization
# Usage: pyBlindOpt.simulated_annealing(objective, bounds, ...)
best_pos, best_score = pyBlindOpt.simulated_annealing(
    objective=pyBlindOpt.functions.sphere,
    bounds=bounds,
    n_iter=100,
    verbose=True
)

print(f"Best Position: {best_pos}")
print(f"Best Score: {best_score}")

Advanced Example

This example demonstrates a complex workflow:

  1. Initializing a reproducible random number generator (RNG).
  2. Creating a Hyper-Latin Cube Sampler (HLC) bound to that RNG.
  3. Generating an initial population using Opposition-Based Learning (OBL) combined with the HLC sampler.
  4. Optimizing using Grey Wolf Optimization (GWO), ensuring the custom population and RNG are passed through.
import numpy as np
import pyBlindOpt

# 1. Setup reproducible RNG
seed = 42
rng = np.random.default_rng(seed)

# 2. Define Problem
bounds = np.array([[-100.0, 100.0]] * 10) # 10 Dimensions
objective = pyBlindOpt.functions.rastrigin
n_pop = 20

# 3. Create Sampler (Hyper-Latin Cube)
sampler = pyBlindOpt.utils.HLCSampler(rng)

# 4. Generate Initial Population using Opposition-Based Learning
# Passing 'sampler' as the population argument tells OBL how to sample the base set
initial_pop = pyBlindOpt.init.opposition_based(
    objective=objective,
    bounds=bounds,
    population=sampler,  # Use HLC for the random part of OBL
    n_pop=n_pop,
    seed=rng
)

# 5. Run GWO with the custom population and shared RNG
best_pos, best_score = pyBlindOpt.grey_wolf_optimization(
    objective=objective,
    bounds=bounds,
    population=initial_pop, # Pass the OBL-optimized population
    n_iter=200,
    n_pop=n_pop,
    verbose=True,
    seed=rng  # Pass the same RNG to ensure reproducibility of GWO internals
)

print(f"Best Position: {best_pos}")
print(f"Best Score: {best_score}")

Documentation

This library is documented using Google-style docstrings. The full documentation can be accessed here.

To generate the documentation locally, run the following command:

pdoc --math -d google -o docs src/pyBlindOpt

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for pyBlindOpt 0.2.8

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

Source distribution (sdist)

Source distribution for pyBlindOpt 0.2.8
File Size Uploaded Provenance Actions
pyblindopt-0.2.8.tar.gz 55.9 kB Without provenance Details for pyblindopt-0.2.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyBlindOpt 0.2.8
File Interpreter ABI Platform Actions
pyblindopt-0.2.8-py3-none-any.whl Python 3 none any Details for pyblindopt-0.2.8-py3-none-any.whl

Total release size: 112.6 kB

Release files / pyblindopt-0.2.8.tar.gz

Download URL pyblindopt-0.2.8.tar.gz
Size 55.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6f551440a4db81022df45b0e3ccc3ed8b7be78437bb1878910b2d0281d6b6018
BLAKE2b-256 checksum
How to use checksums
fe7a238b8a31b753938f36b1b3e951f37ee4588c31c8622e7ce4714b35556106
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pyblindopt-0.2.8-py3-none-any.whl

Download URL pyblindopt-0.2.8-py3-none-any.whl
Size 56.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c09348f892dc99218c9334e60f0eeaeea4c8ac7dc707ce04a4642a15a0f3cc2
BLAKE2b-256 checksum
How to use checksums
d3f349cc40cd79e5c96b3f8e903312b5cd392cf39568eb2f0dff97133340c067
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for pyBlindOpt 0.2.8

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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