A fascinating pathfinding simulation where little agents teach themselves to navigate around obstacles using the power of evolution!
While Genetic Algorithms (GAs) are a foundational concept in machine learning, I've always been fascinated by their elegant simplicity. To get my hands dirty again, I recently built this fun pathfinding simulation where little agents teach themselves how to navigate a maze.
It got me thinking: What if you could solve complex problems by mimicking nature's oldest and most powerful trick: evolution?
That's the core idea behind Genetic Algorithms. This project demonstrates what a GA is, breaks down its core components, and shows how I used one to teach agents to find the best path through a simple maze.
- Agents: 10 yellow circular agents start at the left side of the screen
- Target: Red circle on the right side that agents need to reach
- Obstacle: Black barrier that agents must navigate around
- Goal: Find the optimal path from start to target while avoiding obstacles
- Initialization: Each agent gets a random path (sequence of movement vectors)
- Simulation: Agents follow their paths simultaneously, with the best agent highlighted in red
- Fitness Evaluation: Agents are scored based on distance to target
- Selection: Agents are sorted by fitness, with the best performers selected as parents
- Crossover: Child agents inherit path segments from two parent agents
- Mutation: Random variations are added to the last part of each agent's path
- Elitism: The best agent from each generation survives unchanged
- Success Detection: Simulation stops when an agent successfully reaches the target!
- Population: 10 agents per generation with color-coded visualization
- Genome: Each agent's path (120 movement steps)
- Fitness Function: Euclidean distance to target (lower = better)
- Selection: Tournament selection from top 50% of population
- Mutation: Targeted mutations on the final 40 steps of the path
- Crossover: Single-point crossover between two parent agents
- Elitism: Best agent automatically survives to next generation
- Processing IDE (version 3.0 or higher)
- Clone this repository:
git clone https://github.com/PinsaraPerera/Fun-Intro-to-Genetic-Algorithms.git
- Open
agents.pdein Processing IDE - Click the "Run" button or press
Ctrl+R - Watch the magic happen! 🎭
- Colorful agents moving from left to right (red = best performer, others in rainbow colors)
- A generation counter showing evolutionary progress
- Agents getting progressively better at avoiding the obstacle
- Success message when an agent finally reaches the target!
- The simulation stops automatically upon success, showing which agent and generation achieved it
You can tweak various parameters to experiment:
int NUMBER_OF_AGENTS = 10; // Population size
int STEPS = 120; // Path length (genome size)
float TARGET_X = 550; // Target position
float TARGET_Y = 200;
int frameRate = 60; // Simulation speed- Increase population size: More diversity but slower evolution
- Adjust mutation rate: Change the random range in the
mutate()function - Move the obstacle: Modify the barrier position in
draw() - Add more obstacles: Create a more complex maze
- Change crossover point: Modify where parent paths are combined
- Adjust mutation scope: Change which steps get mutated (currently last 40)
This simulation demonstrates key concepts:
- Emergent Behavior: Complex pathfinding emerges from simple rules
- Natural Selection: Better solutions naturally dominate through sorting
- Genetic Recombination: Crossover combines successful strategies from multiple parents
- Targeted Mutation: Strategic mutations focus on the end of paths for fine-tuning
- Elitist Strategy: Preserving the best solution while exploring variations
- Success Convergence: Evolution naturally leads to problem-solving success
- Agent: Represents individual organisms with position, path, fitness, and color-coded rendering
- DrawLine: Handles the visual starting line indicator
next_generation(): Implements elitist selection with crossover and mutationcrossover(): Creates child agents by combining parent paths at random cut pointsmutate(): Introduces targeted random variations in the final path segmentscalculateDistance(): Fitness evaluation using Euclidean distance
This implementation uses an Elitist Genetic Algorithm with:
- Fitness-proportionate selection from top 50% of population
- Single-point crossover between two randomly selected parents
- Targeted mutation on the last 40 movement steps
- Elitism ensuring the best agent always survives
- Success detection to automatically stop when target is reached
- Implement different crossover strategies (multi-point, uniform)
- Add dynamic obstacles that move between generations
- Implement tournament selection with different tournament sizes
- Add real-time parameter adjustment with keyboard controls
- 3D environment navigation
- Multi-objective optimization (speed + efficiency + path smoothness)
- Population diversity metrics and visualization
- Different mutation strategies (Gaussian, adaptive)
Feel free to experiment and improve! Some ideas:
- Add new obstacle types
- Implement different mutation strategies
- Create more complex fitness functions
- Add visualization of the evolutionary process
Want to dive deeper into Genetic Algorithms?
This project is open source and available under the MIT License.
Happy Evolving! 🧬✨
Built with ❤️ using Processing


