Examples

Record

Record environment data to a file, then read it back in.
#!/usr/bin/env python
"""Simple example recording CartPole data to HDF5."""
# pylint: disable=invalid-name

import gym
from gymbag import record_hdf5, HDF5Reader

# Wrap env with HDF5 recorder, specify filename
env = record_hdf5(gym.make('CartPole-v0'), 'cartpole.h5')
for episode in range(2):
    env.reset()
    done = False
    while not done:
        obs, reward, done, info = env.step(env.action_space.sample())
env.close()     # Closes the file

# Read it back
for episode in HDF5Reader('cartpole.h5'):
    for time, obs, action, reward, done, info in episode:
        print(time, obs, action, reward, done, info)

Playback

Create an environment that will play back recorded data.
#!/usr/bin/env python
"""Gymbag environment playback example.  Uses cartpole data from record.py."""
# pylint: disable=invalid-name

from gymbag import HDF5Reader, PlaybackEnv

env = PlaybackEnv(HDF5Reader('cartpole.h5'))

while not env.played_out:
    env.reset()
    done = False
    while not done:
        obs, reward, done, info = env.step(env.action_space.sample())
        print(obs, reward, done, info)

env.close()

Record to List

Record environment data to a list in memory, then play it back.
#!/usr/bin/env python
"""Record OpenAI Gym data to a list with Gymbag."""
# pylint: disable=invalid-name

import gym

from gymbag import ListRecorder, RecordEnv, PlaybackEnv

# Make a RecordEnv with a ListRecorder and wrap the CartPole env
recorder = ListRecorder()
env = RecordEnv(gym.make('CartPole-v0'), recorder)
for episode in range(2):
    env.reset()
    done = False
    while not done:
        obs, reward, done, info = env.step(env.action_space.sample())

env.close()

# Iterate over the data directly
for episode in recorder.data:
    for time, obs, action, reward, done, info in episode:
        print(time, obs, action, reward, done, info)


# Play the data back in an environment
playback_env = PlaybackEnv(recorder.data, env.observation_space, env.action_space)
while not playback_env.played_out:
    playback_env.reset()
    done = False
    while not done:
        obs, reward, done, info = playback_env.step(playback_env.action_space.sample())
        print(obs, reward, done, info)

env.close()

Agent Playback

An agent that plays back actions, ignoring the environment.
#!/usr/bin/env python
"""Gymbag agent playback example.  Uses cartpole data from record.py.
This is mostly useful with deterministic environments."""
# pylint: disable=invalid-name

import gym
from gymbag import HDF5Reader, PlaybackAgent

env = gym.make('CartPole-v0')
agent = PlaybackAgent(HDF5Reader('cartpole.h5'))

while agent.next_episode():
    env.reset()
    done = False
    while not done and not agent.done:
        obs, reward, done, info = env.step(agent.act())
        print(obs, reward, done, info)

env.close()

Random Environment

A RandomEnv will generate random observations and rewards.
#!/usr/bin/env python
"""Example of RandomEnv environment that generates random observations and rewards."""
# pylint: disable=invalid-name

from gym.spaces import Discrete
from gymbag import RandomEnv


env = RandomEnv(observation_space=Discrete(10), action_space=Discrete(2), episode_steps=10)

for episode in range(2):
    env.reset()
    done = False
    while not done:
        obs, reward, done, info = env.step(env.action_space.sample())
        print(obs, reward, done, info)

env.close()

Metadata

Reading file-level metadata.
#!/usr/bin/env python
"""Gymbag metadata example.  Uses cartpole data from record.py."""
# pylint: disable=invalid-name

from gymbag import HDF5Reader

reader = HDF5Reader('cartpole.h5')
print('Table:', reader.name)
print('Description:', reader.description)
print('Total Steps:', reader.nsteps)
print('Observation Space:', reader.observation_space)
print('Action Space:', reader.action_space)
reader.close()