Skip to content
Go back

Interactive Genetic Algorithm Demo: From Random Strings to Target Solutions

8 min read

What are Genetic Algorithms?

So, what exactly are Genetic Algorithms (GAs)? Think of them as a way to solve problems by mimicking how nature evolves things. It’s like having a bunch of random solutions and then “breeding” the best ones together to get even better solutions.

Here’s how it works in simple terms:

  1. Population: Start with a bunch of random solutions
  2. Fitness: Figure out which ones are the best
  3. Selection: Pick the best ones to “breed”
  4. Crossover: Mix parts of the good solutions together
  5. Mutation: Randomly change some things
  6. Repeat: Keep doing this until you get what you want

This post is heavily inspired by this website. However, I created the code with a very different methodology to include newer JavaScript methods using classes and also web workers so it runs behind the scenes.

The String Matching Problem

For this demo, we’re going to solve a pretty cool problem: string matching. We start with random strings and evolve them until they match a target string. It’s like teaching a computer to spell!

Here’s an example. Let’s say we want to get “HELLO WORLD”:

Interactive Genetic Algorithm Demo

Check out the interactive demo below! You can play around with different settings and see how the algorithm evolves solutions in real-time:





GenerationFitnessString

How the Algorithm Works

Selection Methods

MethodDescriptionEffectiveness
Tournament SelectionRandomly grab a few individuals and pick the best one from that small groupHigh
Random SelectionJust pick someone completely at randomLow
Rank SelectionLine everyone up from best to worst and pick based on their rankingMedium
Roulette Wheel SelectionLike a weighted lottery - the better your fitness, the bigger your chanceMedium-High

Crossover Methods

MethodDescriptionBest For
One Point CrossoverCut both parents at a random spot and swap the piecesString matching
Two Point CrossoverCut at two spots and swap the middle partBalanced problems
Uniform CrossoverFor each character, randomly pick from either parentHigh diversity needed

Example of One Point Crossover:

Parent 1: "HELLO WORLD"
Parent 2: "GOODBYE NOW"
Child: "HELLO NOW"

Key Findings

FindingImpactRecommendation
One Point Crossover works best for string matchingHigh success rateUse for text-based problems
Tournament selection performs better than randomFaster convergenceDefault choice for most problems
Shorter strings (5-10 chars) are much easier to solveQuick resultsStart with simple examples
Longer strings (20+ chars) take more time and may not convergeSlower, less reliableUse smaller targets for demos

Lessons Learned

Benefits of Using MDX

This post demonstrates the power of MDX (Markdown + JSX) for blog posts:

  1. Direct Component Import: Astro components can be imported and used directly in the content
  2. Interactive Content: Rich, interactive demos embedded seamlessly in text
  3. Better Developer Experience: TypeScript support, better tooling, and cleaner code
  4. Performance: Astro islands ensure only necessary JavaScript is loaded
  5. Flexibility: Mix static content with dynamic components as needed

Wrapping Up

Genetic algorithms are pretty awesome for solving complex problems. This interactive demo shows how you can take random solutions and evolve them into something useful through selection, crossover, and mutation.

The key takeaways:

Try playing around with different parameters in the demo above! It’s pretty cool to see how quickly random strings can evolve into meaningful solutions.

References

  1. Genetic Algorithm Fundamentals - Wikipedia overview
  2. JavaScript Genetic Algorithm Library - Original inspiration

Share this post on:

Previous Post
k-Nearest Neighbour on Maps
Next Post
Hacker News Word Cloud: Visualizing What's Trending