-
-
Notifications
You must be signed in to change notification settings - Fork 875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] 'NoneType' object has no attribute 'glfwGetCurrentContext' #1224
Comments
You may have to close the env before the program ends. import gymnasium as gym
env=gym.make('Humanoid-v5', render_mode='human')
obs=env.reset()
env.render()
env.close() |
This might be a windows only issue We don't officially support windows but will try to fix bugs |
If you're using Windows, try the Linux subsystem. |
I ran into this issue as well while using gymnasium to render my MuJoCo environment in Stable-Baselines3. Details and how to replicate are as follows: DetailsOS: Ubuntu 22.04.5 LTS How to replicatefrom stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3 import TD3
# Number of evaluation episodes
N_EVAL_EPISODES = 10
# Environment ID
ENV_ID = 'InvertedPendulum-v5'
# Optimized hyperparameters for the TD3 agent
TD3_OPTIMIZED_HYPERPARAMS = {
'learning_rate': 0.001,
'buffer_size': 100000,
'tau': 0.01,
'gamma': 0.999,
'train_freq': 100,
'gradient_steps': 200,
'batch_size': 64,
'policy_delay': 1
}
from lib.utils import *
class TD3Agent:
"""
TD3 Agent implementation for training and evaluation.
"""
def __init__(self, env_id: str, hyperparams: dict, verbose: int = 1):
"""
Initializes the TD3Agent.
Args:
env_id (str): The Gym environment ID.
hyperparams (dict): Dictionary containing hyperparameters for the TD3 agent.
verbose (int, optional): Verbosity level (default: 1).
"""
self.env_id = env_id
self.env = make_vec_env(env_id, n_envs=1, env_kwargs={'render_mode': 'human'})
self.hyperparams = hyperparams
self.verbose = verbose
self.model = None
def evaluate(self, n_eval_episodes: int = 10) -> tuple:
"""
Evaluates the TD3 model on the environment.
Args:
n_eval_episodes (int): Number of evaluation episodes.
Returns:
tuple: A tuple containing mean reward and standard deviation of rewards.
"""
if self.model is None:
raise ValueError("Model has not been created or trained yet.")
mean_reward, std_reward = evaluate_policy(
self.model,
self.env,
n_eval_episodes=n_eval_episodes
)
return mean_reward, std_reward
def load(self, path: str):
"""
Loads a pre-trained TD3 model from the specified path.
Args:
path (str): Path to the saved model file.
"""
self.model = TD3.load(path, env=self.env)
def run_algorithm():
"""
Creates and evaluates a TD3 agent using the specified environment and hyperparameters.
"""
agent = TD3Agent(env_id=ENV_ID, hyperparams=TD3_OPTIMIZED_HYPERPARAMS, verbose=1)
agent.load("agent") # Path to the pre-trained agent
print("Evaluating...")
mean_reward, std_reward = agent.evaluate(n_eval_episodes=N_EVAL_EPISODES)
print(f"Stats: Mean Reward: {mean_reward:.2f} +/- {std_reward}\n")
# agent.env.close() # Comment this to see the error
if __name__ == "__main__":
run_algorithm() The above code evaluates a pre-trained TD3 agent using SB3 on the InvertedPendulum-v5 MuJoCo environment. The env component is handled by gymnasium and I was getting the same error as this issue. What causes the issue
How to fix this issue
|
import gymnasium
env = gymnasium.make('Humanoid-v5', render_mode='human')
obs, info = env.reset()
# env.render() # no need to call it with `render_mode="human"`
env.unwrapped.mujoco_renderer.viewer._paused = True # this pauses the render window, it can be unpaused by pressing the SPACE key
for _ in range(1000):
env.step(env.action_space.sample())
Exception ignored in: <function WindowViewer.__del__ at 0x7f058805ee80>
Traceback (most recent call last):
File "/home/master-andreas/Gymnasium/gymnasium/envs/mujoco/mujoco_rendering.py", line 377, in __del__
File "/home/master-andreas/Gymnasium/gymnasium/envs/mujoco/mujoco_rendering.py", line 365, in free
File "/home/master-andreas/temp_env/lib/python3.13/site-packages/glfw/__init__.py", line 2369, in get_current_context
AttributeError: 'NoneType' object has no attribute 'glfwGetCurrentContext' |
Question
Hi!I have some questions for you
background:
I run the code below:
An error was reported :
When I take the solution given earlier:
The result of the code run is: The mojoco simulation interface is not responding
The text was updated successfully, but these errors were encountered: