如何将此 python 代码从 Gym 0.26 更新到 Gym 0.21

How can I update this python code to Gym 0.26 from Gym 0.21

提问人:Mich 提问时间:11/15/2023 更新时间:11/15/2023 访问量:19

问:

Gym 的迁移指南只有 10 行代码,对于更新自定义环境没有任何用处

我试过跑步

!pip install setuptools==65.5.0 "wheel<0.40.0"
!pip install gym==0.21
!pip install stable-baselines3[extra]

但是我仍然在功能上遇到相同的错误env_check

AssertionError: Your environment must inherit from the gymnasium.Env class cf. https://gymnasium.farama.org/api/env/
 

我的代码如下,

我不知道如何将其转换为健身房 v 0.26,因为没有关于这样做的指南

如果有人可以帮助我需要更改哪些行,这将非常有帮助,谢谢

import gym
from gym import spaces

import cv2
import numpy as np
import random
from google.colab.patches import cv2_imshow
from IPython.display import display, HTML, clear_output

class PongEnv(gym.Env):
    """Custom Environment that follows gym interface."""

    metadata = {"render_modes": ["human"], "render_fps": 30}

# parameters
    width=600
    height=480
    paddlewidth=150
    paddleheight=20
    paddlecolor=(75, 153, 242)
    lives=3
    deltax=10
    deltay=-10
    xpos=width//2
    ypos=height//2
    ballradius=8
    highscore=0
    bgcolor=(30,79,24)
    scorecolor=(149, 129,252)
    prevval=683
    myscore=0
    paddle = width//2
    done = False

    def __init__(self):
        super(PongEnv, self).__init__()
        # Define action and observation space
        # They must be gym.spaces objects
        # Example when using discrete actions:
        self.action_space = spaces.Discrete(2)
        # Example for using image as input (channel-first; channel-last also works):
        self.observation_space = spaces.Box(low=-self.width, high=self.width, shape=(5,), dtype=np.float32)

    def step(self, action):
        background=np.zeros([self.height,self.width,3],dtype=np.uint8)
        background[:,:]=self.bgcolor

        if action == 0:
            self.val = -1
        else:
            self.val = 1

        self.paddle = self.paddle + self.val
        if self.paddle > self.width:
            self.paddle = self.width
        elif self.paddle < 0:
            self.paddle = 0

        paddleleftcorner=(self.paddle - self.paddlewidth//2,self.height-self.paddleheight)
        paddlerightcorner=(self.paddle + self.paddlewidth//2,self.height)

        cv2.rectangle(background,paddleleftcorner,paddlerightcorner,self.paddlecolor, -1)

        cv2.circle(background,(self.xpos,self.ypos),self.ballradius,(255,255,255),-1)
        self.xpos+=self.deltax
        self.ypos+=self.deltay

        if self.xpos>=self.width-self.ballradius or self.xpos<=self.ballradius:
            self.deltax=-self.deltax
        if self.ypos<=self.ballradius:
            self.deltay=-self.deltay
        if self.xpos<=paddlerightcorner[0] and self.xpos>=paddleleftcorner[0]:
            if self.ypos>=self.height-self.paddleheight-self.ballradius:
                cv2.putText(background,'Scored',(self.width-180,135),cv2.FONT_HERSHEY_PLAIN,2,self.scorecolor,2)
                self.deltay=-self.deltay
                self.myscore+=1
                self.reward = 1
        cv2.putText(background,'Lives: '+str(self.lives),(self.width-180,35),cv2.FONT_HERSHEY_PLAIN,2,self.scorecolor,2)
        cv2.putText(background,'Score: '+str(self.myscore),(self.width-180,101),cv2.FONT_HERSHEY_PLAIN,2,self.scorecolor,2)
        if self.ypos>=self.height:
            self.done = True 
            self.lives-=1
            temp=cv2.blur(background,(15,15))
            cv2.putText(temp,'You Lost a Life !',(self.width//4,self.height//2),cv2.FONT_HERSHEY_DUPLEX,3,(185,89,200),3,1)        
            clear_output(wait=True)
            cv2_imshow(temp)
            cv2.waitKey(2)
            self.reward = -1
        info  = {}
        clear_output(wait=True)
        cv2_imshow(background)
        return self.observation, self.reward, self.terminated, self.truncated, info

    def reset(self, seed=None, options=None):
        self.done = False
        background=np.zeros([720,1366,3],dtype=np.uint8)
        background[:,:]=self.bgcolor

        self.xpos=self.width//2
        self.ypos=self.height//2
        if self.deltay>0:
            self.deltay=-self.deltay

        self.observation=np.array([self.xpos, self.ypos, self.deltax, self.deltay, self.paddle])

        return self.observation
Python PyTorch 稳定基线 协作

评论


答: 暂无答案