- C++ 99.9%
- Shell 0.1%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| inc | ||
| Food.cpp | ||
| GeneticAlgorithm.cpp | ||
| install.sh | ||
| LICENSE | ||
| main.cpp | ||
| NeuralNetwork.cpp | ||
| Organism.cpp | ||
| README.md | ||
| Renderer.cpp | ||
| Simulation.cpp | ||
evolving-neural-networks-cpp
A simple implementation of Neural Network Evolution using a Genetic Algorithm written in C++.
This project demonstrates how simple organisms can evolve their own neural networks through a process inspired by natural selection and biological evolution.
Instead of manually training a neural network using gradient descent and backpropagation, this project evolves the network weights by creating populations of organisms, selecting the best performers, mixing their neural networks, and applying random mutations.
The goal of each organism is simple:
Find and eat as much food as possible.
The organisms start with random brains, but after many generations they gradually learn better navigation strategies.
Concept
Each organism contains:
- A position in the world
- A direction (heading)
- A velocity
- A neural network brain
- A fitness score
The neural network controls:
- Turning direction
- Acceleration / movement speed
The only information given to the brain is:
Direction to nearest food
The organism must learn how to transform this input into movement decisions.
Neural Network
Each organism uses a small fully connected neural network:
Input Layer
|
|
Hidden Layer
|
|
Output Layer
Example:
Food Direction
|
v
+-------+
| Input |
+-------+
|
v
+--------------+
| Hidden Nodes |
+--------------+
|
v
+----------------+
| Output Neurons |
+----------------+
/ \
/ \
Turn Left Change Speed
The network uses the tanh activation function:
tanh(x)
because inputs and outputs are normalized between:
[-1 , +1]
Genetic Algorithm
The neural network weights are optimized using a Genetic Algorithm.
Each organism represents an individual in a population.
The algorithm follows natural evolution:
Random Population
|
v
Evaluate Fitness
|
v
Select Best Organisms
|
v
Crossover Neural Networks
|
v
Mutation
|
v
New Generation
|
└───────────────┐
|
v
Repeat
After many generations:
Random brains
|
|
v
Better navigation strategies
|
|
v
Organisms that efficiently find food
Fitness Function
Fitness represents survival success.
Every time an organism reaches food:
fitness += food energy
Higher fitness means:
More food collected = Better organism
The best organisms are allowed to reproduce.
Evolution Process
1. Population
The simulation begins with many organisms.
Each organism receives random:
Position
Heading
Velocity
Neural Network Weights
Example:
Organism #1
Brain:
w1 = 0.32
w2 = -0.71
w3 = 0.54
Organism #2
Brain:
w1 = -0.12
w2 = 0.91
w3 = 0.08
Most organisms initially behave randomly.
2. Selection
After a simulation period:
The organisms are ranked by fitness.
The strongest survive.
Example:
Best:
Organism A Fitness: 42
Organism B Fitness: 37
Organism C Fitness: 31
Removed:
Organism D Fitness: 2
Organism E Fitness: 0
3. Crossover
Two successful parents create children.
Their neural network weights are blended:
child = (parent1 * alpha) + (parent2 * (1-alpha))
Example:
Parent A:
0.8
Parent B:
0.2
Child:
0.5
The child receives traits from both parents.
4. Mutation
Small random changes are introduced.
Example:
Before:
weight = 0.50
After mutation:
weight = 0.54
Mutation prevents the population from becoming stuck.
Simulation
The world contains:
Organisms
|
|
v
Food particles
Every simulation step:
- Detect food collisions
- Update fitness
- Find nearest food
- Calculate food direction
- Run neural network
- Update movement
The loop repeats thousands of times.
Project Structure
evolving-neural-networks-cpp/
├── main.cpp
├── Simulation.cpp
├── Organism.cpp
├── NeuralNetwork.cpp
├── Food.cpp
├── GeneticAlgorithm.cpp
├── Renderer.cpp
├── inc/
| ├── Simulation.h
| ├── Organism.h
│ ├── Vector2.h
│ ├── Random.h
| ├── NeuralNetwork.h
| ├── Food.h
| ├── GeneticAlgorithm.h
| ├── Renderer.h
│ └── other headers
├── CMakeLists.txt
└── README.md
Requirements
Software
- C++17 compiler
- GCC / g++
- CMake
- SFML 2.x
Installing Dependencies
Ubuntu:
sudo apt update
sudo apt install build-essential cmake libsfml-dev
Check compiler:
g++ --version
Compilation
Using g++
From the project folder:
g++ main.cpp Simulation.cpp Organism.cpp NeuralNetwork.cpp Food.cpp GeneticAlgorithm.cpp Renderer.cpp -I./inc -o EvolvingAI -lsfml-graphics -lsfml-window -lsfml-system -std=c++17
After successful compilation:
EvolvingAI
will be created.
Run
Linux:
./EvolvingAI
A simulation window will appear.
You should see:
- White triangles = organisms
- Green circles = food
- Organisms searching for food
Using CMake
Create build directory:
mkdir build
cd build
Generate build files:
cmake ..
Compile:
make -j$(nproc)
Run:
./EvolvingAI
Example Evolution
Generation 0:
Organisms move randomly.
Food collection:
Low
After many generations:
Organisms learn:
- turn toward food
- control speed
- avoid wasting movement
Food collection:
High
The population gradually evolves better neural networks.
Files Explained
NeuralNetwork
Responsible for:
- Storing weights
- Forward propagation
- Producing movement decisions
Organism
Contains:
- Neural network brain
- Position
- Velocity
- Heading
- Fitness
Controls movement behavior.
Food
Contains:
- Position
- Energy value
Respawns after being eaten.
GeneticAlgorithm
Responsible for evolution:
- Selection
- Elitism
- Crossover
- Mutation
Simulation
Runs the virtual environment:
- Updates organisms
- Checks collisions
- Calculates fitness
- Advances generations
Renderer
Visualizes the simulation using SFML.
Learning Objectives
This project demonstrates:
- Genetic Algorithms
- Evolutionary Computing
- Artificial Neural Networks
- Neural Network Forward Propagation
- Fitness Functions
- Elitism
- Crossover
- Mutation
- Simulation Design
- Object-Oriented C++
Why Use Genetic Algorithms?
Advantages:
- Does not require gradients
- Works with complex environments
- Can optimize non-differentiable problems
- Searches many solutions simultaneously
- Naturally supports exploration
Limitations
- Slower than gradient-based learning
- Requires many evaluations
- Results depend on parameters
- No guarantee of the perfect solution
Possible Improvements
Ideas to extend this project:
- Add obstacles
- Add multiple food types
- Add energy consumption
- Add reproduction
- Add predator organisms
- Add better neural networks
- Add saving/loading evolved brains
- Add parallel simulation
- Add visualization of neural networks
Applications
Genetic algorithms and evolving neural networks are used in:
- Robotics
- Autonomous agents
- Game AI
- Neural Architecture Search
- Engineering optimization
- Scheduling
- Control systems
- Evolutionary robotics
Educational Goal
This project is not designed to compete with modern deep learning frameworks.
Instead, it shows the fundamental idea behind evolutionary intelligence:
Simple rules, repeated over many generations, can create surprisingly intelligent behavior.
License
MIT License