How to use the gym.Env function in gym

To help you get started, we’ve selected a few gym examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sohamghosh121 / PacmanGym / gym_pacman / envs / pacman_env.py View on Github external
ROTATION_ANGLES = [0, 180, 90, 270]

MAX_EP_LENGTH = 100

import os
fdir = '/'.join(os.path.split(__file__)[:-1])
print(fdir)
layout_params = json.load(open(fdir + '/../../layout_params.json'))

print("Layout parameters")
print("------------------")
for k in layout_params:
    print(k,":",layout_params[k])
print("------------------")

class PacmanEnv(gym.Env):
    layouts = [
        'capsuleClassic', 'contestClassic', 'mediumClassic', 'mediumGrid', 'minimaxClassic', 'openClassic', 'originalClassic', 'smallClassic', 'capsuleClassic', 'smallGrid', 'testClassic', 'trappedClassic', 'trickyClassic'
    ]

    noGhost_layouts = [l + '_noGhosts' for l in layouts]

    MAX_MAZE_SIZE = (7, 7)
    num_envs = 1

    observation_space = spaces.Box(low=0, high=255,
            shape=(84, 84, 3), dtype=np.uint8)

    def __init__(self):
        self.action_space = spaces.Discrete(4) # up, down, left right
        self.display = PacmanGraphics(1.0)
        self._action_set = range(len(PACMAN_ACTIONS))
github NervanaSystems / distiller / examples / automated_deep_compression / ADC.py View on Github external
# Train for zero or more epochs
        opt_cfg = self.app_args.optimizer_data
        optimizer = torch.optim.SGD(self.model.parameters(), lr=opt_cfg['lr'],
                                    momentum=opt_cfg['momentum'], weight_decay=opt_cfg['weight_decay'])
        compression_scheduler = self.create_scheduler()
        acc_list = []
        for _ in range(num_epochs):
            # Fine-tune the model
            accuracies = self.services.train_fn(model=self.model, compression_scheduler=compression_scheduler,
                                                optimizer=optimizer, epoch=episode)
            acc_list.extend(accuracies)
        del compression_scheduler
        return acc_list


class DistillerWrapperEnvironment(gym.Env):
    def __init__(self, model, app_args, amc_cfg, services):
        self.pylogger = distiller.data_loggers.PythonLogger(msglogger)
        self.tflogger = distiller.data_loggers.TensorBoardLogger(msglogger.logdir)
        self.orig_model = model
        self.app_args = app_args
        self.amc_cfg = amc_cfg
        self.services = services
        self.net_wrapper = NetworkWrapper(model, app_args, services)
        self.dense_model_macs, self.dense_model_size = self.net_wrapper.get_model_resources_requirements(model)

        self.reset(init_only=True)
        msglogger.info("Model %s has %d Convolution layers", self.app_args.arch, self.net_wrapper.num_layers())
        msglogger.info("\tTotal MACs: %s" % distiller.pretty_int(self.dense_model_macs))
        log_amc_config(amc_cfg)

        self.episode = 0
github facebookresearch / habitat-api / habitat / core / env.py View on Github external
self._config.defrost()
        self._config.SIMULATOR = self._task.overwrite_sim_config(
            self._config.SIMULATOR, self.current_episode
        )
        self._config.freeze()

        self._sim.reconfigure(self._config.SIMULATOR)

    def render(self, mode="rgb") -> np.ndarray:
        return self._sim.render(mode)

    def close(self) -> None:
        self._sim.close()


class RLEnv(gym.Env):
    r"""Reinforcement Learning (RL) environment class which subclasses ``gym.Env``.

    This is a wrapper over `Env` for RL users. To create custom RL
    environments users should subclass `RLEnv` and define the following
    methods: `get_reward_range()`, `get_reward()`, `get_done()`, `get_info()`.

    As this is a subclass of ``gym.Env``, it implements `reset()` and
    `step()`.
    """

    _env: Env

    def __init__(
        self, config: Config, dataset: Optional[Dataset] = None
    ) -> None:
        """Constructor
github codescv / nesgym / src / nesgym / nesenv.py View on Github external
'79': (168, 189, 120),
    '7A': (126, 180, 141),
    '7B': (132, 189, 153),
    '7C': (117, 189, 180),
    '7D': (147, 147, 147),
    '7E': (0, 0, 0),
    '7F': (0, 0, 0),
}

palette_rgb = np.array([rgb[key] for key in sorted(rgb.keys())])
palette_grayscale = np.array([(0.299*r+0.587*g+0.114*b) / 256.0 for r, g, b in palette_rgb])

SCREEN_WIDTH = 256
SCREEN_HEIGHT = 224

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

    def __init__(self, **kwargs):
        utils.EzPickle.__init__(self)
        self.curr_seed = 0
        self.screen = np.zeros((SCREEN_HEIGHT, SCREEN_WIDTH, 3), dtype=np.uint8)
        self.closed = False
        self.can_send_command = True
        self.command_cond = Condition()
        self.viewer = None
        self.reward = 0
        episode_time_length_secs = 7
        frame_skip = 5
        fps = 60
        self.episode_length = episode_time_length_secs * fps / frame_skip
github microsoft / CNTK / Examples / ReinforcementLearning / deeprl / env / maze2d.py View on Github external
# Copyright (c) Microsoft. All rights reserved.

# Licensed under the MIT license. See LICENSE.md file in the project root
# for full license information.
# ==============================================================================

import gym
import numpy as np
from gym import spaces
from gym.utils import seeding


class Maze2D(gym.Env):
    """This class creates a maze problem given a map."""

    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second': 30
    }

    def __init__(self):
        self._load_map()
        self.viewer = None
        self.action_space = spaces.Discrete(4)
        self.observation_space = spaces.Discrete(self.room_lengths[0] *
                                                 self.room_lengths[1])
        self._seed()
        self._reset()
github harry-uglow / Deep-RL-Sim2Real / envs / Reach2DEnv.py View on Github external
# Python imports
import numpy as np
import pygame
from gym import spaces, Env


class Reach2DEnv(Env):
    """
    Simple 2D environment with a three jointed arm. Useful for quick demonstrations.
    """
    def render(self, mode='human'):
        # Check for mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                break
        self.draw_current_state(self.joint_angles, self.target_pose)

    observation_space = spaces.Box(np.array([0, 0, 0, 0, 0]),
                                   np.array([1, 1, 1, 1, 1]),
                                   dtype=np.float32)
    action_space = spaces.Box(np.array([0, 0, 0]),
                              np.array([1, 1, 1]), dtype=np.float32)
    timestep = 0
github DwangoMediaVillage / chainer_spiral / chainer_spiral / environments / mypaint_env.py View on Github external
import logging
import os

import gym
import numpy as np
from gym import spaces

from lib import brush, mypaintlib, pixbufsurface, tiledsurface


class MyPaintEnv(gym.Env):
    """ Open AI Gym of MyPaint """
    action_space = None
    observation_space = None
    reward_range = None
    viewer = None

    metadata = {
        'render.modes': ['human', 'rgb_array'],
    }

    def __init__(self,
                 logger=None,
                 imsize=64,
                 bg_color=None,
                 max_episode_steps=10,
                 pos_resolution=32,
github notadamking / Stock-Trading-Environment / env / StockTradingEnv.py View on Github external
import json
import gym
from gym import spaces
import pandas as pd
import numpy as np

MAX_ACCOUNT_BALANCE = 2147483647
MAX_NUM_SHARES = 2147483647
MAX_SHARE_PRICE = 5000
MAX_OPEN_POSITIONS = 5
MAX_STEPS = 20000

INITIAL_ACCOUNT_BALANCE = 10000


class StockTradingEnv(gym.Env):
    """A stock trading environment for OpenAI gym"""
    metadata = {'render.modes': ['human']}

    def __init__(self, df):
        super(StockTradingEnv, self).__init__()

        self.df = df
        self.reward_range = (0, MAX_ACCOUNT_BALANCE)

        # Actions of the format Buy x%, Sell x%, Hold, etc.
        self.action_space = spaces.Box(
            low=np.array([0, 0]), high=np.array([3, 1]), dtype=np.float16)

        # Prices contains the OHCL values for the last five prices
        self.observation_space = spaces.Box(
            low=0, high=1, shape=(6, 6), dtype=np.float16)
github flow-project / flow / flow / models / macro / lwr.py View on Github external
u = np.insert(np.append(u, u_right), 0, u_left)

    # Godunov numerical flux
    f = Gflux(u, V, R)

    fm = np.insert(f[0:len(f) - 1], 0, f[0])

    # Godunov scheme  (updating u)
    u = u - lam * (f - fm)

    u = np.insert(np.append(u[1:len(u) - 1], u_right), 0, u_left)

    return u[1:len(u) - 1]


class LWR(gym.Env):
    """ Create an rl environment to train and test the lwr model

    Attributes
    ----------
    init : array_like
        density data to be analyzed.
        Note: at time = 0, init = initial density data
    boundary : double
        left boundary condition
    obs : array_like
        the most recent observation
    """

    def __init__(self, initial_conditions, boundary_left):
        """Initialize the LWR model.
github mrahtz / learning-from-human-preferences / gym-gridworld / gym_gridworld / envs / gridworld_env.py View on Github external
import gym
from gym import spaces
from gym.utils import seeding
import numpy as np


class ALE:
    def __init__(self):
        self.lives = lambda: 0


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

    def __init__(self):
        self.size = [2, 2]
        self.observation_space = spaces.Box(low=0,
                                            high=255,
                                            shape=(210, 160, 3))
        self.centre = np.array([80, 105])
        self.action_space = spaces.Discrete(5)
        self.viewer = None

        self.ale = ALE()
        seed = None
        self.np_random, seed1 = seeding.np_random(seed)
        self.random_start = True
        self.maxsteps = 1000