Solution to gym not rendering the picture (gym version number 0.26.2)

Confirm gym version number

I installed a new version of gym. The version number is 0.26.2. The reason why the screen is not rendered is that the new version of gym needs to add a new actual parameter render_mode='human' when initializing env, and does not need to actively call the render method. Official document introductory tutorial as follows

import gym
import numpy as np

env = gym.make("CartPole-v1", render_mode="human") # (1)
observation, info = env.reset(seed=42)  # (2)
for _ in range(1000):
   action = 0  # User-defined policy function
   observation, reward, terminated, truncated, info = env.step(action) # (3)

   if terminated or truncated:
      observation, info = env.reset()

env.close()

The differences in how to use the new version of gym are as follows:
(1) When initializing env, you need to add another actual parameter: render_mode
(2) When you reset, you need to set the seed parameter, and you also need to use a binomial tuple to receive data
(3) In step You need to use a five-item tuple to receive data, otherwise an error will be reported

Error ModuleNotFoundError: No module named 'pygame'

The reason for the error is that the pygame module is not installed.
One way is to install it online and execute the following command in Anaconda Prompt.

pip install pygame

Another method is to install offline
→ search for pygame on the official website www.pypi.org
Insert image description here
Insert image description here

→Select the matching version, for example, I am a 64-bit windows+python3.9 version, then download the following installation package in whl format
Insert image description here

Insert image description here
→Open Anaconda Prompt, go to the folder directory where whl is stored, and execute the pip install whl installation package name command, and the installation is successful.
Insert image description here
Insert image description here

Reference link

  • gym official website
    https://www.gymlibrary.dev/

  • OpenAI-gym's solution to render's inability to pop up the game window and wanting to not render during training and then render again during testing_gym render_Jayetchellot's blog - CSDN blog https://blog.csdn.net/Jaye_xxx/article/details/ 129461989

  • The animation window cannot be rendered when running the gym library_Problems with gym rendering animation_Liuying Ignition's blog-CSDN blog https://blog.csdn.net/weixin_44732379/article/details/127779105

Guess you like

Origin blog.csdn.net/ningmengzhihe/article/details/130841050