<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mayank Singh</title>
    <description>The latest articles on DEV Community by Mayank Singh (@mayank_singh5).</description>
    <link>https://dev.to/mayank_singh5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3945854%2Fda390f91-6252-4252-bf6c-fd4775ea9565.jpg</url>
      <title>DEV Community: Mayank Singh</title>
      <link>https://dev.to/mayank_singh5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mayank_singh5"/>
    <language>en</language>
    <item>
      <title>Solving a Logistics Problem Using Genetic Algorithms</title>
      <dc:creator>Mayank Singh</dc:creator>
      <pubDate>Fri, 22 May 2026 15:36:01 +0000</pubDate>
      <link>https://dev.to/mayank_singh5/solving-a-logistics-problem-using-genetic-algorithms-2b33</link>
      <guid>https://dev.to/mayank_singh5/solving-a-logistics-problem-using-genetic-algorithms-2b33</guid>
      <description>&lt;p&gt;A week back, I started exploring Genetic Algorithms — and honestly, &lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Here's how it went.&lt;/p&gt;


&lt;h2&gt;
  
  
  What Even Are Genetic Algorithms?
&lt;/h2&gt;

&lt;p&gt;The idea is borrowed directly from Darwin's theory of evolution. &lt;br&gt;
In nature, organisms that are better adapted to their environment &lt;br&gt;
survive and reproduce, passing their traits to the next generation. &lt;br&gt;
Over time, the population gets better and better at surviving.&lt;br&gt;
"Survival of the fittest".&lt;/p&gt;

&lt;p&gt;GAs do the exact same thing — but with solutions to a problem.&lt;/p&gt;

&lt;p&gt;Here's the basic loop:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Population&lt;/strong&gt; — Start with a bunch of random solutions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fitness&lt;/strong&gt; — Evaluate how good each solution is&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Selection&lt;/strong&gt; — Pick the better solutions to "reproduce"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crossover&lt;/strong&gt; — Combine two solutions to create new ones&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mutation&lt;/strong&gt; — Randomly tweak solutions to maintain diversity&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeat&lt;/strong&gt; — Do this for number of generations&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each generation, usually population of solutions gets a little bit better (better fitness).&lt;br&gt;
Not guaranteed to find the perfect solution — but surprisingly good &lt;br&gt;
at finding a very good one, very fast.&lt;/p&gt;

&lt;p&gt;That's the magic of it.&lt;/p&gt;


&lt;h2&gt;
  
  
  The Vehicle Routing Problem
&lt;/h2&gt;

&lt;p&gt;Now, the problem I chose to throw this at.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This is the Vehicle Routing Problem (VRP), and it shows up everywhere — Amazon delivery routes, food delivery apps, garbage collection, even school bus scheduling.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
(Keep this example in mind, I have used it to explain my approach)&lt;/p&gt;

&lt;p&gt;Enter the Genetic Algorithm.&lt;/p&gt;


&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;I built this using Python and the DEAP library, which provides me &lt;br&gt;
the building blocks for evolutionary algorithms.&lt;/p&gt;
&lt;h3&gt;
  
  
  Representing a Solution
&lt;/h3&gt;

&lt;p&gt;Keep in mind &lt;/p&gt;

&lt;p&gt;The first design decision is how to encode a solution. I used a &lt;br&gt;
permutation of location indices — a shuffled list of numbers from &lt;br&gt;
0 to 39, where the order determines which locations each vehicle visits.&lt;/p&gt;

&lt;p&gt;Vehicles are assigned stops in a round-robin fashion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vehicle 1 gets indices 0, 4, 8, ...&lt;/li&gt;
&lt;li&gt;Vehicle 2 gets indices 1, 5, 9, ...&lt;/li&gt;
&lt;li&gt;Vehicle 1 gets indices 2, 6, 10, ...&lt;/li&gt;
&lt;li&gt;Vehicle 2 gets indices 3, 7, 11, ...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This keeps the representation simple and makes crossover and &lt;br&gt;
mutation straightforward.&lt;/p&gt;
&lt;h3&gt;
  
  
  Fitness Function
&lt;/h3&gt;

&lt;p&gt;The fitness function evaluates how good a solution is. Mine tracks &lt;br&gt;
two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Total distance&lt;/strong&gt; — sum of all routes across all vehicles 
(primary objective, weight = -100)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standard deviation of distances&lt;/strong&gt; — how balanced the workload 
is across vehicles (secondary objective, weight = -1)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both are minimized. The high weight on total distance means the GA &lt;br&gt;
primarily optimizes for shorter routes, but also nudges toward &lt;br&gt;
balanced workloads.&lt;/p&gt;
&lt;h3&gt;
  
  
  Genetic Operators
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Crossover — Ordered Crossover (OX):&lt;/strong&gt; Preserves the relative &lt;br&gt;
order of locations from one parent while filling in the rest from &lt;br&gt;
the other, prevents duplicate visits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mutation — Shuffle Indexes:&lt;/strong&gt; Randomly swaps a few locations &lt;br&gt;
in the route. Keeps solutions valid while introducing variation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Selection — Tournament Selection:&lt;/strong&gt; Picks the best individual &lt;br&gt;
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.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Elitism
&lt;/h3&gt;

&lt;p&gt;One thing I added on top of the standard GA loop is elitism — &lt;br&gt;
after each generation, I force the all-time best solution back &lt;br&gt;
into the population. This prevents the algorithm from accidentally &lt;br&gt;
losing a great solution during a heavy mutation phase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toolbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hof&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# Elitism
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Small change, big impact on convergence stability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Experimentation and Results
&lt;/h2&gt;

&lt;p&gt;This is where things got interesting. I ran two phases of experiments to understand how each hyperparameter affects the algorithm's behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  Phase 1 — Parameter Sweeps
&lt;/h3&gt;

&lt;p&gt;I tested three parameters independently:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Population Size (5, 50, 500)&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mutation Rate (0.008, 0.08, 0.8)&lt;/strong&gt;&lt;br&gt;
Too low and the population converges prematurely — everyone looks &lt;br&gt;
the same and improvement stalls. Too high and you're basically &lt;br&gt;
random search. 0.08 hit the sweet spot in my experiments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tournament Size (2, 20, 200)&lt;/strong&gt;&lt;br&gt;
This controls selection pressure. A tournament size of 200 in a &lt;br&gt;
population of 300 means almost always picking the very best — &lt;br&gt;
which sounds good but kills diversity fast. Size 2 is too random. &lt;br&gt;
20 worked better.&lt;/p&gt;

&lt;h3&gt;
  
  
  Phase 2 — Diversity vs Convergence
&lt;/h3&gt;

&lt;p&gt;I then ran four full configurations over 300 generations and tracked both the best distance and population diversity (std dev of fitness):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Configuration&lt;/th&gt;
&lt;th&gt;Mutation&lt;/th&gt;
&lt;th&gt;Tournament&lt;/th&gt;
&lt;th&gt;Behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Base&lt;/td&gt;
&lt;td&gt;0.4&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;Fast convergence, low diversity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Low Selection&lt;/td&gt;
&lt;td&gt;0.4&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Slow convergence, high diversity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Low Mutation&lt;/td&gt;
&lt;td&gt;0.05&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;Premature convergence&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Balanced&lt;/td&gt;
&lt;td&gt;0.2&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Best overall trade-off&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The key insight: &lt;strong&gt;high selection pressure + low mutation = premature convergence&lt;/strong&gt;. The population homogenizes too quickly and gets stuck in a local optimum. You need enough mutation to keep exploring even as the population converges.&lt;/p&gt;

&lt;p&gt;The balanced configuration consistently gave the best results — &lt;br&gt;
not the fastest to converge, but the lowest final distance.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Going into this, I expected genetic algorithms to feel like a black &lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;A few things that stuck with me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Representation matters more than I expected.&lt;/strong&gt; The round-robin &lt;br&gt;
assignment is simple but works well. A different encoding could &lt;br&gt;
change everything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Diversity is underrated.&lt;/strong&gt; It's tempting to crank up selection &lt;br&gt;
pressure to get fast convergence, but you pay for it later when &lt;br&gt;
the algorithm gets stuck.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Elitism is a small change with a big payoff.&lt;/strong&gt; Without it, good solutions can get lost during mutation-heavy phases.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The VRP is just one application — the same GA framework could be &lt;br&gt;
adapted for scheduling, network design, or any combinatorial &lt;br&gt;
optimization problem where brute force isn't an option.&lt;/p&gt;

&lt;p&gt;If you want to dig into the code, the full notebook is on GitHub:&lt;br&gt;
&lt;a href="https://github.com/Mayank-Singh5/vehicle-routing-optimization" rel="noopener noreferrer"&gt;Vehicle Routing Optimization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also I have uploaded some images showing progress of generations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foswhzkw0680omwmnvrjv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foswhzkw0680omwmnvrjv.png" alt=" " width="465" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fboh9pwqm6p92qwt6zhiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fboh9pwqm6p92qwt6zhiw.png" alt=" " width="466" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F055dko44e016oazls0tw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F055dko44e016oazls0tw.png" alt=" " width="465" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgss84xo723laf940gfem.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgss84xo723laf940gfem.png" alt=" " width="465" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0lb2wu7zyqnwkmqs02d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0lb2wu7zyqnwkmqs02d.png" alt=" " width="465" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuozsgqlct9q8gpom2tyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuozsgqlct9q8gpom2tyl.png" alt=" " width="465" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8i9pfwcpcwv7rjogxr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8i9pfwcpcwv7rjogxr4.png" alt=" " width="800" height="210"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptex7bsq9lns9lgzausc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptex7bsq9lns9lgzausc.png" alt=" " width="800" height="639"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
