openai / gym

A toolkit for developing and comparing reinforcement learning algorithms.
https://www.gymlibrary.dev
Other
34.77k stars 8.61k forks source link

MountainCar rgb_array rendering is broken #1461

Closed dniku closed 5 years ago

dniku commented 5 years ago
In [1]: import gym                                                                                                                                                                                                                     

In [2]: gym.__version__                                                                                                                                                                                                                
Out[2]: '0.12.1'

In [3]: gym.make('MountainCar-v0').render('rgb_array')                                                                                                                                                                                 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-b6821eb70dd3> in <module>
----> 1 gym.make('MountainCar-v0').render('rgb_array')

/usr/lib/python3.7/site-packages/gym/core.py in render(self, mode, **kwargs)
    247 
    248     def render(self, mode='human', **kwargs):
--> 249         return self.env.render(mode, **kwargs)
    250 
    251     def close(self):

/usr/lib/python3.7/site-packages/gym/envs/classic_control/mountain_car.py in render(self, mode)
    112             self.viewer.add_geom(flag)
    113 
--> 114         pos = self.state[0]
    115         self.cartrans.set_translation((pos-self.min_position)*scale, self._height(pos)*scale)
    116         self.cartrans.set_rotation(math.cos(3 * pos))

AttributeError: 'MountainCarEnv' object has no attribute 'state'

This is a regression in 0.12.1, as 0.12.0 works fine.

christopherhesse commented 5 years ago

You should call env.reset() first, I think that's the only thing missing here.

dniku commented 5 years ago

Uh... a silly mistake, but perhaps the error could be made a little more informative? For example, the state field could be initialized in __init__ (with None, probably), and then render could check whether the environment has been reset.

KhawYewmeng commented 5 years ago

You should call env.reset() first, I think that's the only thing missing here.

I tried calling env.reset() first. Still got the same error? env.reset() gym.make('MountainCar-v0').render('rgb_array')

dniku commented 5 years ago

You should do something like:

import gym

env = gym.make('MountainCar-v0')
env.reset()
env.render('rgb_array')