This repository contains implementations of genetic algorithms (GA) to solve complex optimization problems, specifically the Rastrigin and Rosenbrock functions. These functions are common benchmarks for optimization algorithms due to their challenging landscapes.
Genetic algorithms are a type of evolutionary algorithm inspired by natural selection. They work by:
- Creating a population of potential solutions (individuals)
- Evaluating each solution's fitness
- Selecting the best individuals to reproduce
- Creating new solutions through crossover and mutation
- Repeating the process for multiple generations
This project demonstrates GA implementations for two classic optimization test functions:
- A non-convex function with many local minima
- Global minimum at
f(x) = 0when allx = 0 - Implementation uses 3-dimensional space with value range between -5.12 and 5.12
- A non-convex function with a narrow curved valley
- Global minimum at
f(x) = 0when allx = 1 - Implementation uses 3-dimensional space with value range between -10 and 10
- Multiple Implementations: Standard GA and GA with elitism
- Linear Crossover: Different crossover strategies implemented
- Adaptive Mutation: 20% mutation rate with boundary constraints
- Visualization: Results plotted using matplotlib
- Benchmarking: Save and analyze results across multiple runs
Clone the repository and install the required dependencies:
git clone https://github.com/yourusername/GeneticAlgo.git
cd GeneticAlgo
pip install -r requirements.txt- numpy
- matplotlib
- pandas
- random
- time
cd rastrigin
python app.py # Standard implementation
python "app + elitism.py" # Implementation with elitismcd rosenbrock
python "app + elitism.py"For the Rastrigin implementation that saves results to CSV:
cd rastrigin
python read.py- Population size: Configurable (default 400 for standard, 10000 for elitism version)
- Dimensions: 3D optimization
- Selection: Fitness-proportionate selection
- Crossover: Linear crossover with three possible child points
- Mutation: 20% mutation rate
- Boundary constraint: Values constrained to [-5.12, 5.12]
- Termination: Fixed number of generations (default 1000)
- Population size: User-configurable
- Dimensions: 3D optimization
- Selection: Exponential fitness-proportionate selection
- Crossover: Linear crossover with three possible child points
- Mutation: 20% mutation rate
- Boundary constraint: Values constrained to [-10, 10]
- Elitism: Best solutions preserved between generations
- Termination: User-defined maximum generations
The algorithms visualize optimization progress by plotting the best fitness value in each generation. Annotations highlight the generation number and fitness value when the best solution was found.
For the Rastrigin implementation with data collection (app.py), results from 100 runs are stored in a CSV file and can be analyzed using read.py.
Contributions are welcome! Some potential improvements:
- Implement other optimization test functions
- Add different selection methods
- Experiment with adaptive mutation rates
- Add parallel processing for faster execution
- Improve visualization options

