pythonartificial-intelligencereinforcement-learningopenai-gymstable-baselines

Render() doesn't work for gym environment


I'm working on a reinforcement learning project for the Breakout game, and my environment (env) is set to ALE/Breakout-v5.

I've previously trained a model, saved it, and now when I want to see its output in a Jupyter notebook, it correctly calculates the average rewards but doesn't display any environment. It only provides textual output.

Here's the project code:

import os
from stable_baselines3.common.vec_env import VecFrameStack
from stable_baselines3 import A2C
from stable_baselines3.common.evaluation import evaluate_policy

env = make_atari_env('ALE/Breakout-v5', n_envs=1, seed=0)
env = VecFrameStack(env, n_stack=4)

a2c_path = os.path.join('Training', 'Saved Models', 'A2C_Breakout_Model')
model = A2C.load(a2c_path, env)

evaluate_policy(model, env, n_eval_episodes=100, render=True)

How to visualize the environment? I'm using Python 3.11 and Jupyter Notebook.


Solution

  • I was not able to render directly via evaluate_policy either, however here is a work around that worked for me by loading the pre-trained model and rendering the predicted the next actions:

    vec_env = model.get_env() 
    obs = vec_env.reset()
    for i in range(1000):
        action, _states = model.predict(obs, deterministic=True) 
        obs, rewards, dones, info = vec_env.step(action) 
        vec_env.render("human")