项目
在 GitHub 查看
bones-ai/rust-snake-ai-ratatui
关于
Neural network learns to play snake in a terminal, built in Rust with Ratatui
系统与基础设施实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文AI learns to play Snake!
A neural network learns to play snakes in the terminal
It was built from scratch using Rust and Ratatui
Check out this for an older version of the AI that uses macroquad for GUI

Usage
- Clone the repo
git clone [email protected]:bones-ai/rust-snake-ai-ratatui.git
cd rust-snake-ai-ratatui
- Run the simulation
cargo run --release
Configs
- The project configs file is located at
src/configs.rs - Update
IS_LOW_DETAIL_MODEfor a minimal UI, it runs faster - Set
IS_SAVE_BEST_NETto train your own network. Networks are saved indata/net.json, once saved you can use the trained network by settingIS_LOAD_SAVED_NET - Use
data/net-100.jsonto watch the AI complete the game for 15x15 board size - Set
USE_GAME_CANVASto true and updateVIZ_GAME_SCALEto scale the game if needed.
Resources
Here are some excellent resources to learn more about genetic algorithms:
- Video Series: Genetic Algorithm by The Coding Train.
- Book: Nature of Code by Daniel Shiffman for those who prefer reading.
- I highly recommend checking out Joshka's fork for more idiomatic Rust code.
Algorithm
- Initialization:
- The simulation begins at
Generation 0. - A new population of snakes is created, each with a neural network initialized with random weights and biases.
- The simulation begins at
- Game Update:
- Each step, every game is updated by passing vision inputs to the neural network to decide the snake's action.
- A game is flagged as complete if:
- The snake collides with walls or itself.
- The snake fails to eat food within a certain number of steps, preventing indefinite looping.
- Generation Completion:
- The generation continues updating each game until all games are complete.
- Fitness Evaluation:
- At the end of each generation, snakes are ranked based on their performance.
- Parent Selection:
- Parents for the next generation are chosen based on rankings. Higher-ranked snakes have a higher probability of being selected as parents.
- Reproduction:
- Techniques such as roulette wheel selection, elitism, and other methods are used to generate children for the next generation.
- New Generation:
- A new population is created, and the process repeats from step 2 until the simulation is manually stopped.
This iterative process leads to snakes fine-tuning their strategies, resulting in longer snakes over time.
Extras
- I mostly post about my projects on - Twitter - @BonesaiDev
- Video of snake completing the game
- Check out my other projects on github https://github.com/bones-ai
探索相关项目
加载中…