# Evolutionary Optimizer: Genetic Algorithms

> Learn how to use the Evolutionary Optimizer to discover optimal prompts through genetic algorithms, with support for multi-objective optimization and LLM-driven genetic operations.

The `EvolutionaryOptimizer` uses genetic algorithms to refine and discover effective prompts. It
iteratively evolves a population of prompts, applying selection, crossover, and mutation operations
to find prompts that maximize a given evaluation metric. This optimizer can also perform
multi-objective optimization (e.g., maximizing score while minimizing prompt length) and leverage
LLMs for more sophisticated genetic operations.

<Note>
  `EvolutionaryOptimizer` is a great choice when you want to explore a very diverse range of prompt
  structures or when you have multiple objectives to optimize for (e.g., performance score and
  prompt length). Its strength lies in its ability to escape local optima and discover novel prompt
  solutions through its evolutionary mechanisms, especially when enhanced with LLM-driven genetic
  operators.
</Note>

## How It Works

The `EvolutionaryOptimizer` is built upon the [DEAP](https://deap.readthedocs.io/) library for
evolutionary computation. The core concept behind the optimizer is that we evolve a population of
prompts over multiple generations to find the best one.

We utilize different techniques to evolve the population of prompts:

* **Selection**: We select the best prompts from the population to be the parents of the next generation.
* **Crossover**: We crossover the parents to create the children of the next generation.
* **Mutation**: We mutate the children to create the new population of prompts.

We repeat this process for a number of generations until we find the best prompt.

<Frame>
  <img src="https://files.buildwithfern.com/https://opik.docs.buildwithfern.com/docs/opik/2026-01-30T17:33:07.444Z/img/agent_optimization/evolutionary_optimizer.png" alt="Evolutionary Optimizer" />
</Frame>

<Tip>
  The optimizer is open-source, you can check out the code in the
  [Opik repository](https://github.com/comet-ml/opik/tree/main/sdks/opik_optimizer/src/opik_optimizer/algorithms/evolutionary_optimizer).
</Tip>

## Quickstart

You can use the `EvolutionaryOptimizer` to optimize a prompt:

```python maxLines=1000
from opik_optimizer import EvolutionaryOptimizer
from opik.evaluation.metrics import LevenshteinRatio # or any other suitable metric
from opik_optimizer import datasets, ChatPrompt