Evolutionary Creatures
Jun – Aug 2024 · Team of two · C++ simulation · by Abhiyan Khanal
A simulation written in C++ where virtual creatures with small neural-network brains reproduce, mutate and compete. Leave it running and new species with different brain wiring appear, get protected for a while so they can improve, and either take over or die out.
Features
- Genetic algorithm with selection, mutation and reproduction
- Neural-network brains whose connections evolve
- Species tracking, with protection for new species
- Two challenges: reach the sides of the map, or chase moving food
- Live visualisation of the population
How the creatures think
Each creature has a small neural network with four inputs, four hidden neurons and two outputs, using tanh activation. Instead of all 24 possible connections being active, each brain starts with only six randomly chosen ones. Mutation changes the connection weights and, sometimes, which connections exist, so brains can grow different shapes over generations.
Evolution and species
A population of 500 creatures lives on a 100 by 100 grid for 400 steps per generation, for 100 generations, with a 15% mutation rate. Creatures are scored by a fitness function, the best are kept as parents and the rest of the next generation is bred from them. When a mutation changes a brain's wiring, the creature is labelled a new species and shielded from selection for a few generations, so a promising design is not wiped out before it has had time to improve.
Two modes
- Right-Left: creatures are rewarded for reaching the right side of the map at one age and the left side at a later age.
- Food Chaser: creatures must follow and reach a randomly moving food block.
- The code is written with C++ templates and inheritance: a base Individual class with RightLeft and FoodChaser subclasses, and a GeneticAlgorithm class templated over the creature type with a fitness function for each.
Visualisation
An SDL2 window renders the population every ten generations and replays the best creature at the end, along with its weights, active connections and movement history in the console.
Tech stack
More projects by Abhiyan Khanal
- Gantabya: Started at KU HackFest 2024 · Team project · Full stack
- Janakpur Fish House: Client project · Web · Live at janakpurfishhouse.com
- CogniCare: Nov 2024 · Team of five · Mobile + backend
- कानुन साथी (Kanun Sathi): Jan 2026 · Team of four · AI / full stack
- Hamro Bus: Oct 2024 · Team project · Web, built in a few days
- DDA Drug Registration Portal: Jan 2026, revamped Nov 2026 · Team of four · Full stack