← all projects
Procedural Generation · April 2023 · 5 min read

Pocket Planet

A 100×100 world carved from Perlin noise, then colonized by simulated plants that mutate, compete, and converge on the terrain they're fittest for.

PythonNumPyPerlin NoiseEvolutionary Simulation

The Vision: What if evolution could paint a world? This simulation creates a 100×100 landscape from Perlin noise, then seeds it with digital organisms that compete, mutate, and adapt until the planet blooms with life perfectly matched to every biome.

The Challenge: Design an evolutionary system complex enough to produce realistic ecological patterns, yet simple enough to run in real-time and yield interpretable results. Balance between random exploration (mutation) and local optimization (selection) to achieve global adaptation.

terrainH(x,y) = Σₖ 2⁻ᵏ · Pk(x,y),  smoothstep s(t) = 6t⁵ − 15t⁴ + 10t³ mutationd′ = normalize(clip(d + u, 0, 1)),  u ∼ Uniform(−σ, σ)⁵ offspringN ∼ Binomial(nmax, f) dispersal(x′, y′) ∼ 𝒩((x, y), σ²I) competitionP(i wins) ∝ exp(β · fi)

Terrain Generation & Ecosystem Design

The foundation is multi-octave Perlin noise creating realistic geographic features. Unlike simple random height maps, Perlin noise produces coherent mountain ranges, river valleys, and coastal plains that feel organic.

Technical Implementation: Each terrain type (mountain, forest, plains, water, desert) gets a 5D DNA vector representing optimal organism traits. The fitness function creates soft fitness gradients rather than hard biome boundaries — organisms can survive in adjacent biomes but thrive in their optimal zone.

Key Design Decision: Rather than hand-coding species, the system discovers them through emergent evolution. Each organism’s 5-element “genome” encodes its ecological niche preferences. Over time, the population naturally segregates into distinct species adapted to different terrain types.

The Evolutionary Algorithm

This isn’t standard genetic programming — it’s a spatial evolutionary simulation with several sophisticated features:

1. Fitness Landscape Design

Each cell has an optimal DNA vector based on terrain. Organism fitness = 1 - ||organism_dna - optimal_dna||, creating smooth fitness gradients that reward specialization while allowing gradual adaptation.

2. Spatially-Constrained Reproduction

Offspring disperse via Gaussian random walk from parent location. This creates geographic population structure — coastal organisms rarely mix with mountain species, driving speciation.

3. Boltzmann Tournament Selection

Competition probability follows P(win) ∝ exp(β × fitness). This temperature parameter β controls selection pressure: high β = harsh selection, low β = drift-dominated evolution.

Critical Engineering Challenge: Balancing mutation rates. Too low → populations get stuck in local optima. Too high → no stable adaptation. Solution: adaptive mutation that decreases as populations converge.

Colonization Dynamics

The simulation starts with empty terrain and coastal seeding, mimicking real biogeographic colonization:

Wave 1 (Steps 1-20): Coastal colonization by generalist organisms with mediocre fitness everywhere Wave 2 (Steps 20-50): Inland expansion as populations grow and disperse
Wave 3 (Steps 50-100): Specialization as local selection pressure creates distinct biome-adapted populations

The Beautiful Moment: Around step 60, you can see distinct species boundaries form. Mountain organisms (dark purple) stop trying to colonize coastal plains. Forest specialists (green) develop stable populations. The system self-organizes into an ecosystem.

Convergence Analysis

The simulation solves an implicit optimization problem: maximize total ecosystem fitness across all terrain types simultaneously.

Left panel: Initial random seeding with poor terrain matching Middle panel: 80 steps later showing specialized populations
Right panel: The theoretical optimal solution the system converges toward

Mathematical Insight: This is effectively a distributed optimization where each organism acts as an independent agent optimizing its local fitness, but the collective behavior solves a global problem. Similar to particle swarm optimization but with genetic inheritance.

Convergence Metrics:

  • Coverage: Follows logistic growth as organisms fill available niches
  • Average Fitness: Climbs as populations adapt, plateaus at convergence
  • Biome Specialization: Measured by genetic distance between populations, increases over time

Performance Analysis: Reaches 95% of theoretical maximum fitness in ~100 iterations. Remaining 5% represents fundamental trade-offs (e.g., edge organisms that can’t perfectly match any single biome).

Technical Architecture & Performance

Built with careful attention to computational efficiency:

class Ecosystem:
    # Vectorized fitness calculations using NumPy broadcasting
    # Spatial indexing for O(1) neighbor lookups  
    # Efficient genome representation as float32 arrays
    
class TerrainGenerator:
    # Cached Perlin noise with octave pre-computation
    # Biome classification via K-means clustering
    
class EvolutionEngine:
    # Tournament selection with batch processing
    # Mutation pipeline with GPU acceleration potential

Optimization Highlights:

  • Vectorized Operations: All fitness calculations use NumPy broadcasting → 50x speedup
  • Spatial Hashing: O(1) neighbor queries for competition resolution
  • Memory Management: Pre-allocated arrays, object pooling for 60 FPS performance

Applications & Biological Insights

This system demonstrates several important evolutionary principles:

1. Geographic Speciation

Spatial separation drives genetic divergence even without explicit reproductive barriers. Real-world parallel: Darwin’s finches adapting to different Galápagos islands.

2. Fitness Landscapes

The relationship between smooth fitness gradients and evolutionary dynamics. Steep gradients → rapid local adaptation. Shallow gradients → genetic drift and exploration.

3. Founder Effects

Early colonizers have disproportionate influence on final population genetics, even if they’re not optimal. This creates path-dependent evolution — different random seeds lead to different final ecosystems.

Emergent Behaviors Discovered:

  • Edge species: Organisms that thrive at biome boundaries, exhibiting “generalist” strategies
  • Adaptive radiations: Single coastal population rapidly diversifying as it spreads inland
  • Extinction-recolonization cycles: Mountain populations occasionally crash and get recolonized from valleys

Future Extensions & Research Questions

The framework opens up fascinating evolutionary experiments:

Climate Change Simulation: Gradually shift terrain types and observe adaptation vs extinction Species Interactions: Add predator-prey dynamics or symbiotic relationships Migration Corridors: How do geographic barriers affect gene flow and speciation?

Technical Improvements:

  • Multi-threading: Parallelize evolution across spatial regions
  • Advanced Genetics: Implement chromosomes, recombination, epistatic interactions
  • Machine Learning Integration: Use learned fitness functions from real ecological data

This project showcases how simple rules can generate complex, realistic patterns — a key principle in computational biology, game AI, and any system where agents interact locally to solve global optimization problems.

The deeper lesson: evolutionary algorithms work best when they mirror the structure of the problem domain. Spatial evolution for spatial problems, temporal evolution for temporal problems. Understanding this match between algorithm and domain is crucial for ML engineers designing optimization systems.

#procedural#simulation#evolution

Related projects