A Python implementation of a Genetic Algorithm to solve the Vehicle Routing Problem (VRP). This project demonstrates how to find optimal or near-optimal routes for multiple vehicles to serve a set of locations from a central depot.
- Solves the Capacitated Vehicle Routing Problem (CVRP)
- Multi-objective optimization (minimize total distance and balance vehicle loads)
- Interactive visualization of routes
- Configurable number of locations and vehicles
- Detailed evolution statistics
- Python 3.7+
- NumPy
- Matplotlib
- DEAP (Distributed Evolutionary Algorithms in Python)
-
Clone the repository:
git clone https://github.com/Rajdeep-naha/VRP-Genetic_Algorithm.git cd VRP-Genetic_Algorithm -
Create and activate a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required packages:
pip install -r requirements.txt
Run the main script:
python main.pyThe program will:
- Generate random locations
- Run the genetic algorithm optimization
- Display the best routes found
- Show visualizations of the routes and evolution progress
You can modify the following parameters in vrp_ga/config.py:
NUM_LOCATIONS: Number of locations to visit (default: 10)NUM_VEHICLES: Number of vehicles available (default: 3)MAP_WIDTHandMAP_HEIGHT: Size of the map (default: 100x100)POPULATION_SIZE: Size of the genetic algorithm population (default: 100)GENERATIONS: Number of generations to run (default: 50)CROSSOVER_RATE: Probability of crossover (default: 0.8)MUTATION_RATE: Probability of mutation (default: 0.2)
The program will display:
- Progress of the genetic algorithm
- Best solution found with total distance and balance metrics
- Visual representation of the routes
- Evolution of the population's fitness over generations
Generating random locations...
Generated 10 locations
Setting up genetic algorithm...
Starting genetic algorithm optimization...
gen nevals avg min
0 100 [717.2366877 71.20440608] [461.09477629 10.02940893]
...
Optimization complete!
Best solution found with total distance: 421.15 and balance: 8.24
Vehicle Routes: --------------------------------------------------
Vehicle 1: Depot -> Loc 2 -> Loc 5 -> Loc 8 -> Depot
Vehicle 2: Depot -> Loc 1 -> Loc 3 -> Loc 7 -> Depot
Vehicle 3: Depot -> Loc 4 -> Loc 6 -> Loc 9 -> Depot
Generating visualizations...
main.py: Entry point of the applicationvrp_ga/: Main package__init__.py: Package initializationconfig.py: Configuration parametersdata.py: Data generation and utility functionsgenetic_algorithm.py: Core genetic algorithm implementationvisualization.py: Plotting and visualization functions
requirements.txt: List of Python dependencies
This project is licensed under the MIT License - see the LICENSE file for details.
Rajdeep Naha
Contributions are welcome! Please feel free to submit a Pull Request.