openai / gym

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

Can't close rendered window gym #878

Closed jake-sanvito closed 6 years ago

jake-sanvito commented 6 years ago

I have been fooling around with gym for a few days and boy is it frustrating. A minor nag is that I cant close any window that gets opened. For example:

import gym env = gym.make('CartPole-v0') highscore = 0 for i_episode in range(20): # run 20 episodes observation = env.reset() points = 0 # keep track of the reward each episode while True: # run until episode is done env.render() action = 1 if observation[2] > 0 else 0 # if angle if positive, move right. if angle is negative, move left observation, reward, done, info = env.step(action) points += reward if done: if points > highscore: # record high score highscore = points break

This runs fine but I am left with an open window each time. Like the other forum comments noted, this goes away when the jupyter notebook is closed. But it kinda stinks to have to open and close the whole thing each time.

chenhang98 commented 6 years ago

I think you should use env.close() whenever you want to close the window

akanimax commented 6 years ago

@tinyalpha, calling env.close() closes the environment freeing up all the physics' state resources, requiring to gym.make() the environment again. Since, there is a functionality to reset the environment by env.reset() without closing and remaking the environment, it would be really beneficial to add to the api a method to close the render window without destroying the environment.

cfperez commented 6 years ago

That's not what happens (at least for classic_control and mujoco). Closing just closes the window.

akanimax commented 6 years ago

@cfperez Can you post a small snippet explaining what you mean? That would be really helpful for us. Thanks!

Cheers! akaniamx

cfperez commented 6 years ago

@akanimax Closing the cartpole environment just closes the viewer. https://github.com/openai/gym/blob/5c116fb3c91e872505300031d2bd60672b3a6e03/gym/envs/classic_control/cartpole.py#L140

In theory, you can create a new window, but requires explicitly setting the viewer attribute:

env.viewer = None
env.render()

I've filed #1107 to allow re-opening the window after closing.

cfperez commented 6 years ago

This is fixed (see issue above). @pzhokhov feel free to close this.

suvajit-patra commented 2 years ago
env = gym.make("CarRacing-v0")
env.reset()
for i in range(1000):
    env.render()
    env.step(env.action_space.sample())
env.reset()
env.close()

This worked for me.

PranithChowdary commented 9 months ago

This issue is still on, but don't know why openai gym closed this issue.

Andy-Mielk commented 8 months ago

same problem happens to me.

frankl1 commented 4 days ago

same problem here. I am using the GridWorld example