Recording a movie of a simulation in pyRDDLGym.

Recording a movie of a simulation in pyRDDLGym.#

This basic example illustrates how to record a movie of a simulation in pyRDDLGym as a GIF file.

First install and import the required packages:

%pip install --quiet --upgrade pip
%pip install --quiet pyRDDLGym rddlrepository
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.

Import the required packages:

from IPython.display import Image
import os

import pyRDDLGym
from pyRDDLGym.core.policy import RandomAgent
from pyRDDLGym.core.visualizer.movie import MovieGenerator
C:\Python\envs\rddl\Lib\site-packages\pyRDDLGym\core\debug\exception.py:28: UserWarning: cv2 is not installed: save_as_mp4 option will be disabled.
  warnings.warn(message)

We will simulate traffic in a busy intersection by loading the traffic domain:

env = pyRDDLGym.make('TrafficBLX_SimplePhases', '0')

Next, we will instantiate the movie generator. We specify the folder where the individual frame will be saved, and the maximum number of frames in the video:

if not os.path.exists('frames'):
    os.makedirs('frames')
env.horizon = 30   # just to speed things up
recorder = MovieGenerator("frames", "traffic", max_frames=env.horizon)
env.set_visualizer(viz=None, movie_gen=recorder)

Finally, we will simulate as usual, and the movie will be created when we close the environment:

agent = RandomAgent(action_space=env.action_space, num_actions=env.max_allowed_actions)
agent.evaluate(env, episodes=1, render=True)
env.close()
C:\Python\envs\rddl\Lib\site-packages\pyRDDLGym\core\debug\exception.py:28: UserWarning: Removed 30 temporary files at frames\traffic_*.png.
  warnings.warn(message)
Image(filename='frames/traffic_0.gif') 
../_images/4d2b756d36d65933c607941db92a06d59c626eb74a43b0f2a3f9dfcc9f727a50.gif