pygame window is not shutting down with env.close()

29 Views Asked by At

I've been trying to set up the reinforcement learning environment on my local PC (intel mac os), but I've been continuously having an issue with the pygame window. System is not letting me to close the pygame window with the env.close() function.

Here is my main.py.

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

env_name = 'CartPole-v1'
env = gym.make(env_name, render_mode="human")

for episode in range(1, 11):
    score = 0
    state = env.reset()
    done = False

    while not done:
        env.render()
        action = env.action_space.sample()
        n_state, reward, done, info, _ = env.step(action)
        score += reward

    print('Episode:', episode, 'Score:', score)
env.close()

I've tested on multiple IDEs and different versions of python, but I'm currently lost about what to do. The only way I was able to turn it off was, restart the kernel on Jupiter Notebook, or re-run the script on pycharm. I'm looking for an advise to resolve this issue.

I was able to obtain the score values without using the render_mode="human", but the display would not show up unless I set the specific render_mode. I'm not quite sure how all the other tutorials on youtube were able to pop up the rendering display.

Here's an output sample of the code from pycharm.

/opt/anaconda3/envs/cartpole/lib/python3.8/site-packages/gym/utils/passive_env_checker.py:233: DeprecationWarning: `np.bool8` is a deprecated alias for `np.bool_`.  (Deprecated NumPy 1.24)
  if not isinstance(terminated, (bool, np.bool8)):
Episode: 1 Score: 25.0
Episode: 2 Score: 21.0
Episode: 3 Score: 24.0
Episode: 4 Score: 37.0
Episode: 5 Score: 33.0
Episode: 6 Score: 25.0
Episode: 7 Score: 20.0
Episode: 8 Score: 22.0
Episode: 9 Score: 10.0
Episode: 10 Score: 21.0
env.close()

Although I've added another env.close() function, pygame window never closes. Please let me know if I'm missing anything. I really appreciate for the help.

0

There are 0 best solutions below