Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Playing the game of snake with AI.
| Date | Stars |
|---|---|
| 2026-07-24 | 1776 |
| 2026-07-25 | 1776 |
| 2026-07-28 | 1776 |
| 2026-07-30 | 1776 |
| 2026-08-06 | 1776 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# Snake This project implements two AI algorithms to play the game of [snake](https://en.wikipedia.org/wiki/Snake_(video_game_genre)): | Algorithm | Example | Average Length* | Success Rate* | | :-------: | :-----: | :-------------: | :-----------: | |[Graph Search](#graph-search)||35.86|94%| |[Reinforcement Learning](#reinforcement-learning)||29.33|50%| \* **Average Length:** Average length of the snake before the game ends. The game ends when the snake hits itself or the wall, or consumes all the food and reaches the max length. The game grid is 6x6, so the max length is 36. The average is computed over 1000 rounds. \* **Success Rate:** Success means the snake is able to consume all the food and reach the max length without hitting itself or the wall. The rate is computed over 1000 rounds. ## Installation The project requires Python >= 3.10. Create a virtual environment first: ``` python3 -m venv venv source venv/bin/activate ``` Install mandatory dependencies: ``` pip3 install -r requirements.txt ``` Play the game with graph search (append `-h` to see all supported options): ``` python3 main.py ``` **Optional:** To play the game with reinforcement learning or train the reinforcement learning model, please install [PyTorch](https://pytorch.org/get-started/locally/) based on your CPU/GPU preference. The program supports both CPU and GPU. Run the command below to play the game with reinforcement learning: ``` python3 main.py -m rl ``` The pre-trained reinforcement learning model is available [here](./rl_model.pt). Run the command below to train your own model: ``` python3 rl_train.py ``` ## Algorithms Two algorithms are implemented to play the game. The first algorithm is based on graph search, providing a rule-based strategy for different situations. The second algorithm is based on reinforcement learning, where the snake learns to play the game by trial and error without prior knowledge. ### Graph Search The algorithm models the grid as a graph where nodes are positions and edges connect adjacent positions (up, down, left, right). The algorithm decides the next move using the strategy below: 1. If the snake is long enough, the algorithm searches for a [Hamiltonian path](#hamiltonian-path) from the snake's head to its tail. If the path exists, the snake can safely move along a Hamiltonian cycle to reach the max length. Otherwise, go to the next step. 2. The algorithm searches for the shortest path from the snake's head to the food. If the path exists, it temporarily moves the snake along the path to eat the food and checks whether there's a safe path from the snake's head to its tail afterwards. If so, it means the snake can safely eat the food without getting trapped, so it can follow the shortest path to the food. Otherwise, go to the next step. 3. At this point, the snake cannot safely eat the food. The algorithm searches for a [longer path](#longer-path) from the snake's head to its tail to rearrange the grid, hopefully creating a safe path to the food later. If the path to the tail exists, the snake can move along the path. Otherwise, go to the next step. 4. At this point, the snake can neither safely eat the food nor move towards its tail. The algorithm examines the reachable neighboring positions of the snake's head, and moves the snake towards the position farthest from the food, aiming to move away from the food and hopefully free up space to create a safe path in the future. The algorithm is implemented [here](./src/agents/graph.py#L27). #### Hamiltonian Path A [Hamiltonian path](https://en.wikipedia.org/wiki/Hamiltonian_path) is a path in a graph that visits each vertex exactly once. A Hamiltonian cycle is a Hamiltonian path that is a cycle, meaning it starts and ends at the same vertex. In the game of snake, following a Hamiltonian cycle guarantees the snake visits every position without getting trapped, and hence can safely consume all t
Excerpt of 8,799 characters
Read on GitHub207
4
2
Would you bet a product on this? Bounded 0–100 and slow moving.
matched fp:18036649b2828d7d, topic:reinforcement-learning, topic:deep-reinforcement-learning, readme:reinforcement learning