A week back, I started exploring Genetic Algorithms — and honestly,
the concept blew my mind a little. The idea that you can solve complex optimization problems by mimicking how evolution works felt almost too clever to be real.
So I needed a problem to test it on. Something non-trivial, something with real-world relevance. That's when I stumbled upon the Vehicle Routing Problem — figuring out the efficient routes for a fleet of vehicles to visit a set of locations and return to a depot.
Here's how it went.
What Even Are Genetic Algorithms?
The idea is borrowed directly from Darwin's theory of evolution.
In nature, organisms that are better adapted to their environment
survive and reproduce, passing their traits to the next generation.
Over time, the population gets better and better at surviving.
"Survival of the fittest".
GAs do the exact same thing — but with solutions to a problem.
Here's the basic loop:
- Population — Start with a bunch of random solutions
- Fitness — Evaluate how good each solution is
- Selection — Pick the better solutions to "reproduce"
- Crossover — Combine two solutions to create new ones
- Mutation — Randomly tweak solutions to maintain diversity
- Repeat — Do this for number of generations
Each generation, usually population of solutions gets a little bit better (better fitness).
Not guaranteed to find the perfect solution — but surprisingly good
at finding a very good one, very fast.
That's the magic of it.
The Vehicle Routing Problem
Now, the problem I chose to throw this at.
Imagine I run a delivery company. You have a central depot, a fleet of vehicles, and a bunch of locations to deliver to. My job is to figure out which vehicle visits which locations, and in what order — such that the total distance traveled is minimized and the workload is roughly balanced across vehicles.
This is the Vehicle Routing Problem (VRP), and it shows up everywhere — Amazon delivery routes, food delivery apps, garbage collection, even school bus scheduling.
The reason it's hard: the number of possible routes explodes combinatorially as you add more locations. For example, with 40 locations and 4 vehicles, brute-forcing every possible combination is completely out of the question. I need a smarter approach.
(Keep this example in mind, I have used it to explain my approach)
Enter the Genetic Algorithm.
Implementation
I built this using Python and the DEAP library, which provides me
the building blocks for evolutionary algorithms.
Representing a Solution
Keep in mind
The first design decision is how to encode a solution. I used a
permutation of location indices — a shuffled list of numbers from
0 to 39, where the order determines which locations each vehicle visits.
Vehicles are assigned stops in a round-robin fashion:
- Vehicle 1 gets indices 0, 4, 8, ...
- Vehicle 2 gets indices 1, 5, 9, ...
- Vehicle 1 gets indices 2, 6, 10, ...
- Vehicle 2 gets indices 3, 7, 11, ...
This keeps the representation simple and makes crossover and
mutation straightforward.
Fitness Function
The fitness function evaluates how good a solution is. Mine tracks
two things:
- Total distance — sum of all routes across all vehicles (primary objective, weight = -100)
- Standard deviation of distances — how balanced the workload is across vehicles (secondary objective, weight = -1)
Both are minimized. The high weight on total distance means the GA
primarily optimizes for shorter routes, but also nudges toward
balanced workloads.
Genetic Operators
Crossover — Ordered Crossover (OX): Preserves the relative
order of locations from one parent while filling in the rest from
the other, prevents duplicate visits.Mutation — Shuffle Indexes: Randomly swaps a few locations
in the route. Keeps solutions valid while introducing variation.Selection — Tournament Selection: Picks the best individual
from a random subset of certain size(=pop_size) from the population. Tournament size controls selection pressure — larger tournaments = stronger pressure toward the best solutions.
Elitism
One thing I added on top of the standard GA loop is elitism —
after each generation, I force the all-time best solution back
into the population. This prevents the algorithm from accidentally
losing a great solution during a heavy mutation phase.
pop[-1] = toolbox.clone(hof[0]) # Elitism
Small change, big impact on convergence stability.
Experimentation and Results
This is where things got interesting. I ran two phases of experiments to understand how each hyperparameter affects the algorithm's behavior.
Phase 1 — Parameter Sweeps
I tested three parameters independently:
Population Size (5, 50, 500)
Larger populations explore more of the solution space but are slower per generation. Too small and the GA gets stuck early. 500 gave noticeably better final distances than 5 or 50.
Mutation Rate (0.008, 0.08, 0.8)
Too low and the population converges prematurely — everyone looks
the same and improvement stalls. Too high and you're basically
random search. 0.08 hit the sweet spot in my experiments.
Tournament Size (2, 20, 200)
This controls selection pressure. A tournament size of 200 in a
population of 300 means almost always picking the very best —
which sounds good but kills diversity fast. Size 2 is too random.
20 worked better.
Phase 2 — Diversity vs Convergence
I then ran four full configurations over 300 generations and tracked both the best distance and population diversity (std dev of fitness):
| Configuration | Mutation | Tournament | Behavior |
|---|---|---|---|
| Base | 0.4 | 200 | Fast convergence, low diversity |
| Low Selection | 0.4 | 3 | Slow convergence, high diversity |
| Low Mutation | 0.05 | 200 | Premature convergence |
| Balanced | 0.2 | 5 | Best overall trade-off |
The key insight: high selection pressure + low mutation = premature convergence. The population homogenizes too quickly and gets stuck in a local optimum. You need enough mutation to keep exploring even as the population converges.
The balanced configuration consistently gave the best results —
not the fastest to converge, but the lowest final distance.
Conclusion
Going into this, I expected genetic algorithms to feel like a black
box — tweak some parameters, hope for the best. What I actually found was a surprisingly interpretable system where each design choice has a clear, observable effect on behavior.
A few things that stuck with me:
Representation matters more than I expected. The round-robin
assignment is simple but works well. A different encoding could
change everything.Diversity is underrated. It's tempting to crank up selection
pressure to get fast convergence, but you pay for it later when
the algorithm gets stuck.Elitism is a small change with a big payoff. Without it, good solutions can get lost during mutation-heavy phases.
The VRP is just one application — the same GA framework could be
adapted for scheduling, network design, or any combinatorial
optimization problem where brute force isn't an option.
If you want to dig into the code, the full notebook is on GitHub:
Vehicle Routing Optimization
Also I have uploaded some images showing progress of generations.

















