- Project Overview
- Demo
- Fitness Plot
- Understanding Period-N Oscillations
- Core Features
- Genetic Algorithm Objective & Parameters
- Installation & Setup
- Usage
- Output Interpretation
- Author
This project implements a sophisticated intersection of cellular automata and artificial intelligence: a Genetic Algorithm (GA) designed to evolve stable and long-lived patterns for Conway's Game of Life.
What it does:
- Uses evolutionary computation to discover patterns that survive as long as possible in Conway's Game of Life
- Employs advanced fitness evaluation that detects not just death, but also premature oscillations and stagnation
- Provides comprehensive visualization of both the evolutionary process and the resulting patterns
- Containerized with Docker for consistent execution across different environments
Why it matters: Conway's Game of Life, despite its simple rules, exhibits complex emergent behavior. Finding patterns that survive for extended periods is computationally challenging and provides insights into pattern formation, stability, and evolution in complex systems.
Watch the GA evolve patterns in real-time, discovering stable and long-lived configurations:
Example of a well-known period-3 oscillator that the GA might discover:
This graph illustrates the Genetic Algorithm's evolutionary progress over 2000 generations. With a population size of 300 and a mutation rate of 0.03, the "Best Fitness" steadily increased in distinct steps, reflecting the GA's ability to discover fitter patterns. The fitness ultimately plateaued around 1063, indicating the best pattern found survived for over half of the maximum possible 2000 simulation steps.
Best fitness score over 2000 generations
One of the key behaviors the GA evaluates is oscillation - patterns that repeat after a fixed number of generations. Understanding these patterns is crucial for interpreting fitness scores and evolutionary progress.
A period-n oscillation is a pattern that returns to its original state after exactly n generations:
- Period 1 (Still Lifes): Patterns that never change - completely stable
- Period 2: Patterns that alternate between two states
- Period 3: Patterns that cycle through three distinct states before repeating
- Period N: Patterns with any cycle length N
The simplest stable pattern - a 2×2 square that never changes:
Block pattern: Completely stable (Period 1)
A simple oscillator that alternates between horizontal and vertical orientations:
Blinker: Alternates between two states (Period 2)
Another period-2 oscillator with a different pattern:
Toad: Another period-2 oscillation pattern
A more complex oscillator that cycles through three distinct states:
Pulsar: Cycles through three states (Period 3)
The genetic algorithm's fitness function is designed to detect and penalize early oscillations:
- Early Oscillations = Lower Fitness: If a pattern quickly settles into a simple oscillation (like a blinker appearing at generation 5), it receives a low fitness score
- Late Oscillations = Higher Fitness: Patterns that evolve for many generations before entering an oscillation cycle receive higher fitness scores
- Complex vs. Simple: The GA rewards patterns that maintain complexity and activity longer before stabilizing
This design encourages the evolution of patterns with rich, long-lived dynamics rather than those that quickly settle into trivial behaviors.
- High-Performance Simulation: NumPy-based implementation optimized for speed and memory efficiency
- Configurable Board Size: Customizable grid dimensions (default: 20x20)
- Accurate Rule Implementation: Faithful reproduction of Conway's original rules
- Population Management: Initializes and evolves populations of Game of Life patterns
- Tournament Selection: Selects fitter parents through competitive selection process
- Uniform Crossover: Combines genetic material from parents with configurable crossover rate
- Adaptive Mutation: Introduces random changes to maintain genetic diversity
- Enhanced Fitness Evaluation:
- Detects pattern death (all cells become inactive)
- Identifies oscillation cycles (patterns that repeat)
- Rewards longevity while penalizing early stagnation
- Elitism Strategy: Preserves the best individual from each generation
- Real-time Progress Tracking: Live console output showing generation progress
- Fitness Plotting: Matplotlib-generated graphs showing evolutionary progress
- Pattern Visualization: Pygame-based display of evolved patterns in action
- Comprehensive Data Export: Multiple file formats for further analysis
- Docker Containerization: Ensures consistent runtime environment across platforms
- Data Persistence: Host-mapped volumes for result preservation
- Flexible Configuration: Easy parameter tuning through configuration files
The GA seeks to discover initial Game of Life patterns that maximize survival time while avoiding:
- Early Death: Patterns that quickly die out completely
- Premature Oscillation: Patterns that enter short cycles too early
- Stagnation: Patterns that become static (still lifes) without sufficient evolution
All parameters are defined in ga_parameters.py and can be modified before building the Docker image:
| Parameter | Default Value | Description | Impact |
|---|---|---|---|
ga_population_size |
200 | Number of patterns in each generation | Larger = more diversity, slower execution |
ga_num_generations |
10 | Maximum generations to evolve | More = better solutions, longer runtime |
ga_mutation_rate |
0.03 | Probability of cell mutation (3%) | Higher = more exploration, less stability |
ga_simulation_steps |
2000 | Max steps to evaluate each pattern | Longer = finds truly stable patterns |
ga_board_width |
20 | Grid width in cells | Larger = more complex patterns possible |
ga_board_height |
20 | Grid height in cells | Larger = more complex patterns possible |
ga_fitness_threshold |
2001 | Stop if fitness reaches this value | Early stopping for perfect solutions |
ga_crossover_rate |
0.8 | Probability of crossover (80%) | Higher = more genetic mixing |
ga_tournament_size |
5 | Number of candidates in selection | Larger = stronger selection pressure |
Quick Testing (faster execution):
ga_population_size = 50
ga_num_generations = 100
ga_simulation_steps = 500Deep Search (better results):
ga_population_size = 500
ga_num_generations = 2000
ga_simulation_steps = 5000High Mutation Exploration:
ga_mutation_rate = 0.1 # 10% mutation rate
ga_crossover_rate = 0.6 # Lower crossover for more mutation impact- Start Small: Use smaller values for initial testing and parameter exploration
- Balance Trade-offs: Population size vs. generation count vs. simulation steps
- Monitor Results: Watch fitness plots to understand if parameters need adjustment
- Computational Cost: Remember that total evaluations = population_size × num_generations × simulation_steps
- Docker Desktop (Windows/macOS) or Docker Engine (Linux)
- Git installed
- 8GB+ RAM recommended for larger populations
- 2GB+ disk space for Docker image and results
-
Clone the Repository:
git clone https://github.com/lesprgm/Conways-Life-AI-Engine.git cd Conways-Life-AI-Engine -
Create Host Results Directory:
mkdir ga_results_host
-
Build Docker Image:
docker build --no-cache -t game-of-life-ga .
If you prefer running without Docker:
-
Install Python Dependencies:
pip install -r requirements.txt
-
Create Results Directory:
mkdir ga_results
-
Run Directly:
python main.py
For macOS/Linux:
docker run -it --rm -v $(pwd)/ga_results_host:/app/ga_results game-of-life-gaFor Windows (PowerShell):
docker run -it --rm -v ${PWD}/ga_results_host:/app/ga_results game-of-life-gaFor Windows (Command Prompt):
docker run -it --rm -v %cd%/ga_results_host:/app/ga_results game-of-life-gaTo run in the background and save logs:
docker run -d --name gol-ga -v $(pwd)/ga_results_host:/app/ga_results game-of-life-ga > evolution.log 2>&1Check progress:
docker logs -f gol-gaFor development or debugging:
docker run -it --rm -v $(pwd)/ga_results_host:/app/ga_results --entrypoint /bin/bash game-of-life-gaIf you installed dependencies locally:
python main.pyExecution time varies significantly based on parameters:
| Configuration | Population | Generations | Approx. Time |
|---|---|---|---|
| Quick Test | 50 | 100 | 5-10 minutes |
| Standard | 200 | 1000 | 1-2 hours |
| Deep Search | 500 | 2000 | 10-16 hours |
- Generation Progress: Shows current generation number and total
- Fitness Updates: Reports when a better pattern is discovered
- Final Summary: Best fitness achieved and total generations completed
The application creates three types of output files in ga_results_host/:
Format: evolved_pattern_YYYYMMDD-HHMMSS.txt
1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0
0 1 0 1 0 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0
1 1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 1
- Content: The best evolved pattern as a space-separated grid
- Usage: Can be loaded and visualized in other Game of Life simulators
- Format: 1 = alive cell, 0 = dead cell
Format: evolved_pattern_YYYYMMDD-HHMMSS.npy
- Content: Binary format of the pattern for Python analysis
- Usage: Load with
numpy.load()for programmatic analysis - Advantage: Preserves exact data types and array structure
Format: fitness_history_YYYYMMDD-HHMMSS.csv
Generation,Fitness
1,23
2,23
3,45
4,45
5,67
- Content: Best fitness score for each generation
- Usage: Create custom plots, analyze convergence patterns
- Applications: Parameter tuning, performance analysis
| Fitness Range | Interpretation | Pattern Behavior |
|---|---|---|
| 0-50 | Poor survival | Dies quickly or becomes static early |
| 51-200 | Moderate success | Short-lived activity before stabilizing |
| 201-500 | Good patterns | Sustained activity with eventual cycles |
| 501-1000 | Excellent patterns | Long-lived with complex evolution |
| 1000+ | Outstanding | Near-maximum survival or gliders |
Note: Maximum possible fitness equals ga_simulation_steps (default: 2000)
After evolution completes, the application displays:
- Fitness Plot: Shows evolutionary progress over generations
- Pattern Animation: Live visualization of the best evolved pattern
- Final State: The pattern's behavior after evolution
Leslie Osei-Anane - Initial work and development
This project was created as an exploration of genetic algorithms applied to cellular automata, specifically Conway's Game of Life. It demonstrates the power of evolutionary computation in discovering complex, emergent patterns from simple rules.
- GitHub: @lesprgm
- Project Link: Conway's Life AI Engine
This project is licensed under the MIT License - see the LICENSE file for details.
