An evolutionary simulation where autonomous agents learn to navigate through obstacles using neural networks and genetic algorithms
About • Features • How to Build • How it Works • Acknowledgements
Rockets learning to navigate through obstacles using evolved neural networks
Neural Network Rockets is a simulation project where autonomous agents (rockets) learn to navigate through an obstacle course to reach a target. The project implements a Neural Network library from scratch (without using external AI frameworks like TensorFlow or PyTorch) and utilizes a Genetic Algorithm to evolve the population over generations.
The rockets use raycasting sensors (lasers) to "see" their surroundings and make decisions based on their neural network's output.
This project was created as a final assignment for the Programming 1 course.
- University: Silesian University of Technology
- Faculty: Faculty of Applied Mathematics
- Major: Computer Science (1st Year)
- Language: C++
- Graphics: SFML (Simple and Fast Multimedia Library)
- Build System: CMake
- Neural Network: Fully connected Multi-Layer Perceptron (MLP) built from scratch using matrix operations
- Genetic Algorithm: Implements Selection, Crossover, Mutation, and Elitism
- Raycasting: Custom collision detection sensors for the agents
The structure of the project and the implementation of the core Neural Network were inspired by the "Live Coding 2: C++ Neural Network" series by the YouTube channel devlogs.
This tutorial series was instrumental in understanding how to structure the matrix math and the architecture required to build a neural network from scratch in C++.
Channel: devlogs
- C++ Compiler (supporting C++17)
- CMake (Version 3.10 or higher)
- SFML Library (Must be installed on your system)
-
Clone the repository:
git clone https://github.com/Jachu7/Neural-Network-Rockets.git cd Neural-Network-Rockets -
Create a build directory:
mkdir build cd build -
Generate Makefiles using CMake:
cmake ..
-
Compile the project:
make
-
Run the application:
cd bin ./main
Note: If the bin folder is not created, try running
./NeuralNetworkRocketsdirectly in the build folder, depending on your CMake configuration.
The easiest way to build and run this project on Windows is using CLion (JetBrains) or Visual Studio, as they have built-in CMake support.
- Open CLion
- Select "Open" or "Open from VCS" and select the project folder
- CLion will automatically detect the
CMakeLists.txtfile and load the project structure - Wait for the indexing and CMake configuration to finish (you should see "CMake: Finish" in the bottom console)
- Click the green Run (Play) button in the top right corner
Note: Ensure that SFML is properly linked in your environment or installed via a package manager like vcpkg.
src/
├── main.cpp # Main simulation loop, SFML rendering
├── Rocket.h # Rocket agent: physics, sensors, fitness
├── GeneticAlgorithm.h # Evolution: selection, crossover, mutation
├── LaserReading.h # Laser sensor data structure
├── Utils.h # Math utilities (line intersection, random)
├── assets/
│ └── Roboto_Condensed-Medium.ttf # UI font
├── img/
│ ├── rakieta.png # Rocket sprite
│ └── ogien.png # Thrust flame sprite
└── siec/ # Neural Network Library
├── NeuralNetwork.h/cpp # Network topology & feedforward
├── Layer.h/cpp # Layer of neurons
├── Neuron.h/cpp # Single neuron with activation
├── Matrix.h/cpp # Matrix operations for weights
└── utils/
└── MultiplyMatrix.h/cpp # Matrix multiplication utility
- main.cpp: Main simulation loop and SFML rendering
- Rocket.h: Agent structure with physics, raycasting sensors, neural network brain, and fitness calculation
- GeneticAlgorithm.h: Implements evolution with elitism, tournament selection, crossover, and mutation
- LaserReading.h: Data structure for laser sensor readings
- Utils.h: Helper functions for line intersection detection and random number generation
- NeuralNetwork: Manages the topology of the network. Handles feedForward (passing data from input to output) and manages layers
- Layer: Represents a layer of neurons
- Neuron: A single unit that holds a value and an activation function (Softsign)
- Matrix: A custom math class to handle weights and matrix multiplication
- MultiplyMatrix: Utility class for matrix operations
100 rockets are spawned with random neural weights.
- Inputs (distances, velocity, angle to target) are fed into the Neural Network
- The Network outputs control signals (Thrust, Rotate Left, Rotate Right)
- If a rocket hits a wall, it dies
- If a rocket reaches the target, it succeeds
- Fitness is calculated based on: Checkpoints reached, distance to target, and completion time
- The best rockets are kept (Elitism)
- New rockets are created by mixing the "brains" (weights) of the best performers
- Small random changes are applied (Mutation) to discover new strategies
The cycle continues, and rockets get smarter every generation.
- Training Duration: On average, the population learns to navigate the course effectively within 15 to 50 generations
- Success Rate: A success rate of 30-60 rockets out of 100 reaching the target is perfectly acceptable during training. This level of performance demonstrates that the neural networks have learned viable strategies, and in real-world applications, such success rates would be sufficient for practical deployment
This project demonstrates how:
- Neural networks can be implemented from scratch
- Evolutionary algorithms can replace traditional training methods
- Complex behavior can emerge from simple rules
If you find this project interesting, feel free to ⭐ the repository!
Made with ❤️ as a Programming 1 final project
