---
title: "Dino AI"
year: "2019"
motto: "Dino AI Game"
description: "An AI that learns to play the Chrome dino game. 300 tiny neural networks (perceptrons) run at once, and a genetic algorithm keeps the best players each round."
stack: ["javascript"]
website: "https://github.com/RolandoAndrade/ia-dino"
url: "https://rolandoandrade.me/projects/dino-ai/"
author: "Rolando Andrade"
---

# Dino AI

> Dino AI Game

An AI that learns to play the Chrome dino game. 300 tiny neural networks (perceptrons) run at once, and a genetic algorithm keeps the best players each round.

Year: 2019. Stack: javascript. Website: https://github.com/RolandoAndrade/ia-dino

Repositories:

- [github](https://github.com/RolandoAndrade/ia-dino)
- [gitlab](https://gitlab.com/RolandoAndrade/ia-dino)

Open Dino AI in your browser and 300 dinosaurs start running at once. Most of them die on the first cactus. A few get lucky. Their "brains" pass to the next generation, and within minutes the population learns to play the game flawlessly.

Chrome's dinosaur game looks simple. A cactus comes closer, you press space and the dino jumps. Timing is everything. Jump too early and you land on the obstacle. Jump too late and you crash. As the game speeds up, human reflexes struggle to keep up.

I did not want to write exact rules for when to jump. I wanted a neural network to find them by playing thousands of games and getting better over generations. This experiment combines two classic AI techniques: perceptrons and genetic algorithms.

## 300 dinosaurs at once

Dino AI copies Chrome's offline dinosaur game with one change. Instead of one player, 300 dinosaurs run at the same time, each controlled by AI. Each one has its own neural network brain that decides when to jump, duck or do nothing.

When an obstacle comes closer, each brain gets two inputs. One is the horizontal distance to the nearest obstacle. The other is the vertical distance. The brain passes these inputs through weighted connections and picks one of three actions: jump, duck or keep running.

Every dinosaur that hits an obstacle dies. The survivors pass their brains to the next generation. Over time, the population finds better and better strategies. You can watch it happen in real time.

[https://lab.rolandoandrade.me/ia-dino/](https://lab.rolandoandrade.me/ia-dino/)

## The smallest possible brain

Each dinosaur has a perceptron, the simplest possible neural network. A perceptron takes its inputs, multiplies each one by a weight, adds the results and applies an activation function.

In this game, the perceptron has these parts:

- **Two inputs**, the distance to the obstacle on the X axis and the Y axis
- **Two weights**, learned values that decide how much each input counts
- **One bias**, an offset that moves the point where the decision changes
- **One output**, which picks the action (jump, duck or nothing)

The weights start random. Most random weights produce terrible behavior. The genetic algorithm fixes that.

## Natural selection in code

A genetic algorithm copies natural selection. It works in seven steps:

1. **Population.** 300 dinosaurs appear with random brain weights.
2. **Fitness.** Each dinosaur's score is the number of frames it survived.
3. **Selection.** The top 10% of performers survive.
4. **Reproduction.** Survivors pair up and produce children.
5. **Crossover.** Each child gets weights at random from both parents.
6. **Mutation.** With a 50% probability, weights change by a small amount.
7. **Repeat.** A new generation begins.

This cycle never stops. Bad strategies die out and good strategies spread. The 50% mutation rate keeps the population trying new things.

## Duck, run or jump

To decide what to do, a dinosaur's brain computes a weighted sum of the inputs. The result goes through an activation function that squeezes it into a usable range.

A transfer function then turns that smooth number into one of three actions:

- Output below a threshold means duck
- Output in the middle range means do nothing
- Output above a threshold means jump

The weights decide which obstacles trigger which response. A dinosaur might learn that close obstacles on the ground need a jump, while flying obstacles need a duck.

## Inside the browser game

The game runs in the browser on HTML5 Canvas. It needs no server and has no dependencies.

The code has five main parts:

- **Game loop.** Runs 60 times per second and updates the whole game state.
- **Player entities.** 300 dinosaur instances, each with its own brain.
- **Obstacle spawner.** Creates cacti at random intervals.
- **Collision detection.** Checks each dinosaur against the obstacles.
- **Generation manager.** Handles selection and reproduction.

Every frame, each living dinosaur asks its brain what to do with the current obstacle position. The brain returns an action, and the dinosaur does it. Physics updates the positions. Collision checks find who died.

When every dinosaur has died, the generation ends. The top 30 (10% of 300) become parents. They produce 270 children to fill the population again. Their weights get copied, crossed and mutated.

## Four numbers that matter

The algorithm's behavior depends on a few numbers:

- **Population: 300.** Large enough for variety, small enough to run smoothly.
- **Survival rate: 10%.** Keeps only the best performers.
- **Mutation rate: 50%.** A high rate pushes the population to explore.
- **Step size: 0.01.** Small weight changes prevent wild swings.

I found these values by experimenting. Lower mutation rates made the population stop improving. Bigger step sizes made it unstable. The current settings balance exploring and staying stable.

## From hopeless to precise

First generation dinosaurs are hopeless. They jump at random, or not at all. Most die within seconds.

By generation 5, some dinosaurs have learned to jump. They clear the early obstacles but fail when the speed goes up.

By generation 20, the population handles normal play. Deaths become rare in the first minute.

By generation 50, the dinosaurs play almost perfectly. They jump with precise timing, duck under flying obstacles and adapt as the speed increases.

You can see the learning happen. Skill appears out of random noise.

## Evolution only needs a score

A perceptron is only multiplication and addition. A genetic algorithm is only selection and randomness. Together, they produce players that learn a game.

Evolution does not need to understand the problem. It only needs a way to measure fitness. Good solutions survive and bad ones die. With enough generations, surprisingly smart behavior appears.

I built everything from scratch, without machine learning libraries, so I had to understand each piece. Crossover helps because it combines traits that worked. Mutation matters because it brings in new possibilities. Selection works because it pulls the population toward good solutions.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
