~/projects/neural-network

← all projects

Neural Network from Scratch

A neural network written from scratch in C#, with no ML libraries, and trained with a genetic algorithm to control swarm robots in a Unity simulation.

  • C#
  • Unity
  • neural networks
  • genetic algorithms
0
ML libraries
5-6-2
network shape
50
robots / generation
200+
generations

Overview

Each robot in the swarm is controlled by its own small neural network. The network reads the robot's sensors and outputs motor speeds. Instead of backpropagation, the networks are trained with a genetic algorithm: the best performers each generation are copied and mutated.

input sensor 1 sensor 2 sensor 3 sensor 4 heading hidden output left motor right motor
Sensor readings go in, motor speeds come out. Every line is a weight in a hand-written matrix.

Training with a genetic algorithm

Every generation, each robot is scored on how well it does its task. The top robots survive, and their weights and biases are copied and randomly mutated to make the next generation.

spawn 50 robots simulate run in Unity score fitness evolve select, mutate repeat for the next generation
The training loop repeats until the swarm's behavior stops improving.

Key features

  • Custom matrix math and forward propagation, no ML libraries
  • Genetic algorithm mutates weights and biases each generation
  • Fitness function rewards reaching the goal and penalizes collisions
  • Simulated in Unity so a whole generation runs in parallel

Results

Early generations wander randomly. After a few hundred generations, the robots learn to move toward the goal and avoid each other.