{"id":43975426,"url":"https://github.com/opyate/neuro-evolved-tetris","last_synced_at":"2026-02-07T08:08:34.953Z","repository":{"id":254355652,"uuid":"846290284","full_name":"opyate/neuro-evolved-tetris","owner":"opyate","description":"PyTorch net that learns to play Tetris, and evolves using crossover and gene mutation.","archived":false,"fork":false,"pushed_at":"2024-10-23T14:24:19.000Z","size":9471,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-23T17:06:25.744Z","etag":null,"topics":["docker-compose","fastapi","p5js","pytorch","redis","websocket"],"latest_commit_sha":null,"homepage":"https://pdfcrun.ch/blog/2024-10-17-neuro-evolutionary-tetris/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opyate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-22T22:47:25.000Z","updated_at":"2024-10-23T14:24:29.000Z","dependencies_parsed_at":"2024-08-23T00:12:54.204Z","dependency_job_id":"1fd6bb94-83a6-4300-b07c-74bf835a5837","html_url":"https://github.com/opyate/neuro-evolved-tetris","commit_stats":null,"previous_names":["opyate/neuro-evolved-tetris"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/opyate/neuro-evolved-tetris","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyate%2Fneuro-evolved-tetris","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyate%2Fneuro-evolved-tetris/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyate%2Fneuro-evolved-tetris/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyate%2Fneuro-evolved-tetris/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opyate","download_url":"https://codeload.github.com/opyate/neuro-evolved-tetris/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyate%2Fneuro-evolved-tetris/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29189675,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T07:37:03.739Z","status":"ssl_error","status_checked_at":"2026-02-07T07:37:03.029Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["docker-compose","fastapi","p5js","pytorch","redis","websocket"],"created_at":"2026-02-07T08:08:34.293Z","updated_at":"2026-02-07T08:08:34.948Z","avatar_url":"https://github.com/opyate.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Intro\n\nNeuro-evolutionary bots that learn to play Tetris, by spawning thousands of bots, then doing crossover on weighted selections of nets (virtual coin flip whether a weight comes from parent A or B), then mutation (a small percent chance that any given weight will mutate, mimicking real-world genetic mutations, which typically introduce minor changes rather than entirely new traits).\n\n[Video](https://www.youtube.com/watch?v=Q1aHP5IODUs):\n\n[![Video](https://img.youtube.com/vi/Q1aHP5IODUs/0.jpg)](https://www.youtube.com/watch?v=Q1aHP5IODUs)\n\n# Implementation\n\nTetris neuro-evolved NNs which uses a worker model.\n\nServer: interface between client and workers\nWorker: Docker scaled workers, with light-weight Redis-backed coordinator, and will each run hundreds of brains, and use Redis-backed store for crossover and mutation.\nClient: p5/canvas HTML to render simulations, and getting board states through web socket.\n\n# Run\n\nCopy the `.env`:\n\n```\ncp .env.example .env\n```\n\nThen modify `NUMBER_OF_WORKERS` and `BOTS_PER_WORKER`.\n\n```\ndocker compose up\n```\n\nThen visit\n\nhttp://127.0.0.1:8000/\n\n\n# Development setup\n\n```\npython3 -m venv .venv\nsource .venv/bin/activate\npip install -r requirements.txt\npip3 install torch --index-url https://download.pytorch.org/whl/cu124\n```\n\nGot redis.conf from:\n\n```\ncurl -O https://raw.githubusercontent.com/redis/redis/refs/tags/7.4.0/redis.conf \n```\n\n# Task definition\n\nI am building a neuro-evolutionary simulation of bots learning to play Tetris using Python and PyTorch, which does the following:\n\n- spawn thousands of bots from the simulation runner, and have a bots_list of size N\n- the bots are initialised with a zeroed PyTorch network, which has 10x20 inputs, and 6 outputs\n- each bot has a state, in the form of a Tetris engine\n- the PyTorch network's inputs represent the Tetris grid, and the 6 outputs represent the 6 moves a bot can make: up, down, left, right, rotate_clockwise, rotate_counter_clockwise or noop (which means \"make no move\", or \"no operation\")\n- the simulation loop now starts, and the runner asks each bot to predict their next move\n- after each bot made their move, they get one fitness point for staying alive, and many more fitness points for scoring (i.e. clearing lines on the Tetris grid)\n- after each bot made their move, they might also be in a game over state\n- after all bots reach game over, the evolution starts\n- evolution step 1: parents are chosen based on a weighted selection\n- evolution step 2: the parents are crossed-over, to produce a child\n- evolution step 3: the child is slightly mutated\n- set each bot's next_brain to the new child\n- the entire bots_list is re-initialised so their next_brain becomes their current_brain\n- the simulation loops\n\n# Future\n\n- https://github.com/joerick/pyinstrument to find slow spots in code\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopyate%2Fneuro-evolved-tetris","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopyate%2Fneuro-evolved-tetris","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopyate%2Fneuro-evolved-tetris/lists"}