def _register_vnc(self, address, start_time=None): if start_time is None: start_time = time.time() host, port = host_port(address, default_port=5900) while True: # In VNC, the server sends bytes upon connection sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((host, port)) except (socket.error, socket.gaierror) as e: # ECONNREFUSED: VNC env hasn't come up yet # ETIMEDOUT: the packets can't be delivered yet, such as can happen on kubernetes # gaierror: can't resolve the address yet, which can also happen on kubernetes expected = socket.errno.ECONNREFUSED == e.errno or socket.errno.ETIMEDOUT == e.errno or isinstance(e, socket.gaierror) if self.start_timeout is None or not expected: reraise(suffix='while connecting to VNC server {}'.format(address)) logger.info('VNC server %s did not come up yet (error: %s). Sleeping for 1s.', address, e) time.sleep(1) else: break if time.time() - start_time > self.start_timeout: raise error.Error('VNC server {} did not come up within {}s'.format(address, self.start_timeout)) self.sockets[sock] = ('vnc', address)
def _register_rewarder(self, address, start_time=None): if start_time is None: start_time = time.time() host, port = host_port(address, default_port=15900) while True: # In WebSockets, the server sends bytes once we've upgraded the protocol sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((host, port)) except (socket.error, socket.gaierror) as e: # ECONNREFUSED: VNC env hasn't come up yet # ETIMEDOUT: the packets can't be delivered yet, such as can happen on kubernetes # gaierror: can't resolve the address yet, which can also happen on kubernetes expected = socket.errno.ECONNREFUSED == e.errno or socket.errno.ETIMEDOUT == e.errno or isinstance(e, socket.gaierror) if self.start_timeout is None or not expected: reraise(suffix='while connecting to Rewarder server {}'.format(address)) logger.info('Rewarder server %s did not come up yet (error: %s). Sleeping for 1s.', address, e) time.sleep(1) else: break if time.time() - start_time > self.start_timeout: raise error.Error('Rewarder server {} did not come up within {}s'.format(address, self.start_timeout)) # Send a websocket handshake. # https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers # # The port 10003 is an arbitrary port that we don't actually connect to, but needs to be a valid part # e.g Host: 127.0.0.1:GARBAGE results in the following error: (invalid port 'GARBAGE' in HTTP Host header '127.0.0.1:GARBAGE') sock.send(b'GET / HTTP/1.1\r\nHost: 127.0.0.1:10003\r\nUpgrade: WebSocket\r\nConnection:Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\nauthorization: ' + utils.basic_auth_encode('openai').encode('utf-8') + b'\r\nopenai-observer: true\r\n\r\n') self.sockets[sock] = ('rewarder', address)
def _register_from_config_yaml(self, id): if '.' in id: collection = id[:id.rfind('.')] elif self._default_collection is not None: collection = self._default_collection else: raise error.Error('Could not determine collection from ID, and no default_collection set: {}'.format(id)) try: collection_info = self._collections[collection] except KeyError: raise error.UnregisteredCollection('Could not load requested id={}. That belongs to the {} collection, but this runtime supports {}. Perhaps that was a typo, or you meant to use a different runtime?'.format(id, collection, ', '.join(self._collections.keys()))) srcdir = collection_info['srcdir'] default_task = collection_info['default_task'] path = os.path.abspath(os.path.join(srcdir, id, 'config.yml')) if not os.path.exists(path): raise error.UnregisteredEnv('Could not load spec for {}: no such file {}'.format(id, path), path) with open(path) as f: data = yaml.load(f) try: spec = data['spec'] except KeyError: reraise(suffix='while examining data from {}'.format(path)) constructor_name = spec.pop('type', default_task) constructor = utils.load(constructor_name) spec.pop('id', None) task = constructor(id=id, **spec) self._tasks[id] = task
def connect(self, i, name, vnc_address, rewarder_address, vnc_password=None, rewarder_password=None): logger.info('[%s] Connecting to environment: vnc://%s password=%s. If desired, you can manually connect a VNC viewer, such as TurboVNC. Most environments provide a convenient in-browser VNC client: http://%s/viewer/?password=%s', name, vnc_address, vnc_password, rewarder_address, vnc_password) extra_logger.info('[%s] Connecting to environment details: vnc_address=%s vnc_password=%s rewarder_address=%s rewarder_password=%s', name, vnc_address, vnc_password, rewarder_address, rewarder_password) self.connection_names[i] = name self.connection_labels[i] = '{}:{}'.format(name, vnc_address) if self.vnc_session is not None: kwargs = { 'name': name, 'address': vnc_address, 'password': vnc_password, } kwargs.update(self.vnc_kwargs) try: self.vnc_session.connect(**kwargs) except TypeError as e: reraise(suffix="(HINT: this error was while passing arguments to the VNCSession driver: {})".format(kwargs)) # TODO: name becomes index:pod_id # TODO: never log index, just log name if self.rewarder_session is not None: if self.spec is not None: env_id = self.spec.id else: env_id = None if self._seed_value is not None: # Once we use a seed, we clear it so we never # accidentally reuse the seed. Seeds are an advanced # feature and aren't supported by most envs in any # case. seed = self._seed_value[i] self._seed_value[i] = None else: seed = None assert rewarder_password, "Missing rewarder password: {}".format(rewarder_password) network = self.rewarder_session.connect( name=name, address=rewarder_address, seed=seed, env_id=env_id, fps=self.metadata['video.frames_per_second'], password=rewarder_password, label=self.connection_labels[i], start_timeout=self.remote_manager.start_timeout, observer=self._observer, skip_network_calibration=self._skip_network_calibration ) else: network = None if self.diagnostics is not None: self.diagnostics.connect(i, network, label=name) self.crashed.pop(i, None)
def load(cls, src_dir): spec_file = os.path.join(src_dir, 'vexpect.yml') with open(spec_file) as f: vexpect = yaml.load(f) # Just want a mutable message to indicate what was happening statehack = Dummy() statehack.message = 'getting started' try: states, transitions = cls._load_data(src_dir, vexpect, statehack) except Exception: reraise(suffix='(while {} in {})'.format(statehack.message, spec_file)) timeout = vexpect.get('timeout') vexpect = cls(states, transitions, timeout=timeout) return vexpect
def load(cls, src_dir, state_name, spec): spec = spec.copy() type = spec.pop('type') try: if type == 'ImageMatchState': return ImageMatchState(src_dir=src_dir, state_name=state_name, **spec) if type == 'MaskState': return MaskState.load(src_dir=src_dir, state_name=state_name, **spec) else: raise error.Error('Bad state type: {}'.format(type)) except error.Error: raise except: reraise(suffix='(while applying: state_name={} spec={})'.format( state_name, spec))
def configure(self, *args, **kwargs): """Provides runtime configuration to the environment. This configuration should consist of data that tells your environment how to run (such as an address of a remote server, or path to your ImageNet data). It should not affect the semantics of the environment. """ self._configured = True try: self._configure(*args, **kwargs) except TypeError as e: # It can be confusing if you have the wrong environment # and try calling with unsupported arguments, since your # stack trace will only show core.py. if self.spec: reraise(suffix='(for {})'.format(self.spec.id)) else: raise
def sanity_check_dependencies(): hint = ' is a dependency of `gym` that should have been installed for you. If you directly cloned the GitHub repo, please run `pip install -r requirements.txt` first.' try: import numpy except ImportError as e: reraise(prefix='Failed to import package `numpy`', suffix='HINT: `numpy`' + hint) try: import requests except ImportError as e: reraise(prefix='Failed to import package `requests`', suffix='HINT: `requests`' + hint) try: import six except ImportError as e: reraise(prefix='Failed to import package `six`', suffix='HINT: `six`' + hint) if distutils.version.StrictVersion( numpy.__version__) < distutils.version.StrictVersion('1.10.4'): raise error.DependencyNotInstalled( 'You have `numpy` version {} installed, but gym requires at least 1.10.4. HINT: If you directly cloned the GitHub repo, please run `pip install -r requirements.txt` first.' .format(numpy.__version__)) if distutils.version.StrictVersion( requests.__version__) < distutils.version.StrictVersion('2.0'): raise error.MujocoDependencyError( 'You have `requests` version {} installed, but gym requires at least 2.0. HINT: If you directly cloned the GitHub repo, please run `pip install -r requirements.txt` first.' .format(requests.__version__))
def sanity_check_dependencies(): hint = ' is a dependency of `gym` that should have been installed for you. If you directly cloned the GitHub repo, please run `pip install -r requirements.txt` first.' try: import numpy except ImportError as e: reraise(prefix='Failed to import package `numpy`',suffix='HINT: `numpy`'+hint) try: import requests except ImportError as e: reraise(prefix='Failed to import package `requests`',suffix='HINT: `requests`'+hint) try: import six except ImportError as e: reraise(prefix='Failed to import package `six`',suffix='HINT: `six`'+hint) if distutils.version.StrictVersion(numpy.__version__) < distutils.version.StrictVersion('1.10.4'): raise error.DependencyNotInstalled('You have `numpy` version {} installed, but gym requires at least 1.10.4. HINT: If you directly cloned the GitHub repo, please run `pip install -r requirements.txt` first.'.format(numpy.__version__)) if distutils.version.StrictVersion(requests.__version__) < distutils.version.StrictVersion('2.0'): raise error.MujocoDependencyError('You have `requests` version {} installed, but gym requires at least 2.0. HINT: If you directly cloned the GitHub repo, please run `pip install -r requirements.txt` first.'.format(requests.__version__))
import six import sys if "Apple" in sys.version: if 'DYLD_FALLBACK_LIBRARY_PATH' in os.environ: os.environ['DYLD_FALLBACK_LIBRARY_PATH'] += ':/usr/lib' # (JDS 2016/04/15): avoid bug on Anaconda 2.3.0 / Yosemite from gym.utils import reraise from gym import error try: import pyglet except ImportError as e: reraise( suffix= "HINT: you can install pyglet directly via 'pip install pyglet'. But if you really just want to install all Gym dependencies and not have to think about it, 'pip install -e .[all]' or 'pip install gym[all]' will do it." ) try: from pyglet.gl import * except ImportError as e: reraise( prefix="Error occured while running `from pyglet.gl import *`", suffix= "HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'" ) import math import numpy as np RAD2DEG = 57.29577951308232
2D rendering framework """ from __future__ import division import os, sys if "Apple" in sys.version: if 'DYLD_FALLBACK_LIBRARY_PATH' in os.environ: os.environ['DYLD_FALLBACK_LIBRARY_PATH'] += ':/usr/lib' # (JDS 2016/04/15): avoid bug on Anaconda 2.3.0 / Yosemite from gym.utils import reraise from gym import error try: import pyglet except ImportError as e: reraise(suffix="HINT: you can install pyglet directly via 'pip install pyglet'. But if you really just want to install all Gym dependencies and not have to think about it, 'pip install -e .[all]' or 'pip install gym[all]' will do it.") try: from pyglet.gl import * except ImportError as e: reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") import math import numpy as np RAD2DEG = 57.29577951308232 class Viewer(object): def __init__(self, width, height): self.width = width self.height = height
""" from datetime import datetime import math import os from random import randint from time import strftime from gym.utils import reraise import numpy as np from PIL import Image try: import pyglet except ImportError as error: reraise( suffix="Install pyglet with 'pip install pyglet'. If you want to just " "install all Gym dependencies, run 'pip install -e .[all]' or " "'pip install gym[all]'.") try: from pyglet.gl import * LAYER_BACKGROUND = pyglet.graphics.OrderedGroup(0) LAYER_FOREGROUND = pyglet.graphics.OrderedGroup(1) LAYER_TOP = pyglet.graphics.OrderedGroup(2) except pyglet.canvas.xlib.NoSuchDisplayException as error: print("Import error NSDE! You will not be able to render --> %s" % error) except ImportError as error: print("Import error GL! You will not be able to render --> %s" % error) from . import constants from . import utility
""" from datetime import datetime import math import os from random import randint from time import strftime from gym.utils import reraise import numpy as np from scipy.misc import imresize as resize try: import pyglet except ImportError as e: reraise( suffix="Install pyglet with 'pip install pyglet'. If you want to just " "install all Gym dependencies, run 'pip install -e .[all]' or " "'pip install gym[all]'.") try: from pyglet.gl import * layer_background = pyglet.graphics.OrderedGroup(0) layer_foreground = pyglet.graphics.OrderedGroup(1) layer_top = pyglet.graphics.OrderedGroup(2) except pyglet.canvas.xlib.NoSuchDisplayException as e: print("Import error! You will not be able to render --> %s" % e) except ImportError as e: reraise( prefix="Error occured while running `from pyglet.gl import *`", suffix="Make sure you have OpenGL installed. On Ubuntu, you can run " "'apt-get install python-opengl'. If you're running on a server, you " "may need a virtual frame buffer; something like this should work: "
from gym.utils import reraise from multiagent.rendering.attributes import Color, LineWidth try: from pyglet.gl import * except ImportError as e: reraise(prefix="Error occured while running `from pyglet.gl import *`", suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'." " If you're running on a server, you may need a virtual frame buffer; something like this should " "work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") class Geom(object): def __init__(self): self._color = Color((0, 0, 0, 1.0)) self.attrs = [self._color] def render(self): for attr in reversed(self.attrs): attr.enable() self.render1() for attr in self.attrs: attr.disable() def render1(self): raise NotImplementedError def add_attr(self, attr): self.attrs.append(attr)
from gym.utils import reraise import numpy as np try: import pyglet except ImportError as e: reraise( suffix= "HINT: you can install pyglet directly via 'pip install pyglet'. But if you really just want to install all Gym dependencies and not have to think about it, 'pip install -e .[all]' or 'pip install gym[all]' will do it." ) #try: # from pyglet.gl import * #except ImportError as e: # reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") class Pacman_SimpleImageViewer(object): def __init__(self, display=None, scale=5): self.window = None self.isopen = False self.display = display self.scale = scale def imshow(self, arr): height, width, _channels = arr.shape if self.window is None: self.window = pyglet.window.Window(width=self.scale * width * 4, height=self.scale * height * 4, display=self.display, vsync=False,