rlworkgroup / garage

A toolkit for reproducible reinforcement learning research.
MIT License
1.88k stars 310 forks source link

KeyError: 'render.modes' in GymEnv wrapping "CartPole-vX" #2321

Open AndreaFinazzi opened 2 years ago

AndreaFinazzi commented 2 years ago

Apparently, classic control environments in Gym have a different key for render modes in env.metadata.

In fact:

>>> import gym
>>> env = gym.make('CartPole-v1')
>>> env.metadata
{'render_modes': ['human', 'rgb_array'], 'render_fps': 50}

While in the garage wrapper it expects to find env.metadata['render.modes'], as it is for other environments. https://github.com/rlworkgroup/garage/blob/c56513f42be9cba2ef5426425a8ad36097e679c2/src/garage/envs/gym_env.py#L147

This results in a KeyError, unsurprisingly:

$ python examples/tf/trpo_cartpole.py 
Traceback (most recent call last):
  File "examples/tf/trpo_cartpole.py", line 57, in <module>
    trpo_cartpole()
  File "/home/***/.local/lib/python3.8/site-packages/garage/experiment/experiment.py", line 369, in __call__
    result = self.function(ctxt, **kwargs)
  File "examples/tf/trpo_cartpole.py", line 33, in trpo_cartpole
    env = GymEnv('CartPole-v1')
  File "/home/***/.local/lib/python3.8/site-packages/garage/envs/gym_env.py", line 147, in __init__
    self._render_modes = self._env.metadata['render.modes']
KeyError: 'render.modes'

Is it a problem of my version mix? I run with:

Thank you all for the great work!

causeim commented 2 years ago

I solved same problem by downgrading gym to 0.21.0

pip uninstall gym
pip install gym==0.21.0
krzentner commented 2 years ago

Garage setup.py specifies gym version 0.17.2. I wouldn't be surprised if some other versions work, but they've been changing the API a lot recently and I unfortunately don't have time to keep the wrapper up to date. Thanks for the the info @causeim