當前位置: 首頁>>代碼示例>>Python>>正文


Python gym.wrappers方法代碼示例

本文整理匯總了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 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:27,代碼來源:train_soft_actor_critic_atlas.py

示例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 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:22,代碼來源:gym_utils.py

示例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 
開發者ID:JohannesHeidecke,項目名稱:irl-benchmark,代碼行數:25,代碼來源:__init__.py

示例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 
開發者ID:Stable-Baselines-Team,項目名稱:stable-baselines,代碼行數:27,代碼來源:cmd_util.py

示例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 
開發者ID:vwxyzjn,項目名稱:cleanrl,代碼行數:23,代碼來源:ppo_atari.py

示例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 
開發者ID:vwxyzjn,項目名稱:cleanrl,代碼行數:24,代碼來源:ppo_atari_visual.py

示例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) 
開發者ID:rmst,項目名稱:chi,代碼行數:19,代碼來源:core.py

示例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 
開發者ID:nosyndicate,項目名稱:pytorchrl,代碼行數:35,代碼來源:gym_env.py

示例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() 
開發者ID:nosyndicate,項目名稱:pytorchrl,代碼行數:10,代碼來源:gym_env.py

示例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 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:35,代碼來源:gym_utils.py

示例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 
開發者ID:yyht,項目名稱:BERT,代碼行數:29,代碼來源:gym_utils.py

示例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) 
開發者ID:vladfi1,項目名稱:gym-dolphin,代碼行數:14,代碼來源:ssbm_env.py

示例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)) 
開發者ID:hiwonjoon,項目名稱:ICML2019-TREX,代碼行數:30,代碼來源:behavior_cloning.py

示例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 
開發者ID:vwxyzjn,項目名稱:cleanrl,代碼行數:14,代碼來源:ppo.py

示例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 
開發者ID:vwxyzjn,項目名稱:cleanrl,代碼行數:16,代碼來源:ppo_continuous_action.py


注:本文中的gym.wrappers方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。