FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Building the perfect A.I. Snake game player using Ray, RLlib, Pytorch, and Gymnasium

Chelaru Adrian
FAUN — Developer Community 🐾
9 min readApr 26, 2023

--

You always imagine how it would be to train the computer the knowledge to play a game in place of you 🥷, so let’s make a dream come true using reinforcement learning.

A Snake game with PyGame and Gymnasium | Icons: iconarchive

For short, you got to trial and error multiple times while tuning the machine learning network parameters to get the action we want the snake to take at the output of the network, from a game frame input, resized and post-processed to make it compatible with the requirements.

This process is quite tendentious, as it requires a lot of computing, so if you don’t have access to a GPU-accelerated PC, preferably Nvidia, then you could use a service like Google Colab, but making the inference — running the network — on the PC using CPU is possible for the small size networks.

To get the code for training and inference, look at the bottom of the article.

Libraries

I recommend you install Miniconda to set up a proper environment to run the Ray network on your PC. Alternatively, you can choose to stick with the Colab, but you will experience only a recorded video, not a live demo.

This environment is configured for PC with Nvidia GPU.

Inside the README.txt you can find out how to set up your Conda environment to run it on your PC.

The standard libraries you will have to get installed in the environment are ray, ray[rllib], opencv-python, numpy, pygame, and gymnasium.

Ray enables data scientists and machine learning practitioners to easily scale jobs, access cloud computing resources, and leverage the ML ecosystem, while handling distributed systems processes for engineers.

RLlib, part of Ray, is a reinforcement learning library that supports production-level, highly distributed RL workloads with unified APIs for various applications. It offers simple solutions for multi-agent setups, offline datasets, and external simulators. Ray, RLlib is compatible with the OpenAI Gym environment, now known as Gymnasium, that let users create a control interface for a simulation or a game like the Snake created with the help of the PyGame library.

About the algorithm

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Chelaru Adrian

💻 Hey, I'm here to share my passion for tech! 🚩 Follow me on Medium for exciting new ideas and innovative solutions.

Responses (1)

Write a response