LLaMEA¶
The LLaMEA
class implements the evolutionary loop
around a large language model. Its behaviour is governed by many
hyper-parameters controlling population sizes, mutation style, diversity and
evaluation.
Recent features include:
Niching – enable
niching="sharing"
orniching="clearing"
to maintain diversity.distance_metric
,niche_radius
,adaptive_niche_radius
andclearing_interval
further tune the niches.Unified diff mode – set
diff_mode=True
to request unified diff patches instead of entire source files from the LLM.Population evaluation – with
evaluate_population=True
the evaluation functionf
operates on lists of solutions, allowing batch evaluations.
Initialization Parameters¶
The most important keyword arguments of LLaMEA
are summarised below.
Parameter |
Meaning |
---|---|
|
Evaluation function returning feedback, fitness and error. |
|
Language model wrapper used for generation. |
|
Number of parents and offspring per generation. |
|
|
|
Prompt engineering controls. |
|
Mutation and prompt adaptation settings. |
|
Runtime and parallelisation controls. |
|
Logging configuration. |
|
Special operation modes. |
|
Diversity management. |
|
Use population-level evaluation. |
|
Request unified diff patches. |
LLaMEA - LLM powered Evolutionary Algorithm for code optimization This module integrates OpenAI’s language models to generate and evolve algorithms to automatically evaluate (for example metaheuristics evaluated on BBOB).
- class llamea.llamea.LLaMEA(f, llm, n_parents=5, n_offspring=5, role_prompt='', task_prompt='', example_prompt=None, output_format_prompt=None, experiment_name='', elitism=True, HPO=False, mutation_prompts=None, adaptive_mutation=False, adaptive_prompt=False, budget=100, eval_timeout=3600, max_workers=10, parallel_backend='loky', log=True, minimization=False, _random=False, niching: str | None = None, distance_metric: Callable[[Solution, Solution], float] | None = None, niche_radius: float | None = None, adaptive_niche_radius: bool = False, clearing_interval: int | None = None, evaluate_population=False, diff_mode: bool = False)¶
Bases:
object
A class that represents the Language Model powered Evolutionary Algorithm (LLaMEA). This class handles the initialization, evolution, and interaction with a language model to generate and refine algorithms.
- adapt_niche_radius(population)¶
Adapt the niche radius based on the current population.
- apply_niching(population)¶
Apply the configured niching strategy to
population
.
- construct_prompt(individual)¶
Constructs a new session prompt for the language model based on a selected individual.
- Args:
individual (dict): The individual to mutate.
- Returns:
list: A list of dictionaries simulating a conversation with the language model for the next evolutionary step.
- evaluate_fitness(individual)¶
Evaluates the fitness of the provided individual by invoking the evaluation function f. This method handles error reporting and logs the feedback, fitness, and errors encountered.
- Args:
individual (Solution): The solution instance to evaluate.
- Returns:
Solution: The updated solution with feedback, fitness and error information filled in.
- evaluate_population_fitness(new_population)¶
Evaluate a full population of solutions.
- evolve_solution(individual)¶
Evolves a single solution by constructing a new prompt, querying the LLM, and evaluating the fitness.
- initialize()¶
Initializes the evolutionary process by generating the first parent population.
- initialize_single()¶
Initializes a single solution.
- logevent(event)¶
- optimize_task_prompt(individual)¶
Use the LLM to improve the task prompt for a given individual.
- run()¶
Main loop to evolve the solutions until the evolutionary budget is exhausted. The method iteratively refines solutions through interaction with the language model, evaluates their fitness, and updates the best solution found.
- Returns:
tuple: A tuple containing the best solution and its fitness at the end of the evolutionary process.
- selection(parents, offspring)¶
Select the new population based on the parents and the offspring and the current strategy.
- Args:
parents (list): List of solutions. offspring (list): List of new solutions.
- Returns:
list: List of new selected population.
- update_best()¶
Update the best individual in the new population