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 git+https://github.com/pyrddlgym-project/pyRDDLGym.git
%pip install --quiet git+https://github.com/pyrddlgym-project/rddlrepository.git
Note: you may need to restart the kernel to use updated packages.
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\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\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/e03750e39d26095789c5263c1456fee111a9b91e75a01966abb8e1a61e7c1bb8.gif