{"id":19831224,"url":"https://github.com/torlenor/rlsnake","last_synced_at":"2026-04-17T10:03:02.168Z","repository":{"id":130804705,"uuid":"315008150","full_name":"torlenor/rlsnake","owner":"torlenor","description":"Reinforcement learning for the classic game Snake","archived":false,"fork":false,"pushed_at":"2020-11-22T10:39:14.000Z","size":3782,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T10:16:06.037Z","etag":null,"topics":["game","openai-gym","reinforcement-learning","snake","tensorflow"],"latest_commit_sha":null,"homepage":"https://torlenor.org/machine%20learning/reinforcement%20learning/2020/11/22/machine_learning_reinforcement_learning_snake.html","language":"Python","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/torlenor.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-22T10:11:02.000Z","updated_at":"2022-02-17T21:35:16.000Z","dependencies_parsed_at":"2023-11-20T16:15:06.376Z","dependency_job_id":null,"html_url":"https://github.com/torlenor/rlsnake","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/torlenor%2Frlsnake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torlenor%2Frlsnake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torlenor%2Frlsnake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torlenor%2Frlsnake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/torlenor","download_url":"https://codeload.github.com/torlenor/rlsnake/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241191399,"owners_count":19925241,"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":["game","openai-gym","reinforcement-learning","snake","tensorflow"],"created_at":"2024-11-12T11:29:14.633Z","updated_at":"2026-04-17T10:02:57.139Z","avatar_url":"https://github.com/torlenor.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RLSnake\n\n[![Build and Test](https://github.com/torlenor/rlsnake/workflows/Build%20and%20Test/badge.svg?branch=master)](https://github.com/torlenor/rlsnake/actions?query=workflow%3A%22Build+and+Test%22)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](/LICENSE)\n\n## Description\n\nAn implementation of the classic game Snake and an environment to use to use in reinforcement learning. An actor-critic agent with a pre-trained model is provided.\n\nIt is not the cleanest Python project, but it solves its purpose and hopefully someone can use parts of it or finds it interesting.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"demo.gif\" alt=\"demo\" /\u003e\n\u003c/p\u003e\n\n## Requirements\n\n- Python 3.6–3.8\n- numpy (tested with 1.18.5)\n- OpenAI Gym (tested with 0.17.3)\n- tensorflow (tested with 2.3.0)\n- tensorflow_probability (tested with 0.11.1)\n- PySdl2 (for the graphical representation)\n\nSee *the requirements.txt* file for detailed Python requirements or use\n```bash\npip install -r requirements.txt --user\n```\nto install them (note: you may have to use `pip3` on Ubuntu).\n\nYou may need to install the sdl2 libraries and development files appropriate for the operating system. On Ubuntu-based distributions you can type\n```bash\nsudo apt-get install libsdl2*-dev\n```\nto install them.\n\n## Environment\n\nThe game (rlsnake/game) is encapsulated in an environment inspired by the OpenAI Gym environments. You should be able to use the environment (rlsnake/env) in programs using such environments. However, currently you have to instantiate the environment yourself, using\n\n```python\nfrom rlsnake.envs.rlsnake_env_v1 import RlSnakeEnvV1\n\nenv = RlSnakeEnvV1(args.x, args.y, args.visibility_range)\nenv.seed(RANDOM_SEED)\n```\n\n## Using the pre-trained model\n\nWe provide a pre-trained model which you can use to get you started.\n\nType\n```bash\npython3 simulate.py --model-path models/ac_pretrained/model --x 6 --y 6 --seed 123 --autoplay\n```\nto let the agent play with this model on a 6x6 field.\n\n## Train your own model\n\nTo start training your own model, use the train script. For example\n```bash\npython3 train.py --episodes 100000 --gamma 0.995 --neurons 512 --learning-rate 1e-3 --max-steps 200 --run-id RUN_ID\n```\n\nThe results, including model checkpoints and the final model, will be stored in the directory *results/RUN_ID*.\n\n### TensorBoard\n\nWe write a few training values (reward/episode, running_reward, moves/episode) into TensorBoard data files. Type\n```bash\ntensorboard --logdir results/\n```\nto launch a TensorBoard instance and then follow the output to view the training values in real time.\n\n## Play as human\n\nWe provide a simple implementation to try out the game as a human. In contrast to the original snake, it is turn based, i.e., every time you take an action it will perform that action and wait for you to take the next step. While this takes away the stress of the original Snake, it does not matter much for reinforcement learning, as it is always possible to map one time step of the game onto one observation for the model and get back the action to apply to the game.\n\nType\n```bash\npython3 play.py\n```\nto start playing. Use your arrow keys on the keyboard to move around. You can pass the parameters `--x NUMBER_OF_HORIZONTAL_TILES` and `--y NUMBER_OF_VERTICAL_TILES` to change the size of the field.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorlenor%2Frlsnake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftorlenor%2Frlsnake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorlenor%2Frlsnake/lists"}