An Evolutionary AI Simulation of the Iterated Prisoner’s Dilemma Across Complex Networks
EvoDilemma simulates how artificial agents evolve strategic behavior over generations in the classic Iterated Prisoner’s Dilemma (IPD). Using neuroevolution and small-world network topologies, this project explores the emergence of cooperation, deception, and memory-based adaptation in agent populations.
EvoDilemma models 600+ neural network agents playing iterative IPD matches across dynamically generated Watts-Strogatz small-world networks. Each agent has a simple feedforward network that maps memory (of its last three moves and its opponent’s) to future actions.
After each generation, agents evolve using a custom-built genetic algorithm featuring:
- Top-25% fitness-based selection
- 4:1 offspring generation ratio (to maintain a consistent agent count in each generation)
- Crossover breeding via random weight masking
- Stratified mutation rates (0.01 / 0.1 strength variants)
Fitness calculations exclude initial rounds to allow for neural memory bootstrapping, and entire match histories are logged for each agent across generations.
- Fitness-based selection and pruning
- Neuroevolution using PyTorch (no backpropagation, agents progressively get better through neurevolution, where agent fitness is calculated, and the top 25% get to further reproduce)
- Iterated Prisoner’s Dilemma tournaments
- Custom input encoding of 6 memory slots (3 agent, 3 opponent)
- Behavioral phenotyping (Cooperator / Defector tendencies)
- Agents placed on Watts-Strogatz networks (rewiring prob configurable)
- Multiple neighbor games per agent per generation
- Edge visualization reflects interaction dynamics
- Interactive match history tracking for each agent
- Dynamic network graph with edge highlighting
- Generation progression control with coroutine-based automation
- Phenotype overlays
- Unity frontend (C#) communicates with Flask backend (Python)
- API endpoints for match simulation, mutation, generation progression
- Agent history synced across client/server
| Component | Tech |
|---|---|
| Simulation | Unity (C#), REST API |
| Backend Logic | Python, Flask, PyTorch |
| AI Methods | Mutation Algorithms, 6-Input neural net |
How our agent network looks after a generation progression (Blue nodes are agents that have a tendency to split more often, with red agents being more likely to steal). This is generation 1.
How our agent network looks after a generation progression (Blue nodes are agents that have a tendency to split more often, with red agents being more likely to steal). This is generation 439.
How our agent network looks after an agent has been selected, showing agent match history against other connected agents.
How a generation of agents look after pruning(top 25% of agents with the highest fitness scores kept). These are the top ranked agents of generation 1.
How a generation of agents look after pruning(top 25% of agents with the highest fitness scores kept). These are the top ranked agents of generation 439.
- Python 3.10 or higher installed on your system
pippackage manager available
If you don't already have an established venv that holds all of your dependencies, you can initialize a new one:
python3 -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activateI WAS planning to dockerize this project, but for some reason, dockerization here was a pain, so you (the dear reader), will have to make do with installing the dependencies yourself.
pip install --upgrade pip
pip install -r python_processes/requirements.txtpython python_processes/python_server/server.pyAfter this is done(the port should be 8080), when the Unity simulation is ran, will automatically communicate with the server when needed (as it's set to communicate with 8080), no further setup required!