Search code examples
pythonreinforcement-learningassertionopenai-gym

Whenever I try to use env.render() for OpenAIgym I get "AssertionError"?


I am trying to learn Reinforcement learning. I wanted to build a Reinforcement Learning model for autonomous driving. However, whenever I use env.render() while training the Reinforcement learning model. It gives me an assertion error. The code is as below for my model:

import gym 
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import VecFrameStack
from stable_baselines3.common.evaluation import evaluate_policy
import os

environment_name = "CarRacing-v2"
env = gym.make(environment_name)

episodes = 5
for episode in range(1, episodes+1):
    state = env.reset()
    done = False
    score = 0 
    
    while not done:
        env.render()
        action = env.action_space.sample()
        n_state, reward, done, info = env.step(action)
        score+=reward
    print('Episode:{} Score:{}'.format(episode, score))
env.close()

Error:

AssertionError                            Traceback (most recent call last)
<ipython-input-31-c07c36362924> in <module>
      6 
      7     while not done:
----> 8         env.render()
      9         action = env.action_space.sample()
     10         n_state, reward, done, info = env.step(action)

~\Anaconda3\lib\site-packages\gym\core.py in render(self, *args, **kwargs)
    327     ) -> Optional[Union[RenderFrame, List[RenderFrame]]]:
    328         """Renders the environment."""
--> 329         return self.env.render(*args, **kwargs)
    330 
    331     def close(self):

~\Anaconda3\lib\site-packages\gym\wrappers\order_enforcing.py in render(self, *args, **kwargs)
     49                 "set `disable_render_order_enforcing=True` on the OrderEnforcer wrapper."
     50             )
---> 51         return self.env.render(*args, **kwargs)
     52 
     53     @property

~\Anaconda3\lib\site-packages\gym\wrappers\env_checker.py in render(self, *args, **kwargs)
     51         if self.checked_render is False:
     52             self.checked_render = True
---> 53             return env_render_passive_checker(self.env, *args, **kwargs)
     54         else:
     55             return self.env.render(*args, **kwargs)

~\Anaconda3\lib\site-packages\gym\utils\passive_env_checker.py in env_render_passive_checker(env, *args, **kwargs)
    314             )
    315 
--> 316     result = env.render(*args, **kwargs)
    317 
    318     # TODO: Check that the result is correct

~\Anaconda3\lib\site-packages\gym\envs\box2d\car_racing.py in render(self)
    566 
    567     def render(self):
--> 568         return self._render(self.render_mode)
    569 
    570     def _render(self, mode: str):

~\Anaconda3\lib\site-packages\gym\envs\box2d\car_racing.py in _render(self, mode)
    569 
    570     def _render(self, mode: str):
--> 571         assert mode in self.metadata["render_modes"]
    572 
    573         pygame.font.init()

AssertionError: 

I do not know what the problem is but I have tried to install box2d like this:

!pip install gym[box2d] pyglet==1.3.2

Please help me with this. Thanks!!!!


Solution

  • It seems you use some old tutorial with outdated information. It would need to install gym==0.25.

    With gym==0.26 you have two problems:

    1. You have to use render_mode="human" when you want to run render()

      env = gym.make("CarRacing-v2", render_mode="human")
      
    2. step() returns 5 values, not 4. See official documentation

      observation, reward, terminated, truncated, info = env.step(action)
      

    BTW:

    If you set render_mode="human" then step() will run render() automatically and you don't have to run it manually.

    See source code for step


    Documentation: Autonomous Driving and Traffic Control Environments


    Working example:

    import gym 
    
    environment_name = "CarRacing-v2"
    #environment_name = "Taxi-v3"
    #environment_name = "LunarLander-v2"
    
    env = gym.make(environment_name, render_mode="human")
    #env.metadata['render_fps'] = 150
    
    #print('render_modes:', env.metadata['render_modes'])
    #print('metadata:', env.metadata)
    
    episodes = 5
    
    for episode in range(1, episodes+1):
        
        observation, info = env.reset()
        terminated = False
        truncated = False
        score = 0 
        
        while not (terminated or truncated):
            #env.render()
            action = env.action_space.sample()
            observation, reward, terminated, truncated, info = env.step(action)
            score += reward
              
        print(f'Episode: {episode} Score: {score}')
        
    env.close()