MCPcopy
hub / github.com/DLR-RM/stable-baselines3 / make_vec_env

Function make_vec_env

stable_baselines3/common/env_util.py:40–129  ·  view source on GitHub ↗

Create a wrapped, monitored ``VecEnv``. By default it uses a ``DummyVecEnv`` which is usually faster than a ``SubprocVecEnv``. :param env_id: either the env ID, the env class or a callable returning an env :param n_envs: the number of environments you wish to have in parallel

(
    env_id: str | Callable[..., gym.Env],
    n_envs: int = 1,
    seed: int | None = None,
    start_index: int = 0,
    monitor_dir: str | None = None,
    wrapper_class: Callable[[gym.Env], gym.Env] | None = None,
    env_kwargs: dict[str, Any] | None = None,
    vec_env_cls: type[DummyVecEnv | SubprocVecEnv] | None = None,
    vec_env_kwargs: dict[str, Any] | None = None,
    monitor_kwargs: dict[str, Any] | None = None,
    wrapper_kwargs: dict[str, Any] | None = None,
)

Source from the content-addressed store, hash-verified

38
39
40def make_vec_env(
41 env_id: str | Callable[..., gym.Env],
42 n_envs: int = 1,
43 seed: int | None = None,
44 start_index: int = 0,
45 monitor_dir: str | None = None,
46 wrapper_class: Callable[[gym.Env], gym.Env] | None = None,
47 env_kwargs: dict[str, Any] | None = None,
48 vec_env_cls: type[DummyVecEnv | SubprocVecEnv] | None = None,
49 vec_env_kwargs: dict[str, Any] | None = None,
50 monitor_kwargs: dict[str, Any] | None = None,
51 wrapper_kwargs: dict[str, Any] | None = None,
52) -> VecEnv:
53 """
54 Create a wrapped, monitored ``VecEnv``.
55 By default it uses a ``DummyVecEnv`` which is usually faster
56 than a ``SubprocVecEnv``.
57
58 :param env_id: either the env ID, the env class or a callable returning an env
59 :param n_envs: the number of environments you wish to have in parallel
60 :param seed: the initial seed for the random number generator
61 :param start_index: start rank index
62 :param monitor_dir: Path to a folder where the monitor files will be saved.
63 If None, no file will be written, however, the env will still be wrapped
64 in a Monitor wrapper to provide additional information about training.
65 :param wrapper_class: Additional wrapper to use on the environment.
66 This can also be a function with single argument that wraps the environment in many things.
67 Note: the wrapper specified by this parameter will be applied after the ``Monitor`` wrapper.
68 if some cases (e.g. with TimeLimit wrapper) this can lead to undesired behavior.
69 See here for more details: https://github.com/DLR-RM/stable-baselines3/issues/894
70 :param env_kwargs: Optional keyword argument to pass to the env constructor
71 :param vec_env_cls: A custom ``VecEnv`` class constructor. Default: None.
72 :param vec_env_kwargs: Keyword arguments to pass to the ``VecEnv`` class constructor.
73 :param monitor_kwargs: Keyword arguments to pass to the ``Monitor`` class constructor.
74 :param wrapper_kwargs: Keyword arguments to pass to the ``Wrapper`` class constructor.
75 :return: The wrapped environment
76 """
77 env_kwargs = env_kwargs or {}
78 vec_env_kwargs = vec_env_kwargs or {}
79 monitor_kwargs = monitor_kwargs or {}
80 wrapper_kwargs = wrapper_kwargs or {}
81 assert vec_env_kwargs is not None # for mypy
82
83 def make_env(rank: int) -> Callable[[], gym.Env]:
84 def _init() -> gym.Env:
85 # For type checker:
86 assert monitor_kwargs is not None
87 assert wrapper_kwargs is not None
88 assert env_kwargs is not None
89
90 if isinstance(env_id, str):
91 # if the render mode was not specified, we set it to `rgb_array` as default.
92 kwargs = {"render_mode": "rgb_array"}
93 kwargs.update(env_kwargs)
94 try:
95 env = gym.make(env_id, **kwargs) # type: ignore[arg-type]
96 except TypeError:
97 env = gym.make(env_id, **env_kwargs)

Callers 15

test_vec_deterministicFunction · 0.90
test_renderFunction · 0.90
test_video_recorderFunction · 0.90
test_multiprocessingFunction · 0.90
test_runFunction · 0.90
test_device_bufferFunction · 0.90
test_make_vec_envFunction · 0.90
test_vec_env_kwargsFunction · 0.90

Calls 2

make_envFunction · 0.70
seedMethod · 0.45

Tested by 15

test_vec_deterministicFunction · 0.72
test_renderFunction · 0.72
test_video_recorderFunction · 0.72
test_multiprocessingFunction · 0.72
test_runFunction · 0.72
test_device_bufferFunction · 0.72
test_make_vec_envFunction · 0.72
test_vec_env_kwargsFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…