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

59 Views Asked by At

The migration guide for Gym only has 10 lines of code, none of which are any useful useful for updating custom environments

I tried running

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

But I still get the same error on env_check function

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

My code is below,

I don't know how to convert it to gym v 0.26 because there are no guidelines on doing this

If someone can help with what lines I need to change, this would be very helpful, thanks

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
0

There are 0 best solutions below