• Stars
    star
    2,798
  • Rank 15,723 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🦄 An Artificial Inteligence to teach Google's Dinosaur to jump cactus

IAMDinosaur

IAMDinosaur

A simple artificial intelligence to teach Google Chrome's offline dinosaur to jump cactus, using Neural Networks and a simple Genetic Algorithm.

Watch this video to see it in action, and learn how it works: Artificial Intelligence with Google's Dinosaur

Installation

  1. Install Node.js on your computer.

  2. Clone/download this folder to your computer.

  3. run npm install within this folder

  4. Open Chrome's dinosaur game and put aside the terminal (It MUST be on the same screen) (Tip: go to developer tools, and under network, set to offline )

  5. run node index within this folder. If the game was located, it will move the cursor of the mouse to the origin of the floor of the dino. Press s key in the terminal to start learning.

How does it work

We have 3 different inputs read from the pixels of the screen:

  1. Distance from the next cactus
  2. Length of the next cactus
  3. Speed of the current cactus

We have also, one output with 3 possible states:

  1. output < 0.45: Press DOWN key
  2. output > 0.55: Press UP key
  3. default: Release both keys

Genetic Algorithm

Each Generation consists of 12 neural networks (Genomes).

Each genome is tested with the game, by constantly mapping the read inputs from the game to the inputs of the neural network, and by getting the output/activation from the network and applying to the keys of the keyboard.

While testing each genome, we keep track of it's "fitness" by counting jumped cactus in the game.

When an entire generation is completed, we remove the worst genomes until achieving N genomes. With those N genomes, we then select two randomly, and cross-over their values/configurations. After that, we apply random mutations in the values/configurations of the Neural Network, creating a new genome.

We do the cross-over/mutation until we get 12 genomes again, and repeat it constantly.

Implementation

All the implementation was done using Node.js, with Synaptic (Neural Network library), and RobotJs (a library to read pixels and simulate key presses).

There are a few files in the project:

  • index.js: It tight all things together.

  • Scanner.js: Basic abstraction layer above RobotJs library that reads the screen like ray tracing. Also have some utilities functions.

  • UI.js: Global scope for the UI management. It initializes and also updates the screen on changes.

  • GameManipulator.js: Has all the necessary code to read sensors, and apply outputs to the game. Is also responsible for computing points, getting the game state and triggering callbacks/listeners to real implementation.

  • Learner.js: It is the core implementation of the Genetic Algorithm. This is where "magic" happens, by running generations, doing "natural" selection, cross-over, mutation...

How to: Load a genome

  1. Make sure Genome is inside genomes folder with a .json extension
  2. Run the program
  3. Click the list in the terminal
  4. Navigate up/down to the wanted file
  5. Press enter (then, to start, press s)

Some shortcuts

  1. Run the program
  2. Press o to save the generation
  3. Press ´escape´, ´q´ or C-c to finish the process

Be aware of a game bug

The dino game has a anoying bug: It starts to "drift" to the right with time making the dino to be wrong offseted from the origin of the game. That, makes the program to read the dino as a cactus, since it is the same color.

You can fix that by continuously refreshing the page, or, by pasting this code inside the console in the element inspector:

// Make sure the dino does not drift to the right
setInterval(function (){Runner.instance_.tRex.xPos = 21}, 2000)

Development guidelines

Please, follow the Node.js style guide from Felix. It is not complex, and has a great simple pattern for things.

Credits

More Repositories

1

Is-Now-Illegal

🚫 A NERD protest against Trump's Immigration ban
JavaScript
1,376
star
2

node-draftlog

📜 Create mutable log lines into the terminal, and give life to your logs!
JavaScript
1,236
star
3

ArduinoThread

⏳ A simple way to run Threads on Arduino
C++
931
star
4

LinkedList

🔗 A fully implemented LinkedList made to work with general Microcontrollers and Arduino projects
C++
334
star
5

DueTimer

⏳ Timer Library fully implemented for Arduino DUE
C++
207
star
6

unicute

💙 Cute Unicode symbols. Make the terminal GREAT AGAIN
56
star
7

ArduinoSensors

A Library of Libraries of Sensors. Infrared, Ultrasonic, Compass and many others, ready to work with ArduinoThread and fully object oriented
C++
53
star
8

Gaussian

Library that makes Gaussian work easy to use with C++ and Arduino
C++
51
star
9

BigBang-js

Recreate the BigBang with JavaScript
JavaScript
47
star
10

Robot-Soccer-Simulator

⚽️ An Open-Source Soccer simulator for virtual robots tournaments
Java
35
star
11

EasyFly

A simple and just another Drone Firmware
C++
24
star
12

3DLiveView-ABB

Application using QT and native OpenGL, with a three based rendering engine to show a 3D ABB robot in sync with an arduino
C++
19
star
13

node-require-smart

The smart way of requiring multiple files in NodeJS
JavaScript
18
star
14

ABBNator

ABB Robots, playing TicTacToe with NodeJS in it's free time
JavaScript
17
star
15

ArdUI

A generic, simple, easy to use User Interface for Arduino
C++
14
star
16

Robot-Rescue-2013

RoboCup Junior Rescue B Robot sharing (CAD, Software, Electronics, materials and all stuff)
C
13
star
17

Robot-Soccer-2013

RoboCup Junior Soccer Robot sharing (CAD, Software, Electronics, materials and all stuff)
C
12
star
18

UTFT

A generic LCD Library for Arduino
C
11
star
19

Nao-Taokei

🚫Um app NERD para gerar gifs Huehue BR
JavaScript
6
star
20

NodePlate

Node boilerplate for servers
JavaScript
6
star
21

Robot-Rescue-2012

RoboCup Junior Rescue B Robot sharing (CAD, Software, Electronics, materials and all stuff)
Java
6
star
22

Primo

Primo version using Stepper Motors and custom Software
C++
5
star
23

oscilodrawer

Osciloscope Image drawer with Arduino and Nodejs MQTT
C++
5
star
24

pcb-libraries

Place to keep and share special Altium, Eagle and Proteus components that we create and share with others.
5
star
25

docker-node-python-opencv

A Dockerfile that installs Node.js, Python and OpenCV
4
star
26

tournamenter-obr

Extensão para o Tournamenter que permite pontuar pelo tablet em competições da OBR
JavaScript
4
star
27

easy-admin

Simple Admin UI with CRUD for Node.js
JavaScript
3
star
28

geddy-rest

REST Api made easy for Geddy
JavaScript
3
star
29

Robot-VerySmall-2015

Very Small Robot for 2015 CBR competition
Python
2
star
30

projetohangar

HTML
2
star
31

i23

An innovative way of connecting and communicating sensors with microcontrollers
2
star
32

node-ssl-vision

RoboCup SSL Vision Client library for Node.JS
JavaScript
2
star
33

Robot-Rescue-2011

Emerotecos Team's robot of Rescue A (OBR) 2011
HTML
1
star
34

Phyto-Gear

A simple Arduino robot that communicates with Facebook to notify that it's happy, sad, with or without light...
Arduino
1
star
35

TemPeixeNoRU

App de leitura de cardápio do RU da UFABC
1
star
36

Chenn

An real time graph as learning object for schools
JavaScript
1
star
37

rifa-generator

Projeto antigo de um Gerador de PDF's de Rifas
JavaScript
1
star
38

tournamenter-wro

Tournamenter Plugin for WRO - World Robotics Olympiad
JavaScript
1
star
39

robocup2014

Robocup 2014 Administration panel for leagues
JavaScript
1
star
40

NoFlow

A (really-simple) flow based programming framework
JavaScript
1
star