LLaMEA is a Python framework for automatically generating and refining metaheuristic optimization algorithms using large language models, featuring optional in-the-loop hyper-parameter optimization.
Project description
LLaMEA: Large Language Model Evolutionary Algorithm
Table of Contents
Introduction
LLaMEA (Large Language Model Evolutionary Algorithm) is an innovative framework that leverages the power of large language models (LLMs) such as GPT-4 for the automated generation and refinement of metaheuristic optimization algorithms. The framework utilizes a novel approach to evolve and optimize algorithms iteratively based on performance metrics and runtime evaluations without requiring extensive prior algorithmic knowledge. This makes LLaMEA an ideal tool for both research and practical applications in fields where optimization is crucial.
Key Features:
- Automated Algorithm Generation: Automatically generates and refines algorithms using GPT-based or similar LLM models.
- Performance Evaluation: Integrates seamlessly with the IOHexperimenter for real-time performance feedback, guiding the evolutionary process.
- LLaMEA-HPO: Provides an in-the-loop hyper-parameter optimization mechanism (via SMAC) to offload numerical tuning, so that LLM queries focus on novel structural improvements.
- Extensible & Modular: You can easily integrate additional models and evaluation tools.
🔥 News
-
2025.03 🎉🎉 LLaMEA v1.0.0 released!
-
2025.01 🎉🎉 LLaMEA paper accepted in IEEE TEVC “Llamea: A large language model evolutionary algorithm for automatically generating metaheuristics"!
🎁 Installation
It is the easiest to use LLaMEA from the pypi package.
pip install llamea
[!Important] The Python version must be larger or equal to Python 3.9. You need an OpenAI/Gemini/Ollama API key for using LLM models.
You can also install the package from source using Poetry.
- Clone the repository:
git clone https://github.com/xai-liacs/LLaMEA.git cd LLaMEA
- Install the required dependencies via Poetry:
poetry install
💻 Quick Start
-
Set up an OpenAI API key:
- Obtain an API key from OpenAI.
- Set the API key in your environment variables:
export OPENAI_API_KEY='your_api_key_here'
-
Running an Experiment
To run an optimization experiment using LLaMEA:
from llamea import LLaMEA # Define your evaluation function def your_evaluation_function(solution): # Implementation of your function # return feedback, quality score, error information return "feedback for LLM", 0.1, "" # Initialize LLaMEA with your API key and other parameters optimizer = LLaMEA(f=your_evaluation_function, api_key="your_api_key_here") # Run the optimizer best_solution, best_fitness = optimizer.run() print(f"Best Solution: {best_solution}, Fitness: {best_fitness}")
💻 Examples
Below are two example scripts demonstrating LLaMEA in action for black-box optimization with a BBOB (24 noiseless) function suite. One script (example.py) runs basic LLaMEA, while the other (example_HPO.py) incorporates a hyper-parameter optimization pipeline—known as LLaMEA-HPO—that employs SMAC to tune the algorithm’s parameters in the loop.
Running example.py
example.py showcases a straightforward use-case of LLaMEA. It:
- Defines an evaluation function
evaluateBBOBthat runs generated algorithms on a standard set of BBOB problems (24 functions). - Initializes LLaMEA with a specific model (e.g., GPT-4, GPT-3.5) and prompts the LLM to generate metaheuristic code.
- Iterates over a
(1+1)-style evolutionary loop, refining the code until a certain budget is reached.
How to run:
python example.py
The script will:
- Query the specified LLM with a prompt describing the black-box optimization task.
- Dynamically execute each generated algorithm on BBOB problems.
- Log performance data such as AOCC (Area Over the Convergence Curve).
- Iteratively refine the best-so-far algorithms.
Running example_HPO.py (LLaMEA-HPO)
example_HPO.py extends LLaMEA with in-the-loop hyper-parameter optimization—termed LLaMEA-HPO. Instead of having the LLM guess or refine hyper-parameters directly, the code:
- Allows the LLM to generate a Python class representing the metaheuristic plus a ConfigSpace dictionary describing hyper-parameters.
- Passes these hyper-parameters to SMAC, which then searches for good parameter settings on a BBOB training set.
- Evaluates the best hyper-parameters found by SMAC on the full BBOB suite.
- Feeds back the final performance (and errors) to the LLM, prompting it to mutate the algorithm’s structure (rather than simply numeric settings).
Why LLaMEA-HPO?
Offloading hyper-parameter search to SMAC significantly reduces LLM query overhead and encourages the LLM to focus on novel structural improvements.
How to run:
python example_HPO.py
Script outline:
- Prompt & Generation: Script sets up a role/task prompt, along with hyper-parameter config space templates.
- HPO Step: For each newly generated algorithm, SMAC tries different parameter values within a budget.
- Evaluation: The final best configuration from SMAC is tested across BBOB instances.
- Refinement: The script returns the performance to LLaMEA, prompting the LLM to mutate the algorithm design.
[!Note] Adjust the model name (
ai_model) or API key as needed in the script. Changingbudgetor the HPO budget can drastically affect runtime and cost. Additional arguments (e.g., logging directories) can be set if desired.
🤖 Contributing
Contributions to LLaMEA are welcome! Here are a few ways you can help:
- Report Bugs: Use GitHub Issues to report bugs.
- Feature Requests: Suggest new features or improvements.
- Pull Requests: Submit PRs for bug fixes or feature additions.
Please refer to CONTRIBUTING.md for more details on contributing guidelines.
🪪 License
Distributed under the MIT License. See LICENSE for more information.
✨ Citation
If you use LLaMEA in your research, please consider citing the associated paper:
@article{van2024llamea,
title={Llamea: A large language model evolutionary algorithm for automatically generating metaheuristics},
author={van Stein, Niki and B{\"a}ck, Thomas},
journal={IEEE Transactions on Evolutionary Computation},
year={2024},
publisher={IEEE}
}
If you only want to cite the LLaMEA-HPO variant use the folllowing:
@article{van2024loop,
title={In-the-loop hyper-parameter optimization for llm-based automated design of heuristics},
author={van Stein, Niki and Vermetten, Diederick and B{\"a}ck, Thomas},
journal={arXiv preprint arXiv:2410.16309},
year={2024}
}
For more details, please refer to the documentation and tutorials available in the repository.
flowchart LR
A[Initialization] -->|Starting prompt| B{Stop? fa:fa-hand}
B -->|No| C(Generate Algorithm - LLM )
B --> |Yes| G{{Return best so far fa:fa-code}}
C --> |fa:fa-code|D(Evaluate)
D -->|errors, scores| E[Store session history fa:fa-database]
E --> F(Construct Refinement Prompt)
F --> B
CodeCov test coverage
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llamea-1.0.0.tar.gz.
File metadata
- Download URL: llamea-1.0.0.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15506092738376f0b52d8596560a5be336103fa7c5326b81709b3ce13392d852
|
|
| MD5 |
9b930d639b8d9661c54eacf486421090
|
|
| BLAKE2b-256 |
5400b8386108949c31e8facd14b3da93141383c30db5d2d01e8427cb8de059fa
|
Provenance
The following attestation bundles were made for llamea-1.0.0.tar.gz:
Publisher:
publish.yml on XAI-liacs/LLaMEA
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llamea-1.0.0.tar.gz -
Subject digest:
15506092738376f0b52d8596560a5be336103fa7c5326b81709b3ce13392d852 - Sigstore transparency entry: 176211905
- Sigstore integration time:
-
Permalink:
XAI-liacs/LLaMEA@03a48f223151b025422ba41517c0c86ae5fae425 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/XAI-liacs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@03a48f223151b025422ba41517c0c86ae5fae425 -
Trigger Event:
release
-
Statement type:
File details
Details for the file llamea-1.0.0-py3-none-any.whl.
File metadata
- Download URL: llamea-1.0.0-py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f71f928e7e4f54df48440e7ca1f757b41b620d572422fbe5e4f1bed5219e263
|
|
| MD5 |
105d64cbef0c698253e18d756fd5f2e2
|
|
| BLAKE2b-256 |
c453804add400079fb84e6510c1ef47e8c37a03e8f2b1116a62c87b12efb61b6
|
Provenance
The following attestation bundles were made for llamea-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on XAI-liacs/LLaMEA
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llamea-1.0.0-py3-none-any.whl -
Subject digest:
1f71f928e7e4f54df48440e7ca1f757b41b620d572422fbe5e4f1bed5219e263 - Sigstore transparency entry: 176211906
- Sigstore integration time:
-
Permalink:
XAI-liacs/LLaMEA@03a48f223151b025422ba41517c0c86ae5fae425 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/XAI-liacs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@03a48f223151b025422ba41517c0c86ae5fae425 -
Trigger Event:
release
-
Statement type: