本文整理匯總了Python中gym.wrappers方法的典型用法代碼示例。如果您正苦於以下問題:Python gym.wrappers方法的具體用法?Python gym.wrappers怎麽用?Python gym.wrappers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gym
的用法示例。
在下文中一共展示了gym.wrappers方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: make_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_env(args, seed, test):
if args.env.startswith('Roboschool'):
# Check gym version because roboschool does not work with gym>=0.15.6
from distutils.version import StrictVersion
gym_version = StrictVersion(gym.__version__)
if gym_version >= StrictVersion('0.15.6'):
raise RuntimeError('roboschool does not work with gym>=0.15.6')
import roboschool # NOQA
env = gym.make(args.env)
# Unwrap TimiLimit wrapper
assert isinstance(env, gym.wrappers.TimeLimit)
env = env.env
# Use different random seeds for train and test envs
env_seed = 2 ** 32 - 1 - seed if test else seed
env.seed(int(env_seed))
# Cast observations to float32 because our model uses float32
env = chainerrl.wrappers.CastObservationToFloat32(env)
# Normalize action space to [-1, 1]^n
env = chainerrl.wrappers.NormalizeActionSpace(env)
if args.monitor:
env = chainerrl.wrappers.Monitor(
env, args.outdir, force=True, video_callable=lambda _: True)
if args.render:
env = chainerrl.wrappers.Render(env, mode='human')
return env
示例2: remove_time_limit_wrapper
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def remove_time_limit_wrapper(env):
"""Removes top level TimeLimit Wrapper.
Removes TimeLimit Wrapper from top level if exists, throws error if any other
TimeLimit Wrapper is present in stack.
Args:
env: environment
Returns:
the env with removed time limit wrapper.
"""
if isinstance(env, gym.wrappers.TimeLimit):
env = env.env
env_ = env
while isinstance(env_, gym.Wrapper):
if isinstance(env_, gym.wrappers.TimeLimit):
raise ValueError("Can remove only top-level TimeLimit gym.Wrapper.")
env_ = env_.env
return env
示例3: make_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_env(env_id: str):
"""Make a basic gym environment, without any special wrappers.
Parameters
----------
env_id: str
The environment's id, e.g. 'FrozenLake-v0'.
Returns
-------
gym.Env
A gym environment.
"""
assert env_id in ENV_IDS
if not env_id in ENV_IDS_NON_GYM:
env = gym.make(env_id)
else:
if env_id == 'MazeWorld0-v0':
env = TimeLimit(MazeWorld(map_id=0), max_episode_steps=200)
elif env_id == 'MazeWorld1-v0':
env = TimeLimit(MazeWorld(map_id=1), max_episode_steps=200)
else:
raise NotImplementedError()
return env
示例4: make_robotics_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_robotics_env(env_id, seed, rank=0, allow_early_resets=True):
"""
Create a wrapped, monitored gym.Env for MuJoCo.
:param env_id: (str) the environment ID
:param seed: (int) the initial seed for RNG
:param rank: (int) the rank of the environment (for logging)
:param allow_early_resets: (bool) allows early reset of the environment
:return: (Gym Environment) The robotic environment
"""
set_global_seeds(seed)
env = gym.make(env_id)
keys = ['observation', 'desired_goal']
# TODO: remove try-except once most users are running modern Gym
try: # for modern Gym (>=0.15.4)
from gym.wrappers import FilterObservation, FlattenObservation
env = FlattenObservation(FilterObservation(env, keys))
except ImportError: # for older gym (<=0.15.3)
from gym.wrappers import FlattenDictWrapper # pytype:disable=import-error
env = FlattenDictWrapper(env, keys)
env = Monitor(
env, logger.get_dir() and os.path.join(logger.get_dir(), str(rank)),
info_keywords=('is_success',), allow_early_resets=allow_early_resets)
env.seed(seed)
return env
示例5: make_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_env(gym_id, seed, idx):
def thunk():
env = gym.make(gym_id)
env = wrap_atari(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
if args.capture_video:
if idx == 0:
env = Monitor(env, f'videos/{experiment_name}')
env = wrap_pytorch(
wrap_deepmind(
env,
clip_rewards=True,
frame_stack=True,
scale=False,
)
)
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
return env
return thunk
示例6: make_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_env(gym_id, seed, idx):
def thunk():
env = gym.make(gym_id)
env = wrap_atari(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
if args.capture_video:
if idx == 0:
env = ProbsVisualizationWrapper(env)
env = Monitor(env, f'videos/{experiment_name}')
env = wrap_pytorch(
wrap_deepmind(
env,
clip_rewards=True,
frame_stack=True,
scale=False,
)
)
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
return env
return thunk
示例7: run_episode
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def run_episode(self, env: gym.Env):
meta_wrapper = get_wrapper(env, chi.rl.wrappers.Wrapper)
done = False
ob = env.reset()
a, meta = self.act(ob)
rs = []
while not done:
if meta_wrapper:
meta_wrapper.set_meta(meta) # send meta information to wrappers
ob, r, done, info = env.step(a)
a, meta = self.act(ob, r, done, info)
rs.append(r)
return sum(rs)
示例8: __init__
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def __init__(self, env_name, record_video=True, video_schedule=None, log_dir=None, record_log=True,
force_reset=False):
if log_dir is None:
if logger.get_snapshot_dir() is None:
logger.log("Warning: skipping Gym environment monitoring since snapshot_dir not configured.")
else:
log_dir = os.path.join(logger.get_snapshot_dir(), "gym_log")
Serializable.quick_init(self, locals())
env = gym.envs.make(env_name)
self.env = env
self.env_id = env.spec.id
assert not (not record_log and record_video)
if log_dir is None or record_log is False:
self.monitoring = False
else:
if not record_video:
video_schedule = NoVideoSchedule()
else:
if video_schedule is None:
video_schedule = CappedCubicVideoSchedule()
self.env = gym.wrappers.Monitor(self.env, log_dir, video_callable=video_schedule, force=True)
self.monitoring = True
self._observation_space = convert_gym_space(env.observation_space)
logger.log("observation space: {}".format(self._observation_space))
self._action_space = convert_gym_space(env.action_space)
logger.log("action space: {}".format(self._action_space))
self._horizon = env.spec.tags['wrapper_config.TimeLimit.max_episode_steps']
self._log_dir = log_dir
self._force_reset = force_reset
示例9: reset
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def reset(self):
if self._force_reset and self.monitoring:
from gym.wrappers.monitoring import Monitor
assert isinstance(self.env, Monitor)
recorder = self.env.stats_recorder
if recorder is not None:
recorder.done = True
return self.env.reset()
示例10: gym_env_wrapper
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def gym_env_wrapper(env, rl_env_max_episode_steps, maxskip_env, rendered_env,
rendered_env_resize_to, sticky_actions, output_dtype,
num_actions):
"""Wraps a gym environment. see make_gym_env for details."""
# rl_env_max_episode_steps is None or int.
assert ((not rl_env_max_episode_steps) or
isinstance(rl_env_max_episode_steps, int))
wrap_with_time_limit = ((not rl_env_max_episode_steps) or
rl_env_max_episode_steps >= 0)
if wrap_with_time_limit:
env = remove_time_limit_wrapper(env)
if num_actions is not None:
logging.log_first_n(
logging.INFO, "Number of discretized actions: %d", 1, num_actions)
env = ActionDiscretizeWrapper(env, num_actions=num_actions)
if sticky_actions:
env = StickyActionEnv(env)
if maxskip_env:
env = MaxAndSkipEnv(env) # pylint: disable=redefined-variable-type
if rendered_env:
env = RenderedEnv(
env, resize_to=rendered_env_resize_to, output_dtype=output_dtype)
if wrap_with_time_limit and rl_env_max_episode_steps is not None:
env = gym.wrappers.TimeLimit(
env, max_episode_steps=rl_env_max_episode_steps)
return env
示例11: gym_env_wrapper
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def gym_env_wrapper(env, rl_env_max_episode_steps, maxskip_env, rendered_env,
rendered_env_resize_to, sticky_actions, output_dtype):
"""Wraps a gym environment. see make_gym_env for details."""
# rl_env_max_episode_steps is None or int.
assert ((not rl_env_max_episode_steps) or
isinstance(rl_env_max_episode_steps, int))
wrap_with_time_limit = ((not rl_env_max_episode_steps) or
rl_env_max_episode_steps >= 0)
if wrap_with_time_limit:
env = remove_time_limit_wrapper(env)
if sticky_actions:
env = StickyActionEnv(env)
if maxskip_env:
env = MaxAndSkipEnv(env) # pylint: disable=redefined-variable-type
if rendered_env:
env = RenderedEnv(
env, resize_to=rendered_env_resize_to, output_dtype=output_dtype)
if wrap_with_time_limit and rl_env_max_episode_steps is not None:
env = gym.wrappers.TimeLimit(
env, max_episode_steps=rl_env_max_episode_steps)
return env
示例12: simpleSSBMEnv
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def simpleSSBMEnv(act_every=3, **kwargs):
env = SSBMEnv(**kwargs)
# TODO: make this a wrapper
env.action_space = spaces.Discrete(len(ssbm.simpleControllerStates))
env.realController = lambda action: ssbm.simpleControllerStates[action].realController()
from .box_wrapper import BoxWrapper
env = BoxWrapper(env)
from gym.wrappers import SkipWrapper
return SkipWrapper(3)(env)
示例13: eval
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def eval(args):
env = gym.make(args.env_id)
logdir = str(Path(args.logbase_path) / args.env_id)
policy = Policy(env.observation_space.shape[0],env.action_space.shape[0])
### Initialize Parameters
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
# Training configuration
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession()
sess.run(init_op)
policy.saver.restore(sess,logdir+'/model.ckpt')
from performance_checker import gen_traj
from gym.wrappers import Monitor
perfs = []
for j in range(args.num_trajs):
if j == 0 and args.video_record:
wrapped = Monitor(env, './video/',force=True)
else:
wrapped = env
perfs.append(gen_traj(wrapped,policy,args.render,args.max_len))
print(logdir, ',', np.mean(perfs), ',', np.std(perfs))
示例14: make_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_env(gym_id, seed, idx):
def thunk():
env = gym.make(gym_id)
env = gym.wrappers.RecordEpisodeStatistics(env)
if args.capture_video:
if idx == 0:
env = Monitor(env, f'videos/{experiment_name}')
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
return env
return thunk
示例15: make_env
# 需要導入模塊: import gym [as 別名]
# 或者: from gym import wrappers [as 別名]
def make_env(gym_id, seed, idx):
def thunk():
env = gym.make(gym_id)
env = ClipActionsWrapper(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
if args.capture_video:
if idx == 0:
env = Monitor(env, f'videos/{experiment_name}')
env = NormalizedEnv(env)
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
return env
return thunk