Evolutionary Trait Optimization Using DEAP
Let’s explore an optimization problem inspired by biological evolution, specifically survival of the fittest in an ecosystem.
In this example, different species of organisms are competing for resources, and the goal is to optimize their fitness over time by evolving traits that help them survive and reproduce.
Biological Evolution Optimization Problem
Consider a simplified model where organisms have three key traits:
- Speed: Affects the ability to escape predators.
- Strength: Affects the ability to hunt and gather resources.
- Intelligence: Affects the ability to adapt to environmental changes.
The goal is to evolve a population of organisms to maximize their overall fitness, which is a function of these traits.
The fitness function is given as:
$$
f(\text{speed}, \text{strength}, \text{intelligence}) = \text{speed} \times \text{strength} + \text{intelligence}^2 - \frac{(\text{speed} + \text{strength} + \text{intelligence})^2}{10}
$$
where the three traits $( \text{speed}, \text{strength}, \text{intelligence} )$ are real-valued variables ranging between $0$ and $10$.
Problem Setup
- Objective: Maximize the fitness function, which simulates the survival advantage of organisms.
- Constraints:
- Each trait (speed, strength, intelligence) must be within the range $([0, 10])$.
- Evolutionary Method: We will use a Genetic Algorithm (GA) via $DEAP$ to simulate the evolutionary process and optimize the population’s traits.
DEAP Implementation
The Genetic Algorithm will evolve a population of organisms, each represented by three variables (speed, strength, intelligence).
Over generations, the traits that provide higher fitness will propagate through the population.
Step-by-Step Process:
- Initialization: A random population of organisms (each with speed, strength, and intelligence) is generated.
- Selection: The fittest organisms are selected to reproduce, passing on their traits to the next generation.
- Crossover: Traits are combined from two parent organisms to produce offspring.
- Mutation: Random mutations introduce variations in traits, simulating biological mutations.
- Evolution: Over generations, the population evolves towards higher fitness.
Here is how we can implement this in $DEAP$:
1 | import random |
Explanation of the Code
- Fitness Function: The
fitness_function
simulates the organism’s fitness based on speed, strength, and intelligence.
It rewards combinations of traits that lead to better survival while penalizing extreme values that might be biologically impractical (e.g., overly strong or fast organisms that exhaust themselves quickly). - Bounds: We impose bounds on the traits to ensure that they stay within biologically plausible ranges ($0$ to $10$).
- Mating and Mutation:
- Mating:
cxBlend
combines the traits of two parent organisms.
We ensure the offspring traits remain within bounds. - Mutation:
mutGaussian
introduces random mutations to the traits.
After mutation, we apply the bounds to keep the traits within the valid range.
- Mating:
- Population Evolution: The algorithm evolves the population over $200$ generations.
During each generation, selection, crossover, and mutation are applied to produce new offspring, simulating biological evolution.
Running the Algorithm
When you run the genetic algorithm, it will evolve the population over time.
$DEAP$ will track the best individual (the organism with the highest fitness) and the average fitness in the population across generations.
The final result will show the traits of the fittest individual and their fitness score.
Biological Insights
This example mimics the evolutionary process, where organisms with the best combination of traits survive and reproduce.
The fitness landscape is non-linear, with trade-offs between speed, strength, and intelligence.
The genetic algorithm simulates how populations can optimize their traits over time to maximize survival.
This example showcases how $DEAP$ can be used to model and solve optimization problems inspired by biological evolution.
Output
1 | gen nevals avg min max |
This table shows the results of a genetic algorithm run over $200$ generations.
Here’s a simple breakdown of the columns:
- gen: The generation number, starting from $0$ up to $200$.
- nevals: Number of evaluations (solutions checked) in each generation.
- avg: The average fitness (quality of solutions) in that generation.
- min: The minimum fitness in that generation (worst solution).
- max: The maximum fitness in that generation (best solution).
The goal of the algorithm is to maximize fitness.
As we can see, the maximum fitness steadily improves across generations, starting from $84.94$ in generation $0$, and reaching the maximum of 110 by the end.
The best individual (solution) found is [10, 10.0, 10]
with a fitness value of 110.0.
This shows the algorithm successfully found an optimal or near-optimal solution.