Skip to main content

MetaGen: A Framework for Metaheuristic Development and Hyperparameter Optimization

Python PyPI Latest Release CI Documentation License: GPL v3

🚀 Why MetaGen?

MetaGen simplifies the development of metaheuristics and the optimization of hyperparameters in machine learning and deep learning. Whether you're a researcher, developer, or practitioner, MetaGen provides a structured, flexible, and scalable framework.

🔹 Key Features

Metaheuristic Development Framework – A base class with the run loop, elitism and callbacks; you write three methods.
Hyperparameter Optimization Tools – Search spaces with integers, reals, categoricals, groups and variable-length structures, for layer and architecture-level tuning.
Standardized Interface – Ensures compatibility between metaheuristic developers and end users.
Dynamic Architecture Optimization – Structures whose length the search itself changes.
Seamless Integration – Compatible with scikit-learn, tensorflow, pytorch, and other ML libraries.
Built-in Metaheuristics – Pre-implemented algorithms ready to use.
Reproducible Runs – A seed parameter controls every random draw, also across Ray workers.
TensorBoard Integration – Give a run a log_dir and follow how the algorithm evolves, graphically, in TensorBoard.
Scalable and Distributed Execution – With distributed=True, metaheuristics run across the CPUs of a Ray cluster.

📌 Built-in Metaheuristics

  • Random Search
  • Hill Climbing
  • Tabu Search
  • Simulated Annealing
  • Genetic Algorithm and Steady-State Genetic Algorithm
  • Memetic Algorithm
  • Tree-structured Parzen Estimator: TPE, which evaluates a pool of candidates per iteration, and KernelTPE, which evaluates a single candidate per iteration, for expensive fitness functions
  • Coronavirus Optimization Algorithm (CVOA), with two strain classes, CVOA and ProbabilisticCVOA, that differ in how deaths, superspreaders and isolation are decided

The documentation has a guide to choosing one, with what each costs in evaluations.

📦 Installation

MetaGen requires Python 3.10+ and can be installed with:

pip install pymetagen-datalabupo

Two features are optional and installed on demand:

pip install pymetagen-datalabupo[distributed]   # Ray, for distributed=True
pip install pymetagen-datalabupo[tensorboard]   # TensorBoard logging
pip install pymetagen-datalabupo[all]           # both

📖 Documentation

The official API reference and usage guides are available at: MetaGen Documentation

🤖 Example: Hyperparameter Optimization

Optimizing hyperparameters for a regression model:

from metagen.framework import Domain, Solution
from metagen.metaheuristics import RandomSearch
from sklearn.datasets import make_regression
from sklearn.linear_model import SGDRegressor
from sklearn.model_selection import cross_val_score

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=4, random_state=0)

# Define the search space
regression_domain = Domain()
regression_domain.define_real("alpha", 0.0001, 0.001)
regression_domain.define_integer("iterations", 5, 200)
regression_domain.define_categorical("loss", ["squared_error", "huber", "epsilon_insensitive"])

# Fitness function: MetaGen always minimizes
def regression_fitness(solution: Solution) -> float:
    model = SGDRegressor(
        loss=solution["loss"],
        alpha=solution["alpha"],
        max_iter=solution["iterations"]
    )
    mape = cross_val_score(model, X, y, scoring="neg_mean_absolute_percentage_error").mean() * -1
    return mape

# Run optimization; the seed makes the run reproducible
best_solution = RandomSearch(regression_domain, regression_fitness, seed=0).run()
print(best_solution)

Any other metaheuristic takes the same two arguments. To follow the run in TensorBoard, add log_dir="logs/random_search" and launch tensorboard --logdir=logs; to see progress on the console, call set_metagen_logger_level() from metagen.logging.metagen_logger.

🛠 Example: Developing a Metaheuristic

Creating a simple Random Search metaheuristic:

from copy import deepcopy
from typing import Callable, List
from metagen.framework import Domain, Solution

class RandomSearch:

    def __init__(self, domain: Domain, fitness: Callable[[Solution], float], search_space_size: int = 30,
                iterations: int = 20) -> None:

        self.domain = domain
        self.fitness = fitness
        self.search_space_size = search_space_size
        self.iterations = iterations

    def run(self) -> Solution:

        potential_solutions: List[Solution] = list()

        for _ in range(0, self.search_space_size):
            potential_solutions.append(Solution(self.domain, connector=self.domain.get_connector()))

        solution: Solution = deepcopy(min(potential_solutions))

        for _ in range(0, self.iterations):
            for ps in potential_solutions:
                ps.mutate()

                ps.evaluate(self.fitness)
                if ps < solution:
                    solution = deepcopy(ps)

        return solution

A metaheuristic can also inherit from the Metaheuristic base class, which adds the run loop, elitism, seed, log_dir and distributed execution in exchange for three methods; see Extending the Metaheuristic class.

📝 Citing MetaGen

If you use MetaGen in your research, please cite:

D. Gutiérrez-Avilés, M. J. Jiménez-Navarro, J. F. Torres, F. Martínez-Álvarez. MetaGen: A framework for metaheuristic development and hyperparameter optimization in machine and deep learning. Neurocomputing 637 (2025) 130046. https://doi.org/10.1016/j.neucom.2025.130046

@article{metagen2025,
  title   = {MetaGen: A framework for metaheuristic development and hyperparameter optimization in machine and deep learning},
  author  = {Guti{\'e}rrez-Avil{\'e}s, David and Jim{\'e}nez-Navarro, Manuel Jes{\'u}s and Torres, Jos{\'e} Francisco and Mart{\'i}nez-{\'A}lvarez, Francisco},
  journal = {Neurocomputing},
  volume  = {637},
  pages   = {130046},
  year    = {2025},
  doi     = {10.1016/j.neucom.2025.130046}
}

The article describes version 0.2.0, which remains available on PyPI and as a tagged release.

🤝 Contributing

We welcome contributions from developers of all experience levels! To contribute:

  • Open an issue or submit a pull request.

  • Install the package for development and run the tests:

    pip install -e .[test]
    pytest test
    mypy src
    

    Both must pass; the continuous integration runs them on Python 3.10 to 3.12.

📌 Resources

⚖️ License

MetaGen is free software, distributed under the GNU General Public License v3 or later.


MetaGen is an open-source project developed and maintained by:

  • David Gutiérrez-Avilés
  • Manuel Jesús Jiménez-Navarro
  • Francisco José Torres-Maldonado
  • Francisco Martínez-Álvarez

The authors are members of the Minerva AI Lab Group at the University of Seville and of DataLabUPO, the Data Science & Big Data Research Lab at Pablo de Olavide University.

Release files for pymetagen-datalabupo 1.0.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 pymetagen-datalabupo 1.0.0
File Size Uploaded
pymetagen_datalabupo-1.0.0.tar.gz 109.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymetagen-datalabupo 1.0.0
File Interpreter ABI Platform
pymetagen_datalabupo-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 260.5 kB

Release files / pymetagen_datalabupo-1.0.0.tar.gz

Download URL pymetagen_datalabupo-1.0.0.tar.gz
Size 109.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9dcfae752764d67d978454d7181bf9160c8acb43516915e75f8cc3e2f1cfe5d7
BLAKE2b-256 checksum
How to use checksums
f67a824caa7716edcca52237df6c25e23bccca0365ff2cbc9fc9849b335f813d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release files / pymetagen_datalabupo-1.0.0-py3-none-any.whl

Download URL pymetagen_datalabupo-1.0.0-py3-none-any.whl
Size 151.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58d7022d5aab6a4b875daf1b7b14732b26f9d8805f73c73718b683a3a496cb26
BLAKE2b-256 checksum
How to use checksums
39ed4b0465102a603abb3d38c25857080fc54a02839462afd8732b60ae9a9304
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

1.1.1

2 release files

1.1.0

2 release files

This release

1.0.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

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