Coder Social home page Coder Social logo

luishsr / rust-snake-ai-ratatui Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bones-ai/rust-snake-ai-ratatui

0.0 0.0 0.0 257 KB

Neural network learns to play snake in a terminal, built in Rust with Ratatui

License: MIT License

Rust 100.00%

rust-snake-ai-ratatui's Introduction

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

screenshot

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_MODE for a minimal UI, it runs faster
  • Set IS_SAVE_BEST_NET to train your own network. Networks are saved in data/net.json, once saved you can use the trained network by setting IS_LOAD_SAVED_NET
  • Use data/net-100.json to watch the AI complete the game for 15x15 board size
  • Set USE_GAME_CANVAS to true and update VIZ_GAME_SCALE to scale the game if needed.

Resources

Here are some excellent resources to learn more about genetic algorithms:

Algorithm

  1. 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.
  2. 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.
  3. Generation Completion:
    • The generation continues updating each game until all games are complete.
  4. Fitness Evaluation:
    • At the end of each generation, snakes are ranked based on their performance.
  5. Parent Selection:
    • Parents for the next generation are chosen based on rankings. Higher-ranked snakes have a higher probability of being selected as parents.
  6. Reproduction:
    • Techniques such as roulette wheel selection, elitism, and other methods are used to generate children for the next generation.
  7. 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

rust-snake-ai-ratatui's People

Contributors

bones-ai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.