Пример #1
0
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__))
Пример #2
0
 def render(self, mode='human', close=False):
     try:
         import pygame
     except ImportError as e:
         raise error.DependencyNotInstalled(
             "{}. (HINT: install pygame using `pip install pygame`".format(
                 e))
     if close:
         pygame.quit()
     else:
         # update render
         man_img = pygame.image.load('man.png')
         man_img = pygame.transform.scale(man_img,
                                          (self.MAN_SIZE, self.MAN_SIZE))
         enemy_img = pygame.image.load('enemy.png')
         enemy_img = pygame.transform.scale(
             enemy_img, (self.ENEMY_SIZE, self.ENEMY_SIZE))
         if self.screen is None:
             pygame.init()
             self.screen = pygame.display.set_mode(
                 (self.PAD_WIDTH, self.PAD_HEIGHT))
         clock = pygame.time.Clock()
         self.screen.fill((255, 255, 255))
         self.screen.blit(man_img, (self.man_x, self.man_y))
         for enemy in self.enemies:
             self.screen.blit(enemy_img, enemy.getxy())
         font = pygame.font.SysFont(None, 30)
         text = font.render('Score: {}'.format(self.score), True, (0, 0, 0))
         self.screen.blit(text, (self.PAD_WIDTH / 2, 30))
         pygame.display.update()
         clock.tick(30)
Пример #3
0
    def __init__(self, output_path, frame_shape, frames_per_sec,
                 output_frames_per_sec):
        self.proc = None
        self.output_path = output_path
        # Frame shape should be lines-first, so w and h are swapped
        h, w, pixfmt = frame_shape
        if pixfmt != 3 and pixfmt != 4:
            raise error.InvalidFrame(
                "Your frame has shape {}, but we require (w,h,3) or (w,h,4), i.e., RGB values for a w-by-h image, with an optional alpha channel."
                .format(frame_shape))
        self.wh = (w, h)
        self.includes_alpha = pixfmt == 4
        self.frame_shape = frame_shape
        self.frames_per_sec = frames_per_sec
        self.output_frames_per_sec = output_frames_per_sec

        if distutils.spawn.find_executable("avconv") is not None:
            self.backend = "avconv"
        elif distutils.spawn.find_executable("ffmpeg") is not None:
            self.backend = "ffmpeg"
        else:
            raise error.DependencyNotInstalled(
                """Found neither the ffmpeg nor avconv executables. On OS X, you can install ffmpeg via `brew install ffmpeg`. On most Ubuntu variants, `sudo apt-get install ffmpeg` should do it. On Ubuntu 14.04, however, you'll need to install avconv with `sudo apt-get install libav-tools`."""
            )

        self.start()
Пример #4
0
    def render(self, mode='human', close=False):
        """
        This function renders the current game state in the given mode.
        TODO : render using default gym functions

        """
        if mode == 'console':
            print(self._get_state)
        elif mode == "human":
            try:
                import pygame
            except ImportError as e:
                raise error.DependencyNotInstalled(
                    "{}. (HINT: pygame install karo mere bhai; use `pip install pygame`".format(e))
            if close:
                pygame.quit()
            else:
                if self.screen is None:
                    pygame.init()
                    self.screen = pygame.display.set_mode(
                        (round(self.window_width), round(self.window_height)))
                    # surr = self.surr
                    
                clock = pygame.time.Clock()
                #trick to keep the screen running:
                pygame.event.pump()
                                
                self.screen.blit(self.bg, (0,0))

                # Draw car running
                # TODO : change the car orientation and better resize perhapes
                car_img_angle = pygame.transform.rotate(self.car_img, -self.angle)
                self.screen.blit(car_img_angle, (self.car))

                # show foodbox and delivery
                self.screen.blit(self.foodbox, (self.x1-30, self.y1-30))
                self.screen.blit(self.delivery, (self.x2-50, self.y2-50))

                self.screen.blit(self.surr, (1140,500))


                pygame.display.update()
                clock.tick(self.metadata["video.frames_per_second"])
                
                #trick to quit the screen
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()

                
        else:
            raise error.UnsupportedMode("Unsupported render mode: " + mode)
Пример #5
0
    def render(self, mode='human', close=False):
        """
        This function renders the current game state in the given mode.
        """

        if mode == 'console':
            print(self._get_game_state)
        elif mode == "human":
            try:
                import pygame
                from pygame import gfxdraw
            except ImportError as e:
                raise error.DependencyNotInstalled(
                    "{}. (HINT: install pygame using `pip install pygame`".format(e))
            if close:
                pygame.quit()
            else:
                if self.screen is None:
                    pygame.init()
                    self.screen = pygame.display.set_mode(
                        (round(self.window_width), round(self.window_height)))
                clock = pygame.time.Clock()

                self.screen.fill((255, 255, 255))
                # for line in self.lines:
                #    pygame.gfxdraw.line(self.screen, *line, BLACK)
                for key in self.resources:
                    self.resources[key].draw(pygame, self.screen)

                for key in self.spots:
                    self.spots[key].draw(pygame, self.screen)

                for key in self.roads:
                    self.roads[key].draw(pygame, self.screen)

                i = 0
                for key in self.collected_resources:
                    font = pygame.font.SysFont("arial", 25)
                    text = font.render(
                        str(key[:2])+str(self.collected_resources[key]), 3, (0, 0, 255))
                    self.screen.blit(text, (400, 20*i))
                    i += 1

                font = pygame.font.SysFont("arial", 25)
                text = font.render(str(self.episode), 3, (0, 0, 255))
                self.screen.blit(text, (10, 10))

                pygame.display.update()

        else:
            raise error.UnsupportedMode("Unsupported render mode: " + mode)
Пример #6
0
    def render(self, mode='human', close=False):
        """
    Renders the game to a window, or as an image.
    Override render_image() to define what's shown.
    Close is a hint that the window should b
    """
        try:
            import pygame
            from pygame import gfxdraw
        except ImportError as e:
            raise error.DependencyNotInstalled(
                "{}. (HINT: install pygame using `pip install pygame`".format(
                    e))
        if close:
            pygame.quit()

        background_colour = self._get_background_colour()
        self.screen.fill(background_colour)

        #img = self.render_image()
        #surface = pygame.surfarray.make_surface(img)
        #self.screen.blit(surface, (0, 0))  # Draw game state
        self.render_screen(self.screen)

        if mode == 'rgb_array':
            pass
        elif mode == 'human':
            #print('HUMAN')
            if self.display_screen is None:
                screen_shape = self.get_screen_shape()
                self.window_height = screen_shape[0]  # height
                self.window_width = screen_shape[1]  # width
                self.display_screen = pygame.display.set_mode(
                    (round(self.window_width), round(self.window_height)))
                pygame.display.set_caption(self._get_caption())

            # Update the window
            #surface = pygame.surfarray.make_surface(img)
            #self.screen.blit(surface, (0, 0))  # Draw game state
            self.display_screen.blit(self.screen, (0, 0))  # Draw game state
            pygame.display.update()
        else:
            raise error.UnsupportedMode("Unsupported render mode: " + mode)

        # Convert to numpy array
        rgb_array = pygame.surfarray.array3d(self.screen)
        #print("RENDER OBJ = ", type(rgb_array))
        #print("RENDER OBJ SHAPE = ", rgb_array.shape)
        return rgb_array
Пример #7
0
    def __init__(
        self,
        output_path: str,
        frame_shape: tuple[int, int, int],
        frames_per_sec: int,
        output_frames_per_sec: int,
    ):
        """Encoder for capturing image based frames of environment for Video Recorder.

        Args:
            output_path: The output data path
            frame_shape: The expected frame shape, a tuple of height, weight and channels (3 or 4)
            frames_per_sec: The number of frames per second the environment runs at
            output_frames_per_sec: The output number of frames per second for the video
        """
        self.proc = None
        self.output_path = output_path
        # Frame shape should be lines-first, so w and h are swapped
        h, w, pixfmt = frame_shape
        if pixfmt != 3 and pixfmt != 4:
            raise error.InvalidFrame(
                f"Your frame has shape {frame_shape}, but we require (w,h,3) or (w,h,4), "
                "i.e., RGB values for a w-by-h image, with an optional alpha channel."
            )
        self.wh = (w, h)
        self.includes_alpha = pixfmt == 4
        self.frame_shape = frame_shape
        self.frames_per_sec = frames_per_sec
        self.output_frames_per_sec = output_frames_per_sec

        if shutil.which("avconv") is not None:
            self.backend = "avconv"
        elif shutil.which("ffmpeg") is not None:
            self.backend = "ffmpeg"
        elif pkgutil.find_loader("imageio_ffmpeg"):
            import imageio_ffmpeg

            self.backend = imageio_ffmpeg.get_ffmpeg_exe()
        else:
            raise error.DependencyNotInstalled(
                "Found neither the ffmpeg nor avconv executables. "
                "On OS X, you can install ffmpeg via `brew install ffmpeg`. "
                "On most Ubuntu variants, `sudo apt-get install ffmpeg` should do it. "
                "On Ubuntu 14.04, however, you'll need to install avconv with `sudo apt-get install libav-tools`. "
                "Alternatively, please install imageio-ffmpeg with `pip install imageio-ffmpeg`"
            )

        self.start()
Пример #8
0
    def render(self, mode='human', close=False):
        """
        This function renders the current game state in the given mode.
        """
        if mode == 'console':
            print(self.board.blit(self.current_piece, ret=True))
        elif mode == "rgb_array":
            aux = self.board.blit(self.current_piece, ret=True).reshape([self.board.size[0], self.board.size[1], 1])
            return np.concatenate([aux for i in range(3)], axis=2)
        elif mode == "human":
            try:
                import pygame
                from pygame import gfxdraw
            except ImportError as e:
                raise error.DependencyNotInstalled(
                    "{}. (HINT: install pygame using `pip install pygame`".format(e))
            if close:
                pygame.quit()
            else:
                if self.screen is None:
                    pygame.init()
                    self.screen = pygame.display.set_mode(
                        (round(self.board.size[1]*10), round(self.board.size[0]*10)))
                clock = pygame.time.Clock()
                
                board = self.board.blit(self.current_piece, ret=True)
                # Draw old bubbles
                self.screen.fill((255, 255, 255))
                for row in range(board.shape[0]):
                    for column in range(board.shape[1]):
                        if board[row, column]:
                            pygame.draw.rect(self.screen, (0,0,0), pygame.Rect(column*10, row*10, 9, 9))
                            #pygame.box(self.screen, pygame.Rect(column*10, row*10, 9, 9), (0,0,0))

                pygame.display.update()
        else:
            raise error.UnsupportedMode("Unsupported render mode: " + mode)
        
        
        
        
        
        
        
        
        """if mode == 'human':
Пример #9
0
    def _step(self, action):
        try:
            table = pd.read_csv('/home/vtd/Documents/test.csv')
        except IOError as e:
            raise error.DependencyNotInstalled(
                '{}. (HINT: you may need to install the Go dependencies.)'.format(e))

        if action == 'first':
            reward = get_reward(self)
            next_state = None
            Done = None
            pass
        else:
            a_num = {'left': 0, 'right': 1, 'top': 2, 'down': 3,
                     'left_down': 4, 'right_down': 5, 'left_up': 6, 'right_up': 7}
            nxt_state = table.ix[a_num[action], self.state]
            reward = get_reward(self)
        return (nxt_state, reward, done)
        pass
Пример #10
0
    def _render(self, mode='human', close=False):
        if mode == 'rgb_array':
            return self.last_image
        elif mode == 'human':
            try:
                import pygame
            except ImportError as e:
                raise error.DependencyNotInstalled("{}. (HINT: install pygame using `pip install pygame`".format(e))

            if close:
                pygame.quit()
            else:
                if self.screen is None:
                    pygame.init()
                    self.screen = pygame.display.set_mode((self.video_width, self.video_height))
                img = pygame.surfarray.make_surface(self.last_image.swapaxes(0, 1))
                self.screen.blit(img, (0, 0))
                pygame.display.update()
        else:
            raise error.UnsupportedMode("Unsupported render mode: " + mode)
Пример #11
0
    def render(self, mode='human', close=False):
        if mode == 'console':
            raise NotImplementedError
        elif mode == "human":
            try:
                import pygame
            except ImportError as e:
                raise error.DependencyNotInstalled(
                    "{}. (HINT: install pygame using 'pip install pygame'".format(e)
                )
            if close:
                pygame.quit()
            else:
                if self.screen is None:
                    pygame.init()
                    self.screen = pygame.display.set_mode((self.s_width, self.s_height))
                    pygame.display.set_caption('Tetris')

                self.draw_window(self.screen, self.grid)
                self.draw_next_shape(self.next_piece, self.screen)
                pygame.display.update()
Пример #12
0
from gym import error
try:
    import pachi_py
except ImportError as e:
    # The dependency group [pachi] should match the name is setup.py.
    raise error.DependencyNotInstalled('{}. (HINT: you may need to install the Go dependencies via "pip install gym[pachi]".)'.format(e))

import numpy as np
import gym
from gym import spaces
from gym.utils import seeding
from six import StringIO
import sys
import six


# The coordinate representation of Pachi (and pachi_py) is defined on a board
# with extra rows and columns on the margin of the board, so positions on the board
# are not numbers in [0, board_size**2) as one would expect. For this Go env, we instead
# use an action representation that does fall in this more natural range.

def _pass_action(board_size):
    return board_size**2

def _resign_action(board_size):
    return board_size**2 + 1

def _coord_to_action(board, c):
    '''Converts Pachi coordinates to actions'''
    if c == pachi_py.PASS_COORD: return _pass_action(board.size)
    if c == pachi_py.RESIGN_COORD: return _resign_action(board.size)
Пример #13
0
from gym import error
try:
    import cv2
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (To use the human viewer you need to install `opencv-python` via pip, not conda)"
        .format(e))
import uuid
'''
Credit: https://github.com/zuoxingdong/dm2gym/blob/master/dm2gym/envs/opencv_image_viewer.py
'''


class OpenCVImageViewer():
    """A simple OpenCV highgui based dm_control image viewer
    This class is meant to be a drop-in replacement for
    `gym.envs.classic_control.rendering.SimpleImageViewer`
    """
    def __init__(self, *, escape_to_exit=True):
        """Construct the viewing window"""
        self._escape_to_exit = escape_to_exit
        self._window_name = str(uuid.uuid4())
        cv2.namedWindow(self._window_name, cv2.WINDOW_AUTOSIZE)
        self._isopen = True

    def __del__(self):
        """Close the window"""
        cv2.destroyWindow(self._window_name)
        self._isopen = False

    def imshow(self, img):
Пример #14
0
import os, subprocess, time, signal
import gym
from gym import error, spaces, utils
from gym.utils import seeding
from gym_snake.envs.snake import Controller, Discrete

try:
    import matplotlib.pyplot as plt
except ImportError as e:
    raise error.DependencyNotInstalled("{}. (HINT: see matplotlib documentation for installation https://matplotlib.org/faq/installing_faq.html#installation".format(e))

class SnakeExtraHardEnv(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self, grid_size=[25,25], unit_size=10, unit_gap=1, snake_size=5, n_snakes=3, n_foods=2, random_init=True):
        self.grid_size = grid_size
        self.unit_size = unit_size
        self.unit_gap = unit_gap
        self.snake_size = snake_size
        self.n_snakes = n_snakes
        self.n_foods = n_foods
        self.viewer = None
        self.action_space = Discrete(4)
        self.last_obs = None
        self.random_init = random_init

    def step(self, action):
        self.last_obs, rewards, done, info = self.controller.step(action)
        return self.last_obs, rewards, done, info

    def reset(self):
Пример #15
0
import os, subprocess, time, signal
import gym
from gym import error, spaces
from gym import utils
from gym.utils import seeding

try:
    import hfo_py
except ImportError as e:
    raise error.DependencyNotInstalled("{}. (HINT: you can install HFO dependencies with 'pip install gym[soccer].)'".format(e))

import logging
logger = logging.getLogger(__name__)

class AsayerEnv(gym.Env, utils.EzPickle):
    metadata = {'render.modes': ['human']}

    def __init__(self):
        self.viewer = None
        self.server_process = None
        self.server_port = None
        self.hfo_path = hfo_py.get_hfo_path()
        self._configure_environment()
        self.env = hfo_py.HFOEnvironment()
        self.env.connectToServer(config_dir=hfo_py.get_config_path())
        self.observation_space = spaces.Box(low=-1, high=1,
                                            shape=(self.env.getStateSize()))
        # Action space omits the Tackle/Catch actions, which are useful on defense
        self.action_space = spaces.Tuple((spaces.Discrete(3),
                                          spaces.Box(low=0, high=100, shape=1),
                                          spaces.Box(low=-180, high=180, shape=1),
Пример #16
0
import numpy as np
import os
import gym
from gym import error, spaces
from gym import utils

try:
    import atari_py
except ImportError:
    raise error.DependencyNotInstalled("{}. (HINT: you can install Atari dependencies with 'pip install gym[atari].)'")

import logging
logger = logging.getLogger(__name__)

def to_rgb(ale):
    (screen_width,screen_height) = ale.getScreenDims()
    arr = np.zeros((screen_height, screen_width, 4), dtype=np.uint8)
    ale.getScreenRGB(arr) # says rgb but actually bgr
    return arr[:,:,[2, 1, 0]].copy()

def to_ram(ale):
    ram_size = ale.getRAMSize()
    ram = np.zeros((ram_size),dtype=np.uint8)
    ale.getRAM(ram)
    return ram

class AtariEnv(gym.Env, utils.EzPickle):
    metadata = {'render.modes': ['human', 'rgb_array']}

    def __init__(self, game='pong', obs_type='ram'):
        utils.EzPickle.__init__(self, game, obs_type)
Пример #17
0
import gym
from gym import error, spaces, utils
from gym.utils import seeding

from random import randrange

import numpy as np

try:
    import chess
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}.  (HINT: see README for python-chess installation instructions".
        format(e))


class ChessEnv(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self):
        self.env = chess.Board()
        self.alt_moves = 0
        self.reward_lookup = {
            'check': 0.05,
            'mate': 100.0,
            'stalemate': 0.0,
            'p': 0.1,
            'n': 0.3,
            'b': 0.3,
            'r': 0.5,
            'q': 0.9,
Пример #18
0
import logging
import time
import os

import numpy as np
import json
import xml.etree.ElementTree as ET
import gym
from gym import spaces, error

try:
    import minecraft_py
    import MalmoPython
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (HINT: install minecraft_py from https://github.com/tambetm/minecraft-py"
        .format(e))

logger = logging.getLogger(__name__)

SINGLE_DIRECTION_DISCRETE_MOVEMENTS = [
    "jumpeast", "jumpnorth", "jumpsouth", "jumpwest", "movenorth", "moveeast",
    "movesouth", "movewest", "jumpuse", "use", "attack", "jump"
]

MULTIPLE_DIRECTION_DISCRETE_MOVEMENTS = [
    "move", "turn", "look", "strafe", "jumpmove", "jumpstrafe"
]


class MinecraftEnv(gym.Env):
Пример #19
0
import os
import copy
from gym import error
import numpy as np
import gym
from softgym.utils.visualization import save_numpy_as_gif
import cv2
import os.path as osp
import pickle

try:
    import pyflex
except ImportError as e:
    raise error.DependencyNotInstalled("{}. (You need to first compile the python binding)".format(e))


class FlexEnv(gym.Env):
    def __init__(self,
                 device_id=-1,
                 headless=False,
                 render=True,
                 horizon=100,
                 camera_width=720,
                 camera_height=720,
                 num_variations=1,
                 action_repeat=8,
                 camera_name='default_camera',
                 deterministic=True,
                 use_cached_states=True,
                 save_cached_states=True, **kwargs):
        self.camera_params, self.camera_width, self.camera_height, self.camera_name = {}, camera_width, camera_height, camera_name
Пример #20
0
import os
import signal
import socket
import subprocess
import time
from contextlib import closing

from gym import error

try:
    import hfo_py
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (Try 'pip install -e .' to install HFO dependencies.')".format(e))

import logging

logger = logging.getLogger(__name__)


def find_free_port():
    """Find a random free port. Does not guarantee that the port will still be free after return.
    Note: HFO takes three consecutive port numbers, this only checks one.

    Source: https://github.com/crowdAI/marLo/blob/master/marlo/utils.py

    :rtype:  `int`
    """

    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
        s.bind(('', 0))
Пример #21
0
import numpy as np
import os
import gym
from gym import error, spaces
from gym import utils
from gym.utils import seeding

try:
    import nes_le
    from nes_le.interface import NESLEInterface, show_image
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (HINT: you can install NES dependencies by running 'pip install gym[nes]'.)"
        .format(e))

import logging
logger = logging.getLogger(__name__)


class NESEnv(gym.Env, utils.EzPickle):
    metadata = {'render.modes': ['human', 'rgb_array']}

    def __init__(self,
                 game='super_mario_bros',
                 obs_type='image',
                 frameskip=(2, 5),
                 repeat_action_probability=0.):
        """Frameskip should be either a tuple (indicating a random range to
        choose from, with the top value exclude), or an int."""

        utils.EzPickle.__init__(self, game, obs_type)
Пример #22
0
from gym import error, spaces
from gym.utils import seeding
import numpy as np
from os import path
import gym
import six

from gym.envs.dart.static_window import *

try:
    import pydart2 as pydart
    from pydart2.gui.trackball import Trackball
    pydart.init()
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (HINT: you need to install pydart2.)".format(e))


class DartEnv(gym.Env):
    """Superclass for all Dart environments.
    """

    def __init__(self, model_paths, frame_skip, observation_size, action_bounds, \
                 dt=0.002, obs_type="parameter", action_type="continuous", visualize=True, disableViewer=False,\
                 screen_width=80, screen_height=45):
        assert obs_type in ('parameter', 'image')
        assert action_type in ("continuous", "discrete")
        print('pydart initialization OK')

        self.viewer = None
Пример #23
0
import numpy as np
import os
import gym
import cv2
cv2.ocl.setUseOpenCL(False)
import traceback
from gym import error, spaces
from gym import utils
from gym.utils import seeding

try:
    import atari_py
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (HINT: you can install Atari dependencies by running "
        "'pip install gym[atari]'.)".format(e))


def to_ram(ale):
    ram_size = ale.getRAMSize()
    ram = np.zeros((ram_size), dtype=np.uint8)
    ale.getRAM(ram)
    return ram


class AtariEnv(gym.Env, utils.EzPickle):
    metadata = {'render.modes': ['human', 'rgb_array']}

    def __init__(self,
                 mode,
                 game='pong',
Пример #24
0
    def xrange(*args, **kwargs):
        return range(*args, **kwargs)


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 import error

try:
    import pyglet
except ImportError as e:
    raise error.DependencyNotInstalled("""{}

(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:
    raise error.DependencyNotInstalled(
        """{} (while running: from pyglet.gl import *).

(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" <your script here>')"""
        .format(e))

import math
import numpy as np

RAD2DEG = 57.29577951308232
import xml.etree.ElementTree as ET
import gym
from gym import spaces, error
from minecraft_env.eating_env import *

reshape = False

try:
    import malmo.MalmoPython as MalmoPython
except ImportError as e:
    err = e
    try:
        import MalmoPython
    except ImportError:
        raise error.DependencyNotInstalled(
            "{}. Malmo doesn't seem to be installed."
            "Please install Malmo from GitHub or with \"pip3 install malmo\".".
            format(err))

logger = logging.getLogger(__name__)

SINGLE_DIRECTION_DISCRETE_MOVEMENTS = [
    "jumpeast", "jumpnorth", "jumpsouth", "jumpwest", "movenorth", "moveeast",
    "movesouth", "movewest", "jumpuse", "use", "attack", "jump"
]

MULTIPLE_DIRECTION_DISCRETE_MOVEMENTS = [
    "move", "turn", "look", "strafe", "jumpmove", "jumpstrafe"
]


class InvalidMissionFile(Exception):
Пример #26
0
import gym
from gym import error, spaces
from gym.utils import seeding

try:
    import agx
    import agxPython
    import agxCollide
    import agxOSG
    import agxIO
    import agxSDK
    import agxUtil
    import agxRender
except ImportError as e:
    raise error.DependencyNotInstalled("{}. (HINT: you need to install AGX Dynamics, "
                                       "have a valid license and run 'setup_env.bash'.)".format(e))

logger = logging.getLogger('gym_agx.envs')


class AgxEnv(gym.Env):
    """Superclass for all AGX Dynamics environments. Initializes AGX, loads scene from file and builds it."""
    metadata = {'render.modes': ['osg', 'debug']}

    def __init__(self, scene_path, n_substeps, n_actions, observation_type, image_size, camera_pose, no_graphics, args):
        """Initializes a AgxEnv object
        :param scene_path: path to binary file containing serialized simulation defined in sim/ folder
        :param n_substeps: number os simulation steps per call to step()
        :param n_actions: number of actions (DoF)
        :param camera_pose: dictionary containing EYE, CENTER, UP information for rendering
        :param args: arguments for agxViewer
Пример #27
0
import numpy as np
from gym import error
from pprint import pprint
from SumTree2 import SumTree
import nasim
from others import save_data
from torch.autograd import Variable
try:
    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torch.nn.functional as F
    from torch.utils.tensorboard import SummaryWriter
except ImportError as e:
    raise error.DependencyNotInstalled(
        f"{e}. (HINT: you can install dqn_agent dependencies by running "
        "'pip install nasim[dqn]'.)"
    )




class ReplayMemoryPER:
    e = 0.01
    a = 0.6
    beta = 0.4
    beta_increment_per_sampling = 0.001
    
    def __init__(self, capacity,device="cuda"):
        self.tree = SumTree(capacity)
        self.capacity = capacity
        self.device = device
Пример #28
0
    def __init__(
        self,
        model_path,
        frame_skip,
        render_mode: Optional[str] = None,
        mujoco_bindings="mujoco",
    ):
        if model_path.startswith("/"):
            fullpath = model_path
        else:
            fullpath = path.join(path.dirname(__file__), "assets", model_path)
        if not path.exists(fullpath):
            raise OSError(f"File {fullpath} does not exist")

        if mujoco_bindings == "mujoco_py":
            logger.warn(
                "This version of the mujoco environments depends "
                "on the mujoco-py bindings, which are no longer maintained "
                "and may stop working. Please upgrade to the v4 versions of "
                "the environments (which depend on the mujoco python bindings instead), unless "
                "you are trying to precisely replicate previous works).")
            try:
                import mujoco_py

                self._mujoco_bindings = mujoco_py
            except ImportError as e:
                raise error.DependencyNotInstalled(
                    "{}. (HINT: you need to install mujoco_py, and also perform the setup instructions here: https://github.com/openai/mujoco-py/.)"
                    .format(e))

            self.model = self._mujoco_bindings.load_model_from_path(fullpath)
            self.sim = self._mujoco_bindings.MjSim(self.model)
            self.data = self.sim.data

        elif mujoco_bindings == "mujoco":
            try:
                import mujoco

                self._mujoco_bindings = mujoco

            except ImportError as e:
                raise error.DependencyNotInstalled(
                    f"{e}. (HINT: you need to install mujoco)")
            self.model = self._mujoco_bindings.MjModel.from_xml_path(fullpath)
            self.data = self._mujoco_bindings.MjData(self.model)

        self.init_qpos = self.data.qpos.ravel().copy()
        self.init_qvel = self.data.qvel.ravel().copy()
        self._viewers = {}

        self.frame_skip = frame_skip

        self.viewer = None

        self.metadata = {
            "render_modes": [
                "human",
                "rgb_array",
                "depth_array",
                "single_rgb_array",
                "single_depth_array",
            ],
            "render_fps":
            int(np.round(1.0 / self.dt)),
        }

        self._set_action_space()

        assert render_mode is None or render_mode in self.metadata[
            "render_modes"]
        self.render_mode = render_mode
        self.renderer = Renderer(self.render_mode, self._render)

        action = self.action_space.sample()
        observation, _reward, done, _info = self.step(action)
        assert not done

        self._set_observation_space(observation)
Пример #29
0
import math
import colorsys
import numpy as np
import xml
import xml.etree.ElementTree as elt
import tempfile

from gym import utils, error
from envs.haptix import rooms_hand_env
from envs.haptix.utils import set_model_path

try:
    import mujoco_py
except ImportError as e:
    raise error.DependencyNotInstalled(
        "{}. (HINT: you need to install mujoco_py, and also perform the setup instructions here: https://github.com/openai/mujoco-py/.)"
        .format(e))

# objecttypes
OBJ_TYPES = ['box', 'cylinder', 'ellipsoid']

# (obj) material types
OBJ_MATERIALS = [
    "MatFoil", "MatPlane", "MatWood", "MatSquare", "MatWoodR", "MatWoodG",
    "MatWoodB"
]

# (wall) material types
WALL_MATERIALS = [
    "MatFoil", "MatPlane", "MatWood", "MatSquare", "MatWoodR", "MatWoodG",
    "MatWoodB"
Пример #30
0
    def render(self, mode='human', close=False):
        """
        This function renders the current game state in the given mode.
        """
        if mode == 'console':
            print(self._get_game_state)
        elif mode == "human":
            try:
                import pygame
                from pygame import gfxdraw
            except ImportError as e:
                raise error.DependencyNotInstalled(
                    "{}. (HINT: install pygame using `pip install pygame`".
                    format(e))
            if close:
                pygame.quit()
            else:
                if self.screen is None:
                    pygame.init()
                    self.screen = pygame.display.set_mode(
                        (round(self.window_width), round(self.window_height)))
                clock = pygame.time.Clock()

                # Draw old bubbles
                self.screen.fill((255, 255, 255))
                for row in range(self.array_height):
                    for column in range(self.array_width):
                        if self.last_board[row][column].color is not None:
                            bubble = self.last_board[row][column]
                            pygame.gfxdraw.filled_circle(
                                self.screen, round(bubble.center_x),
                                round(bubble.center_y), self.bubble_radius,
                                bubble.color)
                pygame.display.update()

                # Draw flying bubble
                last_x, last_y = None, None
                for position in self.last_positions:
                    if last_x is not None and last_y is not None:
                        pygame.gfxdraw.filled_circle(self.screen,
                                                     round(last_x),
                                                     round(last_y),
                                                     self.bubble_radius,
                                                     (255, 255, 255))
                    last_x, last_y = position[0], position[1]
                    pygame.gfxdraw.filled_circle(self.screen,
                                                 round(position[0]),
                                                 round(position[1]),
                                                 self.bubble_radius,
                                                 self.last_color)
                    pygame.display.update()
                    clock.tick(self.metadata["video.frames_per_second"])

                # Draw new bubbles
                self.screen.fill((255, 255, 255))
                for row in range(self.array_height):
                    for column in range(self.array_width):
                        if self.board[row][column].color is not None:
                            bubble = self.board[row][column]
                            pygame.gfxdraw.filled_circle(
                                self.screen, round(bubble.center_x),
                                round(bubble.center_y), self.bubble_radius,
                                bubble.color)
                pygame.display.update()
        else:
            raise error.UnsupportedMode("Unsupported render mode: " + mode)