Python make vec env
8 Python code examples are found related to "
make vec env".
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: cmd_util.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def make_vec_env(env_id, env_type, num_env, seed, wrapper_kwargs=None, start_index=0, reward_scale=1.0): """ Create a wrapped, monitored SubprocVecEnv for Atari and MuJoCo. """ if wrapper_kwargs is None: wrapper_kwargs = {} mpi_rank = MPI.COMM_WORLD.Get_rank() if MPI else 0 def make_env(rank): # pylint: disable=C0111 def _thunk(): env = make_atari(env_id) if env_type == 'atari' else gym.make(env_id) env.seed(seed + 10000*mpi_rank + rank if seed is not None else None) env = Monitor(env, logger.get_dir() and os.path.join(logger.get_dir(), str(mpi_rank) + '.' + str(rank)), allow_early_resets=True) if env_type == 'atari': return wrap_deepmind(env, **wrapper_kwargs) elif reward_scale != 1: return RewardScaler(env, reward_scale) else: return env return _thunk set_global_seeds(seed) if num_env > 1: return SubprocVecEnv([make_env(i + start_index) for i in range(num_env)]) else: return DummyVecEnv([make_env(start_index)])
Example 2
Source File: cmd_util.py From ICML2019-TREX with MIT License | 6 votes |
def make_vec_env(env_id, env_type, num_env, seed, wrapper_kwargs=None, start_index=0, reward_scale=1.0, gamestate=None): """ Create a wrapped, monitored SubprocVecEnv for Atari and MuJoCo. """ if wrapper_kwargs is None: wrapper_kwargs = {} mpi_rank = MPI.COMM_WORLD.Get_rank() if MPI else 0 seed = seed + 10000 * mpi_rank if seed is not None else None def make_thunk(rank): return lambda: make_env( env_id=env_id, env_type=env_type, subrank = rank, seed=seed, reward_scale=reward_scale, gamestate=gamestate, wrapper_kwargs=wrapper_kwargs ) set_global_seeds(seed) if num_env > 1: return SubprocVecEnv([make_thunk(i + start_index) for i in range(num_env)]) else: return DummyVecEnv([make_thunk(start_index)])
Example 3
Source File: sub_proc.py From rltime with Apache License 2.0 | 5 votes |
def make_sub_proc_vec_env(creator, num_env, base_seed=0): def make_env(rank): # pylint: disable=C0111 def _thunk(): env = creator() env.seed(base_seed + rank) return env return _thunk return SubprocVecEnv([make_env(i) for i in range(num_env)])
Example 4
Source File: simple.py From rltime with Apache License 2.0 | 5 votes |
def make_simple_vec_env(creator, num_env, base_seed=0): """Creates a simple vec-env with the given env-creator and amount of envs""" def make_env(rank): env = creator() env.seed(base_seed + rank) return env return SimpleVecEnv([make_env(i) for i in range(num_env)])
Example 5
Source File: util.py From RL-Experiments with MIT License | 5 votes |
def make_vec_env(env_id, env_type, nenv, seed, reward_scale, log_path, frame_stack=True): """ Make vectorized env """ env = SubprocVecEnv([ partial(make_env, env_id, env_type, seed + i, reward_scale, log_path) for i in range(nenv) ]) if frame_stack: env = VecFrameStack(env, 4) return env
Example 6
Source File: cmd_util.py From baselines with MIT License | 5 votes |
def make_vec_env(env_id, env_type, num_env, seed, wrapper_kwargs=None, env_kwargs=None, start_index=0, reward_scale=1.0, flatten_dict_observations=True, gamestate=None, initializer=None, force_dummy=False): """ Create a wrapped, monitored SubprocVecEnv for Atari and MuJoCo. """ wrapper_kwargs = wrapper_kwargs or {} env_kwargs = env_kwargs or {} mpi_rank = MPI.COMM_WORLD.Get_rank() if MPI else 0 seed = seed + 10000 * mpi_rank if seed is not None else None logger_dir = logger.get_dir() def make_thunk(rank, initializer=None): return lambda: make_env( env_id=env_id, env_type=env_type, mpi_rank=mpi_rank, subrank=rank, seed=seed, reward_scale=reward_scale, gamestate=gamestate, flatten_dict_observations=flatten_dict_observations, wrapper_kwargs=wrapper_kwargs, env_kwargs=env_kwargs, logger_dir=logger_dir, initializer=initializer ) set_global_seeds(seed) if not force_dummy and num_env > 1: return SubprocVecEnv([make_thunk(i + start_index, initializer=initializer) for i in range(num_env)]) else: return DummyVecEnv([make_thunk(i + start_index, initializer=None) for i in range(num_env)])
Example 7
Source File: util.py From imitation with MIT License | 4 votes |
def make_vec_env( env_name: str, n_envs: int = 8, seed: int = 0, parallel: bool = False, log_dir: Optional[str] = None, max_episode_steps: Optional[int] = None, ) -> VecEnv: """Returns a VecEnv initialized with `n_envs` Envs. Args: env_name: The Env's string id in Gym. n_envs: The number of duplicate environments. seed: The environment seed. parallel: If True, uses SubprocVecEnv; otherwise, DummyVecEnv. log_dir: If specified, saves Monitor output to this directory. max_episode_steps: If specified, wraps each env in a TimeLimit wrapper with this episode length. If not specified and `max_episode_steps` exists for this `env_name` in the Gym registry, uses the registry `max_episode_steps` for every TimeLimit wrapper (this automatic wrapper is the default behavior when calling `gym.make`). Otherwise the environments are passed into the VecEnv unwrapped. """ # Resolve the spec outside of the subprocess first, so that it is available to # subprocesses running `make_env` via automatic pickling. spec = gym.spec(env_name) def make_env(i, this_seed): # Previously, we directly called `gym.make(env_name)`, but running # `imitation.scripts.train_adversarial` within `imitation.scripts.parallel` # created a weird interaction between Gym and Ray -- `gym.make` would fail # inside this function for any of our custom environment unless those # environments were also `gym.register()`ed inside `make_env`. Even # registering the custom environment in the scope of `make_vec_env` didn't # work. For more discussion and hypotheses on this issue see PR #160: # https://github.com/HumanCompatibleAI/imitation/pull/160. env = spec.make() # Seed each environment with a different, non-sequential seed for diversity # (even if caller is passing us sequentially-assigned base seeds). int() is # necessary to work around gym bug where it chokes on numpy int64s. env.seed(int(this_seed)) if max_episode_steps is not None: env = TimeLimit(env, max_episode_steps) elif spec.max_episode_steps is not None: env = TimeLimit(env, max_episode_steps=spec.max_episode_steps) # Use Monitor to record statistics needed for Baselines algorithms logging # Optionally, save to disk log_path = None if log_dir is not None: log_subdir = os.path.join(log_dir, "monitor") os.makedirs(log_subdir, exist_ok=True) log_path = os.path.join(log_subdir, f"mon{i:03d}") env = bench.Monitor(env, log_path) env = wrappers.RolloutInfoWrapper(env) return env rng = np.random.RandomState(seed) env_seeds = rng.randint(0, (1 << 31) - 1, (n_envs,)) env_fns = [functools.partial(make_env, i, s) for i, s in enumerate(env_seeds)] if parallel: # See GH hill-a/stable-baselines issue #217 return SubprocVecEnv(env_fns, start_method="forkserver") else: return DummyVecEnv(env_fns)
Example 8
Source File: cmd_util.py From stable-baselines with MIT License | 4 votes |
def make_vec_env(env_id, n_envs=1, seed=None, start_index=0, monitor_dir=None, wrapper_class=None, env_kwargs=None, vec_env_cls=None, vec_env_kwargs=None): """ Create a wrapped, monitored `VecEnv`. By default it uses a `DummyVecEnv` which is usually faster than a `SubprocVecEnv`. :param env_id: (str or Type[gym.Env]) the environment ID or the environment class :param n_envs: (int) the number of environments you wish to have in parallel :param seed: (int) the initial seed for the random number generator :param start_index: (int) start rank index :param monitor_dir: (str) Path to a folder where the monitor files will be saved. If None, no file will be written, however, the env will still be wrapped in a Monitor wrapper to provide additional information about training. :param wrapper_class: (gym.Wrapper or callable) Additional wrapper to use on the environment. This can also be a function with single argument that wraps the environment in many things. :param env_kwargs: (dict) Optional keyword argument to pass to the env constructor :param vec_env_cls: (Type[VecEnv]) A custom `VecEnv` class constructor. Default: None. :param vec_env_kwargs: (dict) Keyword arguments to pass to the `VecEnv` class constructor. :return: (VecEnv) The wrapped environment """ env_kwargs = {} if env_kwargs is None else env_kwargs vec_env_kwargs = {} if vec_env_kwargs is None else vec_env_kwargs def make_env(rank): def _init(): if isinstance(env_id, str): env = gym.make(env_id) if len(env_kwargs) > 0: warnings.warn("No environment class was passed (only an env ID) so `env_kwargs` will be ignored") else: env = env_id(**env_kwargs) if seed is not None: env.seed(seed + rank) env.action_space.seed(seed + rank) # Wrap the env in a Monitor wrapper # to have additional training information monitor_path = os.path.join(monitor_dir, str(rank)) if monitor_dir is not None else None # Create the monitor folder if needed if monitor_path is not None: os.makedirs(monitor_dir, exist_ok=True) env = Monitor(env, filename=monitor_path) # Optionally, wrap the environment with the provided wrapper if wrapper_class is not None: env = wrapper_class(env) return env return _init # No custom VecEnv is passed if vec_env_cls is None: # Default: use a DummyVecEnv vec_env_cls = DummyVecEnv return vec_env_cls([make_env(i + start_index) for i in range(n_envs)], **vec_env_kwargs)