{"id":15030104,"url":"https://github.com/ivanseidel/iamdinosaur","last_synced_at":"2025-05-15T10:05:38.054Z","repository":{"id":39673732,"uuid":"48660863","full_name":"ivanseidel/IAMDinosaur","owner":"ivanseidel","description":"🦄 An Artificial Inteligence to teach Google's Dinosaur to jump cactus","archived":false,"fork":false,"pushed_at":"2018-02-07T09:02:56.000Z","size":356,"stargazers_count":2812,"open_issues_count":42,"forks_count":537,"subscribers_count":138,"default_branch":"master","last_synced_at":"2025-05-15T10:04:34.964Z","etag":null,"topics":["artificial-intelligence","dino","genetic-algorithm","genome","google-dinosaur","neural-network"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivanseidel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-27T21:19:53.000Z","updated_at":"2025-05-11T13:43:22.000Z","dependencies_parsed_at":"2022-09-05T13:30:30.219Z","dependency_job_id":null,"html_url":"https://github.com/ivanseidel/IAMDinosaur","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2FIAMDinosaur","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2FIAMDinosaur/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2FIAMDinosaur/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2FIAMDinosaur/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivanseidel","download_url":"https://codeload.github.com/ivanseidel/IAMDinosaur/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319719,"owners_count":22051073,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","dino","genetic-algorithm","genome","google-dinosaur","neural-network"],"created_at":"2024-09-24T20:12:27.021Z","updated_at":"2025-05-15T10:05:32.999Z","avatar_url":"https://github.com/ivanseidel.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# IAMDinosaur\n\n![IAMDinosaur](https://raw.githubusercontent.com/ivanseidel/IAMDinosaur/master/assets/top_score.png)\n\nA simple artificial intelligence to teach Google Chrome's offline dinosaur to\njump cactus, using Neural Networks and a simple Genetic Algorithm.\n\n**Watch** this video to see it in action, and learn how it works: [Artificial Intelligence with Google's Dinosaur](https://youtu.be/P7XHzqZjXQs)\n\n## Installation\n\n1. Install `Node.js` on your computer.\n\n2. Clone/download this folder to your computer.\n\n3. run `npm install` within this folder\n\n4. Open Chrome's dinosaur game and put aside the terminal (It MUST be on the same screen)\n   **(Tip: go to developer tools, and under network, set to offline )**\n\n5. run `node index` within this folder. If the game was located, it will move the cursor\n   of the mouse to the origin of the `floor` of the dino. Press `s` key in the terminal to \n   start learning. \n\n\n## How does it work\n\nWe have 3 different inputs read from the pixels of the screen:\n\n1. Distance from the next cactus\n2. Length of the next cactus\n3. Speed of the current cactus\n\nWe have also, one output with 3 possible states:\n\n1. output \u003c 0.45: Press DOWN key\n2. output \u003e 0.55: Press UP key\n2. default: Release both keys\n\n## Genetic Algorithm\n\nEach Generation consists of 12 neural networks (Genomes). \n\nEach genome is tested with the game, by constantly mapping the read \ninputs from  the game to the inputs of the neural network, and by getting\nthe output/activation from the network and applying to the keys of the\nkeyboard.\n\nWhile testing each genome, we keep track of it's \"fitness\" by counting\njumped cactus in the game.\n\nWhen an entire generation is completed, we remove the worst genomes until\nachieving `N` genomes. With those `N` genomes, we then select two randomly,\nand cross-over their values/configurations. After that, we apply random mutations\nin the values/configurations of the Neural Network, creating a new genome.\n\nWe do the cross-over/mutation until we get 12 genomes again, and repeat it constantly.\n\n\n## Implementation\n\nAll the implementation was done using Node.js, with Synaptic (Neural Network library),\nand RobotJs (a library to read pixels and simulate key presses).\n\nThere are a few files in the project:\n\n- `index.js`: It tight all things together.\n\n- `Scanner.js`: Basic abstraction layer above RobotJs library that reads the screen like\n  ray tracing. Also have some utilities functions.\n\n- `UI.js`: Global scope for the UI management. It initializes and also updates the screen\n  on changes.\n\n- `GameManipulator.js`: Has all the necessary code to read sensors, and apply outputs\n  to the game. Is also responsible for computing points, getting the game state and\n  triggering callbacks/listeners to real implementation.\n\n- `Learner.js`: It is the core implementation of the Genetic Algorithm. This is where\n  \"magic\" happens, by running generations, doing \"natural\" selection, cross-over, mutation...\n\n\n### How to: Load a genome\n\n1. Make sure Genome is inside `genomes` folder with a `.json` extension\n2. Run the program\n3. Click the list in the terminal\n4. Navigate up/down to the wanted file\n5. Press `enter` (then, to start, press `s`)\n\n### Some shortcuts\n\n1. Run the program\n2. Press `o` to save the generation\n3. Press ´escape´, ´q´ or `C-c` to finish the process\n\n\n### Be aware of a game bug\n\nThe dino game has a anoying bug: It starts to \"drift\" to the right with time\nmaking the dino to be wrong offseted from the origin of the game. That, makes\nthe program to read the dino as a cactus, since it is the same color.\n\nYou can fix that by continuously refreshing the page, or, by pasting this code inside the \nconsole in the element inspector:\n\n```\n// Make sure the dino does not drift to the right\nsetInterval(function (){Runner.instance_.tRex.xPos = 21}, 2000)\n```\n\n## Development guidelines\n\nPlease, follow the Node.js style guide from [Felix](https://github.com/felixge/node-style-guide).\nIt is not complex, and has a great simple pattern for things.\n\n## Credits\n\n- [Ivan Seidel](https://github.com/ivanseidel)\n- [João Pedro](https://github.com/joaopedrovbs)\n- [Tony Ngan](https://github.com/tngan) **The idea came from him**\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanseidel%2Fiamdinosaur","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivanseidel%2Fiamdinosaur","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanseidel%2Fiamdinosaur/lists"}