A sandbox for exploring natural selection with multi-agent reinforcement learning and neuroevolution in a 2D world.
- Simulates agents moving and interacting in a 2D environment
- Uses evolutionary algorithms and reinforcement learning policies
- Supports configurable agent behavior, physics, and environment rules
- Collects results suitable for experiments and model training
src/natural_selection_2d/– core packagesrc/natural_selection_2d/env/– world, entities, and physicssrc/natural_selection_2d/algorithms/– evolution logic, contains the genetic algorithmsrc/natural_selection_2d/models/– policy definitions, includes the PyTorch AgentBrainsrc/natural_selection_2d/viz/– rendering utilitiesscripts/– experiment and simulation runnersexperiments/– analysis notebooks and result artifacts
Natural survival, agent collisions and discrete food spawns are essentially non-differentiable step functions. And standard gradient descent requires differentiable loss functions that's why Evolutionary strategies are a good fit for such usecases. This was demonstrated by OpenAI in 2017 in their paper "Evolution strategies as a scalable alternative to Reinforcement Learning" where they showed that ES can match PPO on complex continuous control benchmarks.
A simple feedforward MLP is implemented for the Agent's brain whose parameters are selected based on the fitness scores in the genetic algorithm.
-
Install the package in editable mode:
pip install -e . -
Run a simulation or experiment from
scripts/:python scripts/train_neuroevolution.py # to simulate the actual evolutionary process coupled with visuals python scripts/run_experiment.py # to experiment with key factors such as mutation rate, metabolism and population size
- Designed for experimentation rather than production use
- Modify
config.pyto tune environment and training parameters - Use
experiments/experiment_analysis.ipynbto inspect result data