Exemplo n.º 1
0
 def reset_recorder(self, record_path: Path):
     record_path.mkdir(parents=True, exist_ok=True)
     print(f'Recording video to {record_path}.mp4')
     video_recorder = VideoRecorder(
         env=self,
         base_path=str(record_path),
         enabled=True,
     )
     closer.Closer().register(video_recorder)
     return video_recorder
Exemplo n.º 2
0
import logging
logger = logging.getLogger(__name__)

import numpy as np

from gym import error
from gym.utils import closer

env_closer = closer.Closer()

# Env-related abstractions

class Env(object):
    """The main OpenAI Gym class. It encapsulates an environment with
    arbitrary behind-the-scenes dynamics. An environment can be
    partially or fully observed.

    The main API methods that users of this class need to know are:

        step
        reset
        render
        close
        seed

    When implementing an environment, override the following methods
    in your subclass:

        _step
        _reset
        _render
Exemplo n.º 3
0
import pipes
import sys
import threading
import uuid

import docker
import six.moves.urllib.parse as urlparse
from gym.utils import closer
from universe import error
from universe.remotes import healthcheck, remote
from universe import error, utils
from universe.remotes.compose import container, log_printer, progress_stream

logger = logging.getLogger(__name__)

docker_closer = closer.Closer()


def random_alphanumeric(length=14):
    buf = []
    while len(buf) < length:
        entropy = base64.encodestring(uuid.uuid4().bytes).decode('utf-8')
        bytes = [c for c in entropy if c.isalnum()]
        buf += bytes
    return ''.join(buf)[:length]


def pretty_command(command):
    return ' '.join(pipes.quote(c) for c in command)

Exemplo n.º 4
0
    for file in files:
        os.unlink(file)


def capped_cubic_video_schedule(episode_id):
    if episode_id < 1000:
        return int(round(episode_id**(1. / 3)))**3 == episode_id
    else:
        return episode_id % 1000 == 0


def disable_videos(episode_id):
    return False


monitor_closer = closer.Closer()


# This method gets used for a sanity check in scoreboard/api.py. It's
# not intended for use outside of the gym codebase.
def _open_monitors():
    return list(monitor_closer.closeables.values())


def load_env_info_from_manifests(manifests, training_dir):
    env_infos = []
    for manifest in manifests:
        with open(manifest) as f:
            contents = json.load(f)
            env_infos.append(contents['env_info'])
Exemplo n.º 5
0
import base64
import gzip
import json
import numpy as np
import pickle

import gym
from gym.utils import closer

recorder_closer = closer.Closer()


class DemonstrationRecorder(object):
    def __init__(self, env, file_name):
        self._id = recorder_closer.register(self)
        self.env = env
        self.i = 0

        self.file = gzip.GzipFile(file_name, 'w')
        self.file.write(json.dumps({'metadata': True, 'env_id': env.spec.id}))
        self.file.write("\n")

    def record_step(self, observation, reward, done, info, action):
        observation = self.env.observation_space.to_jsonable(observation)
        self.file.write(
            json.dumps({
                'i': 0,
                'action': action,
                'reward': reward,
                'done': done,
                'observation': observation,
Exemplo n.º 6
0
import logging
import gym
from gym.utils import closer
from gym_recording.recording import TraceRecording
logger = logging.getLogger(__name__)

__all__ = ['TraceRecordingWrapper']

trace_record_closer = closer.Closer()


class TraceRecordingWrapper(gym.Wrapper):
    """

    A Wrapper that records a trace of every action, observation, and reward generated by an environment.
    For an episode of length N, this will consist of:
      actions [0..N]
      observations [0..N+1]. Including the initial observation from `env.reset()`
      rewards [0..N]

    Usage:

      from gym_recording.wrappers import TraceRecordingWrapper
      if args.record_trace:
        env = TraceRecordingWrapper(env, '/tmp/mytraces')

    It'll save a numbered series of json-encoded files, with large arrays stored in binary, along
    with a manifest in /tmp/mytraces/openaigym.traces.*.
    See gym_recording.recording for more on the file format

    Later you can load the recorded traces:
Exemplo n.º 7
0
import json
import struct
import time

from gym.utils import atomic_write, closer

fbs_closer = closer.Closer()


class FBSWriter(object):
    def __init__(self, path):
        self._closed = False

        self.start = None
        self.stop = None

        self._id = fbs_closer.register(self)

        self.file = open(path, 'wb')
        # custom format: exactly the same as FBS 001.000 except:
        #
        # FBS 001.002
        # {line-of-json}
        # [length-byte, data, timestamp]...
        # \0\0\0\0 {line-of-json}
        self.file.write(b'FBS 001.002\n')

    def write(self, data):
        # Format:
        #
        # length