Skip to content

Commit c406531

Browse files
authored
Document multi-objective options and example in docs (#128)
1 parent 5b92309 commit c406531

4 files changed

Lines changed: 69 additions & 6 deletions

File tree

‎docs/Quickstart.rst‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ Quick Start
3131
Examples
3232
--------
3333

34-
Below are three example scripts demonstrating LLaMEA in action for black-box
35-
optimization with a BBOB (24 noiseless) function suite, and one Automated Machine Learning use-case.
34+
Below are four example scripts demonstrating LLaMEA in action for black-box
35+
optimization with a BBOB (24 noiseless) function suite, a multi-objective
36+
optimization workflow, and one Automated Machine Learning use-case.
3637
One of the black-box optimization scripts (`example.py`) runs basic LLaMEA, while the other (`example_HPO.py`) incorporates
3738
a **hyper-parameter optimization** pipeline—known as **LLaMEA-HPO**—that employs
3839
SMAC to tune the algorithm’s parameters in the loop.
@@ -124,4 +125,35 @@ In this example, a basic classification task on the breast-cancer dataset from s
124125
125126
.. note::
126127
Adjust the model name (`ai_model`) or API key as needed in the script.
127-
You can easily change the dataset, task and evaluation function to fit your needs.
128+
You can easily change the dataset, task and evaluation function to fit your needs.
129+
130+
131+
Running ``multi_objective.py``
132+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133+
134+
**``multi_objective.py``** demonstrates Pareto-based optimization with
135+
LLaMEA on a synthetic Travelling Salesman Problem variant that optimizes
136+
two conflicting objectives:
137+
138+
- **Distance**: total route length.
139+
- **Fuel**: route cost with load-dependent fuel consumption.
140+
141+
The example highlights:
142+
143+
- Returning a :class:`~llamea.multi_objective_fitness.Fitness` object from
144+
the evaluator.
145+
- Enabling ``multi_objective=True`` in :class:`~llamea.llamea.LLaMEA`.
146+
- Passing ``multi_objective_keys=["Distance", "Fuel"]`` so objective values
147+
are tracked consistently.
148+
- Receiving a :class:`~llamea.pareto_archive.ParetoArchive` and extracting the
149+
final non-dominated set.
150+
151+
How to run:
152+
153+
.. code-block:: bash
154+
155+
python multi_objective.py
156+
157+
.. note::
158+
The script defaults to an Ollama model (``gemma3:12b``). Update the LLM
159+
backend and credentials to match your local setup.

‎docs/llamea.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Recent features include:
1515
instead of entire source files from the LLM. This is more token efficient for large code bases.
1616
* **Population evaluation** – with ``evaluate_population=True`` the evaluation
1717
function ``f`` operates on lists of solutions, allowing batch evaluations.
18+
* **Multi-objective mode** – set ``multi_objective=True`` and provide
19+
``multi_objective_keys=[...]`` to optimize multiple objectives and maintain a
20+
Pareto archive instead of a single best solution.
1821
* **Warm start** -With every iteration, **LLaMEA** archives its latest run in
1922
`<experiment_log_directory>/llamea_config.pkl`. The framework provides
2023
**``warm_start`` class methods** that allow you to resume from a previously
@@ -52,6 +55,8 @@ The most important keyword arguments of :class:`LLaMEA` are summarised below.
5255
- Prompt engineering controls.
5356
* - ``mutation_prompts`` / ``adaptive_mutation`` / ``adaptive_prompt``
5457
- Mutation and prompt adaptation settings.
58+
* - ``multi_objective`` / ``multi_objective_keys``
59+
- Enable Pareto-based optimization and define objective names.
5560
* - ``budget`` / ``eval_timeout`` / ``max_workers`` / ``parallel_backend``
5661
- Runtime and parallelisation controls.
5762
* - ``log`` / ``experiment_name``

‎examples/multi_objective.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""Multi-objective LLaMEA example on a synthetic TSP variant.
2+
3+
This script shows how to:
4+
1. Evaluate generated code against two objectives (Distance and Fuel).
5+
2. Return objective values using ``Fitness``.
6+
3. Run LLaMEA with ``multi_objective=True`` and objective keys.
7+
4. Read final non-dominated solutions from ``ParetoArchive``.
8+
"""
9+
110
import os
211
import random
312
from typing import Optional
@@ -26,6 +35,7 @@ def __repr__(self):
2635

2736

2837
def generate_tsp_test(seed: Optional[int] = None, size: int = 10):
38+
"""Generate a depot and customer set for the synthetic TSP task."""
2939
if seed is not None:
3040
random.seed(seed)
3141
depot = Location(0, 50, 50, 0)
@@ -46,6 +56,12 @@ def generate_tsp_test(seed: Optional[int] = None, size: int = 10):
4656
referable_dict[customer.id] = customer
4757

4858
def evaluate(solution: Solution, explogger: Optional[ExperimentLogger] = None):
59+
"""Evaluate generated solver code on a two-objective TSP benchmark.
60+
61+
The generated class must return a permutation of customer ids. The evaluator
62+
validates the route, computes total travel distance and load-dependent fuel
63+
usage, then stores a ``Fitness`` object with both objectives.
64+
"""
4965
code = solution.code
5066

5167
global_ns, issues = prepare_namespace(
@@ -180,7 +196,8 @@ def __call__():
180196
return customer_ids
181197
"""
182198

183-
llamea_inst = LLaMEA(f=evaluate,
199+
# Multi-objective mode returns a Pareto archive instead of a single winner.
200+
llamea_inst = LLaMEA(f=evaluate,
184201
llm=llm,
185202
multi_objective=True,
186203
max_workers=3,
@@ -192,10 +209,11 @@ def __call__():
192209
example_prompt=example_prompt,
193210
experiment_name="MOO-TSP",
194211
minimization=True,
195-
budget=27
212+
budget=27
196213
)
197214

198215
solutions = llamea_inst.run()
216+
# Keep only the final non-dominated set for reporting/inspection.
199217
if isinstance(solutions, ParetoArchive):
200218
solutions = solutions.get_best()
201219

@@ -206,4 +224,4 @@ def __call__():
206224
print(solutions.description)
207225
print(solutions.code)
208226
print(solutions.fitness)
209-
print("------------------------------------------------------------------------------------------------------------------------")
227+
print("------------------------------------------------------------------------------------------------------------------------")

‎llamea/llamea.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def __init__(
110110
task_prompt (str): A prompt describing the task for the language model to generate optimization algorithms.
111111
example_prompt (str): An example prompt to guide the language model in generating code (or None for default).
112112
output_format_prompt (str): A prompt that specifies the output format of the language model's response.
113+
multi_objective (bool): Enable multi-objective optimization mode.
114+
When set to ``True``, the evaluation function should assign a
115+
:class:`~llamea.multi_objective_fitness.Fitness` object via
116+
:meth:`~llamea.solution.Solution.set_scores`.
117+
multi_objective_keys (list[str]): Ordered objective names used by
118+
the multi-objective pipeline (e.g. ``["Distance", "Fuel"]``).
119+
Each key must be present in every returned
120+
:class:`~llamea.multi_objective_fitness.Fitness` object.
113121
experiment_name (str): The name of the experiment for logging purposes.
114122
elitism (bool): Flag to decide if elitism should be used in the evolutionary process.
115123
HPO (bool): Flag to decide if hyper-parameter optimization is part of the evaluation function.

0 commit comments

Comments
 (0)