Skip to content

ntwiles/life-sim-python

Repository files navigation

Life Simulation with Evolutionary Neural Networks

A Python-based artificial life simulation where neural network "individuals" evolve through natural selection to navigate a 2D environment with beneficial heal zones and harmful radiation zones. Models are trained only through random mutation and natural selection.

Overview

This project explores evolutionary algorithms applied to neural networks in a spatial survival simulation. Individual agents must learn to:

  • Navigate toward healing zones for fitness benefits
  • Avoid moving radiation zones that cause damage
  • Survive and reproduce based on their accumulated healing score

Architecture

Neural Network Structure

  • Input Layer: 10 features including:
    • Previous movement direction (2)
    • Nearest heal zone direction and distance (3)
    • Nearest radiation zone direction and distance (3)
    • Radiation zone movement direction (2)
  • Hidden Layers: 12 → 8 neurons with ReLU activation
  • Output Layer: 5 movement actions (N/S/E/W/Stay) with softmax

Environment

  • Grid Size: 300×300 simulation space
  • Heal Zones: 7 stationary beneficial areas (radius 30)
  • Radiation Zones: 6 moving harmful areas (radius 25)
  • Population: 120 individuals per generation
  • Simulation Length: 300 steps per generation

Configuration

Key parameters in config.py:

NUM_INDIVS = 120          # Population size
MUTATION_RATE = 0.001      # 1% of weights mutated per generation
MUTATION_MAGNITUDE = 0.001 # Standard deviation of weight changes
GATE_DISABLE_RATE = 0.003 # Probability of connection gating
SELECTION_RATE = 1/3      # Top 33% selected for breeding

Selection Rate Constraints

The SELECTION_RATE must satisfy mathematical constraints to ensure proper population replacement:

  • NUM_INDIVS must be evenly divisible by SELECTION_RATE
  • The inverse of SELECTION_RATE (offspring per parent) must be an integer
  • Valid examples: 1/2 (50%), 1/3 (33%), 1/4 (25%), 1/5 (20%)
  • Invalid examples: 0.4 (40%), 1/7 (14.3%), 0.15 (15%)

Evolution Mechanics

  1. Selection: Fitness-proportional selection based on times_healed score
  2. Breeding: Each selected parent produces 3 offspring
  3. Mutation: Gaussian noise applied to inherited weights
  4. Structural Variation: Random gating patterns applied each generation
  5. Survival: Pure replacement - no individuals survive between generations

Installation & Usage

# Clone repository
git clone <repository-url>
cd life-sim-python

# Install dependencies and register the CLI
pip install -e .

# Run
life-sim

Selecting a project launches both the trainer and the visualizer simultaneously.

Project Structure

├── pyproject.toml
└── src/
    ├── cli/                      # Entry point (life-sim command)
    ├── trainer/
    │   ├── core/                 # Application and project management
    │   ├── model/                # Neural network model and mutations
    │   ├── simulation/           # Population, individuals, zones
    │   ├── strategies/           # Evolutionary and reinforcement strategies
    │   └── rendering/            # Pyglet renderer
    ├── visualizer/               # Matplotlib fitness visualizer
    └── shared/
        ├── config.py             # Simulation parameters
        ├── services/             # Persistence (projects, individuals)
        └── types/                # Shared data types

Future Enhancements

  • Gate inheritance across generations for structural evolution
    • This will require a mechanism for opening gates; they're currently created randomly.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages