0

I am trying to implement simple cart pole code but pygame window doesnt close on env.close and freezes. When i try to manually close, it is restarting kernel. This is my code :

env = gym.make("CartPole-v1", render_mode = "human")
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,truncated, info = env.step(action)
        score += reward
    print("Episode:{} Score: {}".format(episode, score))
env.close()

I tried to declera env in different cell but nothing changed. I tried to uninstall and reinstall the packages but it same again. Anyone can help ?

Yigit Kaya
  • 23
  • 4

1 Answers1

0

I think that the problem is with env.render() function. It is not properly closing the window after the simulation is completed. Maybe you can try to add pygame.quit() after the while loop. Something like this:

env = gym.make("CartPole-v1", render_mode = "human")
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,truncated, info = env.step(action)
        score += reward

    print("Episode:{} Score: {}".format(episode, score))

pygame.quit()
env.close()

I hope it will help you.

kurmo
  • 38
  • 4