## Mathematical Function Optimization Using DEAP

In this example, we will optimize a mathematical function using the $DEAP$ library.

The goal is to find the values of variables that minimize or maximize the value of a given function.

#### Objective:

We will optimize the well-known **Rastrigin function**, a common test function used in optimization problems.

The function is multimodal, meaning it has many local minima, making it challenging for algorithms to find the global minimum.

The Rastrigin function is given by the following equation:

$$

f(x_1, x_2, \dots, x_n) = 10n + \sum_{i=1}^{n} \left[ x_i^2 - 10 \cos(2 \pi x_i) \right]

$$

Where $( n )$ is the number of dimensions (variables), and $( x_i )$ are the decision variables.

The function has a global minimum at $( x_i = 0 )$ for all $( i )$, where $( f(x) = 0 )$.

### Problem Setup:

**Decision Variables**: $( x_1, x_2, \dots, x_n )$ (in this example, we’ll use $2$ dimensions).**Objective**: Minimize the value of the Rastrigin function.**Constraints**: The variables $( x_1, x_2, \dots, x_n )$ are usually constrained within a range, e.g., $( -5.12 \leq x_i \leq 5.12 )$.

### DEAP Setup for Optimization

We will use a $Genetic$ $Algorithm$ ($GA$) in $DEAP$ to solve this problem by evolving a population of solutions over multiple generations.

### DEAP Implementation

1 | import random |

### Explanation

**Problem Setup**:- We are working with the Rastrigin function in $2$ dimensions (though you can extend this to more dimensions).
- The decision variables $( x_1 )$ and $( x_2 )$ are initialized randomly within the range $([-5.12, 5.12])$.

**Fitness Function**:- The fitness function is the Rastrigin function, which calculates the fitness (objective value) for each individual.

We want to minimize this value.

- The fitness function is the Rastrigin function, which calculates the fitness (objective value) for each individual.
**Genetic Operators**:**Crossover (mate)**: We use the blend crossover (`cxBlend`

) to combine two individuals.

This mixes the variable values from two parents to create offspring.**Mutation (mutate)**: We use Gaussian mutation (`mutGaussian`

) to introduce small changes in the individuals by adding noise.**Selection**: Tournament selection (`selTournament`

) is used to select the best individuals from the population to continue to the next generation.

**Algorithm Execution**:- The algorithm runs for $50$ generations, evolving the population by applying crossover, mutation, and selection.
- At the end of the process, the algorithm selects the best individual (solution) with the minimum Rastrigin value.

### Sample Output:

1 | Best individual: [-0.9949586383085709, -1.964404107504631e-09] |

### Interpretation of Results:

- The
**best individual**represents the values of $( x_1 )$ and $( x_2 )$ that minimize the Rastrigin function. - The
**best fitness**is the minimum value of the Rastrigin function at that point, which should be close to zero (the global minimum of the Rastrigin function).

### Conclusion:

This example demonstrates how to use $DEAP$ to optimize a mathematical function, specifically the Rastrigin function.

The genetic algorithm evolves a population of solutions, eventually finding the optimal values for the decision variables that minimize the function.