Reinforcement learning Openai Gym basic environment construction

Gym provides some basic reinforcement learning environments and integrates many interesting environments. Let's take a look at how to build a Gym development environment on your computer.

​ Github address of Gym .

1. ReadMe Interpretation

Gym is an open source Python library for developing and comparing reinforcement learning algorithms by providing a standard API for communication between reinforcement learning algorithms and environments, and a standard set of environments conforming to the API. Since its release, Gym's API has become the standard in the field of reinforcement learning.

​ Gym's official document address: https://www.gymlibrary.ml/

​ At first, I couldn’t find this document. What I saw was a simple [one-page document] ( Gym (openai.com) ) provided on the official website of Gym. I have always felt that the documentation of Gym is too simple and has very little content. It is difficult to start developing my own environment, but I found the above documentation later, which is basically consistent with my study during this period, which can be regarded as two-phase confirmation.

Resource Allocation Map

​ Gym is more a standard for an intensive learning environment. You only need to implement a few key interfaces of Gym, and the rest of the content is free to play. You can use Pygame, pyglet, etc. to develop a game as an environment, or You can simply use matplotlib to render the environment image. Later, I will have an article to introduce Gym's API separately.

​ Gym now supports python versions 3.7, 3.8, 3.9, and 3.10, and supports installation on Linux and Mac; it also supports installing Gym on Windows, but there is no official support. Some environments in Gym, such as Mujoco, do not support Windows as well as Linux and Mac, but for beginners, is enough to use. And on different systems, the installation steps are basically similar.

2. Environment construction

​ For the management of the python environment, I personally recommend that you use conda. Conda is not only a tool for installing python packages, but its more important function is a tool for environment management. We can create python environments through conda. Each environment is independent of each other, and different versions of python can be used in each environment. , different versions of the development package. The conda environment is similar to docker, and with conda, you no longer have to switch python versions back and forth on a computer due to code reasons, or upgrade or upgrade a certain package for a certain project and affect other projects.

Resource Allocation Map

​ The installation standard of conda will not be discussed further, you can refer to this article (Windows, linux can be Baidu by itself). After conda is installed, you must remember to configure the domestic source, otherwise the download speed will be limited.

​ After python is installed, execute the following command to create a python virtual environment (you can choose the name yourself). I am using version 3.7 of Python. You can choose the version you need:

conda create --name Gym python=3.7

​ After a while, conda will help us create an environment named Gym and python version 3.7.

conda info --envs
conda activate Gym

​ These two commands are to view the python environment installed on the machine, and the other is to activate the virtual environment Gym on the current terminal, that is, to switch the python used by the current terminal to Gym. You can see that the symbol at the front has changed to (Gym ).

Resource Allocation Map
pip install gym
pip install pygame
pip install numpy

​ In this way, the basic environment of Gym is installed. We open Pycharm (Python development environment, it is recommended to use Pycharm, the page is simple and powerful. If you have a campus mailbox, you can use the professional version through education discounts, or use the community office. It is also enough to use), create a new project, create a python file, and copy the following code into the file:

import gym
import time
env = gym.make("CartPole-v1")
observation, info = env.reset(seed=42, return_info=True)

for _ in range(1000):
    # 显示图像
    env.render()
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    time.sleep(0.01)
    if done:
        observation, info = env.reset(return_info=True)
env.close()

​ To execute the above code, you also need to set the python environment in pycharm to the Gym we just created, click the lower right corner, Add Interpreter,

Resource Allocation Map

​ Then select Existing environment, remember to check Make available to all project, so that other projects can directly use the added interpreter in the future,

Resource Allocation Map

​ Click the three dot icon on the right, go to the python.exe in our conda installation directory –>envs–>Gym directory (this is the directory under windows, if it is linux, you need to enter the bin directory and find python executable, with a small question mark icon)

Resource Allocation Map

​ Click ok, just click ok, after pycharm switches the environment, we can execute the above test code, the execution results are as follows:

Resource Allocation Map

3. Summary

​ The content of this article is over here, and the basic environment of Gym is relatively simple to build. Gym has separated different modules. The basic environment of Gym we installed above, and other environments such as Atari and mojoco need to be installed separately by users. There will be an article later on how to install the Atari environment and use the keyboard to play Atari games.

​ If this article can help you, please like, bookmark, and follow the blogger. If there are any mistakes, please criticize and correct.

Guess you like

Origin blog.csdn.net/qq_34666857/article/details/124019116