def __init__(self, name, horizon, gamma): """ Constructor. Args: name (str): gym id of the environment; horizon (int): the horizon; gamma (float): the discount factor. """ # MDP creation if '- ' + name in pybullet_envs.getList(): import pybullet pybullet.connect(pybullet.DIRECT) self.env = gym.make(name) self.env._max_episode_steps = np.inf # Hack to ignore gym time limit. # MDP properties assert not isinstance(self.env.observation_space, gym_spaces.MultiDiscrete) assert not isinstance(self.env.action_space, gym_spaces.MultiDiscrete) action_space = self._convert_gym_space(self.env.action_space) observation_space = self._convert_gym_space(self.env.observation_space) mdp_info = MDPInfo(observation_space, action_space, gamma, horizon) if isinstance(action_space, Discrete): self._convert_action = lambda a: a[0] else: self._convert_action = lambda a: a super().__init__(mdp_info)
def gym_env_creator(env_context: EnvContext, env_descriptor: str) -> gym.Env: """Tries to create a gym env given an EnvContext object and descriptor. Note: This function tries to construct the env from a string descriptor only using possibly installed RL env packages (such as gym, pybullet_envs, vizdoomgym, etc..). These packages are no installation requirements for RLlib. In case you would like to support more such env packages, add the necessary imports and construction logic below. Args: env_context: The env context object to configure the env. Note that this is a config dict, plus the properties: `worker_index`, `vector_index`, and `remote`. env_descriptor: The env descriptor, e.g. CartPole-v0, MsPacmanNoFrameskip-v4, VizdoomBasic-v0, or CartPoleContinuousBulletEnv-v0. Returns: The actual gym environment object. Raises: gym.error.Error: If the env cannot be constructed. """ # Allow for PyBullet or VizdoomGym envs to be used as well # (via string). This allows for doing things like # `env=CartPoleContinuousBulletEnv-v0` or # `env=VizdoomBasic-v0`. try: import pybullet_envs pybullet_envs.getList() except (ModuleNotFoundError, ImportError): pass try: import vizdoomgym vizdoomgym.__name__ # trick LINTer. except (ModuleNotFoundError, ImportError): pass # Try creating a gym env. If this fails we can output a # decent error message. try: return gym.make(env_descriptor, **env_context) except gym.error.Error: raise EnvError(ERR_MSG_INVALID_ENV_DESCRIPTOR.format(env_descriptor))
def __init__(self, name, horizon, gamma, wrappers=None, wrappers_args=None, **env_args): """ Constructor. Args: name (str): gym id of the environment; horizon (int): the horizon; gamma (float): the discount factor; wrappers (list): list of wrappers to apply over the environment. It is possible to pass arguments to the wrappers by providing a tuple with two elements: the gym wrapper class and a dictionary containing the parameters needed by the wrapper constructor; wrappers_args (list): list of list of arguments for each wrapper; ** env_args: other gym environment parameters. """ # MDP creation self._not_pybullet = True self._first = True if pybullet_found and '- ' + name in pybullet_envs.getList(): import pybullet pybullet.connect(pybullet.DIRECT) self._not_pybullet = False self.env = gym.make(name, **env_args) if wrappers is not None: if wrappers_args is None: wrappers_args = [dict()] * len(wrappers) for wrapper, args in zip(wrappers, wrappers_args): if isinstance(wrapper, tuple): self.env = wrapper[0](self.env, *args, **wrapper[1]) else: self.env = wrapper(self.env, *args, **env_args) self.env._max_episode_steps = np.inf # Hack to ignore gym time limit. # MDP properties assert not isinstance(self.env.observation_space, gym_spaces.MultiDiscrete) assert not isinstance(self.env.action_space, gym_spaces.MultiDiscrete) action_space = self._convert_gym_space(self.env.action_space) observation_space = self._convert_gym_space(self.env.observation_space) mdp_info = MDPInfo(observation_space, action_space, gamma, horizon) if isinstance(action_space, Discrete): self._convert_action = lambda a: a[0] else: self._convert_action = lambda a: a super().__init__(mdp_info)
def _get_bullet_env_list(): """Return a complete list of Bullet Gym environments. Returns: list: a list of bullet environment id (str) """ envs = [env.replace('- ', '') for env in pybullet_envs.getList()] # Hardcoded missing environment names from pybullet_envs.getList() envs.extend([ 'MinitaurExtendedEnv-v0', 'MinitaurReactiveEnv-v0', 'MinitaurBallGymEnv-v0', 'MinitaurTrottingEnv-v0', 'MinitaurStandGymEnv-v0', 'MinitaurAlternatingLegsEnv-v0', 'MinitaurFourLegStandEnv-v0', 'KukaDiverseObjectGrasping-v0' ]) return envs
def __init__(self, name, horizon, gamma, **kwargs): """ Constructor. Args: name (str): gym id of the environment; horizon (int): the horizon; gamma (float): the discount factor. """ # MDP creation self._close_at_stop = True if '- ' + name in pybullet_envs.getList(): import pybullet pybullet.connect(pybullet.DIRECT) self._close_at_stop = False self.env = gym.make(name) # set to 100Hz for pendulum if name == 'Pendulum-ID-v1' or name == 'Cartpole-ID-v1': self.env._max_episode_steps = 1000 self.env.unwrapped._dt = 0.01 self.env.unwrapped._sigma = 1e-4 self.env._max_episode_steps = np.inf # Hack to ignore gym time limit. # MDP properties assert not isinstance(self.env.observation_space, gym_spaces.MultiDiscrete) assert not isinstance(self.env.action_space, gym_spaces.MultiDiscrete) action_space = self._convert_gym_space(self.env.action_space) observation_space = self._convert_gym_space(self.env.observation_space) mdp_info = MDPInfo(observation_space, action_space, gamma, horizon) if isinstance(action_space, Discrete): self._convert_action = lambda a: a[0] else: self._convert_action = lambda a: a super().__init__(mdp_info)
def make_pybullet(_=None): import pybullet_envs import gym print("Successfully import pybullet and found: ", pybullet_envs.getList()) return gym.make(env_name)
"""Test module for BulletEnv""" import pickle import gym import pybullet_envs from pybullet_utils.bullet_client import BulletClient import pytest from garage.envs.bullet import _get_unsupported_env_list, BulletEnv from tests.helpers import step_env @pytest.mark.parametrize('env_ids', [pybullet_envs.getList()]) def test_can_step(env_ids): """Test Bullet environments can step""" for env_id in env_ids: # extract id string env_id = env_id.replace('- ', '') if env_id == 'KukaCamBulletEnv-v0': # Kuka environments calls py_bullet.resetSimulation() in reset() # unconditionally, which globally resets other simulations. So # only one Kuka environment is tested. continue if env_id in _get_unsupported_env_list(): pytest.skip('Skip unsupported Bullet environments') env = BulletEnv(env_name=env_id) ob_space = env.observation_space act_space = env.action_space env.reset()
def make_pybullet(_=None): import pybullet_envs import gym print("Successfully import pybullet and found: ", pybullet_envs.getList()) return gym.make("Walker2DBulletEnv-v0")
ray.init(address=os.environ["ip_head"], redis_password=redis_password) print(ray.available_resources()) print('*******************************************') print('*******************************************') print('*******************************************') print('*******************************************') print('*******************************************') print("Nodes in the Ray cluster:") print(ray.nodes()) from ray import tune import pybullet_envs print(pybullet_envs.getList()) from ray.tune.registry import register_env def make_pybullet(_=None): import pybullet_envs import gym print("Successfully import pybullet and found: ", pybullet_envs.getList()) return gym.make("Walker2DBulletEnv-v0") register_env("Walker2DBulletEnv-v0", make_pybullet) tune.run("PPO", config={
import argparse import numpy as np import gym from gym import spaces import pybullet_envs import torch from torch import distributions from torch import nn import pytorch_lightning as pl from lightning_baselines3.on_policy_models import PPO from lightning_baselines3.common.vec_env import make_vec_env, SubprocVecEnv pybullet_envs.getList() class Model(PPO): def __init__(self, lr=3e-4, hidden_size=64, **kwargs): super(Model, self).__init__(**kwargs) self.lr = lr actor = [ nn.Linear(self.observation_space.shape[0], hidden_size), nn.Tanh(), nn.Linear(hidden_size, hidden_size), nn.Tanh() ] if isinstance(self.action_space, spaces.Discrete): actor += [
def gym_env_creator(env_context: EnvContext, env_descriptor: str): """Tries to create a gym env given an EnvContext object and descriptor. Note: This function tries to construct the env from a string descriptor only using possibly installed RL env packages (such as gym, pybullet_envs, vizdoomgym, etc..). These packages are no installation requirements for RLlib. In case you would like to support more such env packages, add the necessary imports and construction logic below. Args: env_context (EnvContext): The env context object to configure the env. Note that this is a config dict, plus the properties: `worker_index`, `vector_index`, and `remote`. env_descriptor (str): The env descriptor, e.g. CartPole-v0, MsPacmanNoFrameskip-v4, VizdoomBasic-v0, or CartPoleContinuousBulletEnv-v0. Returns: gym.Env: The actual gym environment object. Raises: gym.error.Error: If the env cannot be constructed. """ import gym # Allow for PyBullet or VizdoomGym envs to be used as well # (via string). This allows for doing things like # `env=CartPoleContinuousBulletEnv-v0` or # `env=VizdoomBasic-v0`. try: import pybullet_envs pybullet_envs.getList() except (ModuleNotFoundError, ImportError): pass try: import vizdoomgym vizdoomgym.__name__ # trick LINTer. except (ModuleNotFoundError, ImportError): pass # Try creating a gym env. If this fails we can output a # decent error message. try: return gym.make(env_descriptor, **env_context) except gym.error.Error: error_msg = f"The env string you provided ('{env_descriptor}') is:" + \ """ a) Not a supported/installed environment. b) Not a tune-registered environment creator. c) Not a valid env class string. Try one of the following: a) For Atari support: `pip install gym[atari] atari_py`. For VizDoom support: Install VizDoom (https://github.com/mwydmuch/ViZDoom/blob/master/doc/Building.md) and `pip install vizdoomgym`. For PyBullet support: `pip install pybullet pybullet_envs`. b) To register your custom env, do `from ray import tune; tune.register('[name]', lambda cfg: [return env obj from here using cfg])`. Then in your config, do `config['env'] = [name]`. c) Make sure you provide a fully qualified classpath, e.g.: `ray.rllib.examples.env.repeat_after_me_env.RepeatAfterMeEnv` """ raise gym.error.Error(error_msg)
def test_envs_are_registered(self): for e, a in env_pairs: assert f'- {e}' in pybullet_envs.getList() assert a in envs.getList()