-
Notifications
You must be signed in to change notification settings - Fork 100
/
env_step.py
129 lines (115 loc) · 4.37 KB
/
env_step.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2021 Garena Online Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gym
import numpy as np
from packaging import version
import envpool
is_legacy_gym = version.parse(gym.__version__) < version.parse("0.26.0")
def gym_sync_step() -> None:
num_envs = 4
env = envpool.make_gym("Pong-v5", num_envs=num_envs)
action_num = env.action_space.n
if is_legacy_gym:
obs = env.reset() # reset all envs
else:
obs, _ = env.reset() # reset all envs
assert obs.shape == (num_envs, 4, 84, 84)
for _ in range(1000):
# autoreset is automatically enabled in envpool
action = np.random.randint(action_num, size=num_envs)
result = env.step(action)
if is_legacy_gym:
obs, rew, done, info = env.step(action)
else:
obs, rew, term, trunc, info = env.step(action)
# Of course, you can specify env_id to step corresponding envs
if is_legacy_gym:
obs = env.reset(np.array([1, 3])) # reset env #1 and #3
else:
obs, _ = env.reset(np.array([1, 3])) # reset env #1 and #3
assert obs.shape == (2, 4, 84, 84)
partial_action = np.array([0, 0, 2])
env_id = np.array([3, 2, 0])
result = env.step(partial_action, env_id)
obs, info = result[0], result[-1]
np.testing.assert_allclose(info["env_id"], env_id)
assert obs.shape == (3, 4, 84, 84)
def dm_sync_step() -> None:
num_envs = 4
env = envpool.make_dm("Pong-v5", num_envs=num_envs)
action_num = env.action_spec().num_values
ts = env.reset()
# ts.observation is a **NamedTuple** instead of np.ndarray
# because we need to store other valuable information in this field
assert ts.observation.obs.shape, (num_envs, 4, 84, 84)
for _ in range(1000):
# autoreset is automatically enabled in envpool
action = np.random.randint(action_num, size=num_envs)
ts = env.step(action)
# Of course, you can specify env_id to step corresponding envs
ts = env.reset(np.array([1, 3])) # reset env #1 and #3
assert ts.observation.obs.shape == (2, 4, 84, 84)
partial_action = np.array([0, 0, 2])
env_id = np.array([3, 2, 0])
ts = env.step(partial_action, env_id)
np.testing.assert_allclose(ts.observation.env_id, env_id)
assert ts.observation.obs.shape == (3, 4, 84, 84)
def async_step() -> None:
num_envs = 8
batch_size = 4
# Create an envpool that each step only 4 of 8 result will be out,
# and left other "slow step" envs execute at background.
env = envpool.make_dm("Pong-v5", num_envs=num_envs, batch_size=batch_size)
action_num = env.action_spec().num_values
ts = env.reset()
for _ in range(1000):
env_id = ts.observation.env_id
assert len(env_id) == batch_size
# generate action with len(action) == len(env_id)
action = np.random.randint(action_num, size=batch_size)
ts = env.step(action, env_id)
# Same as gym
env = envpool.make_gym(
"Pong-v5",
num_envs=num_envs,
batch_size=batch_size,
gym_reset_return_info=True,
)
# If you want gym's reset() API return env_id,
# just set gym_reset_return_info=True
obs, info = env.reset()
assert obs.shape == (batch_size, 4, 84, 84)
env_id = info["env_id"]
for _ in range(1000):
action = np.random.randint(action_num, size=batch_size)
result = env.step(action, env_id)
obs, info = result[0], result[-1]
env_id = info["env_id"]
assert len(env_id) == batch_size
assert obs.shape == (batch_size, 4, 84, 84)
# We can also use a low-level API (send and recv)
env = envpool.make_gym("Pong-v5", num_envs=num_envs, batch_size=batch_size)
env.async_reset() # no return, just send `reset` signal to all envs
for _ in range(1000):
result = env.recv()
obs, info = result[0], result[-1]
env_id = info["env_id"]
assert len(env_id) == batch_size
assert obs.shape == (batch_size, 4, 84, 84)
action = np.random.randint(action_num, size=batch_size)
env.send(action, env_id)
if __name__ == "__main__":
gym_sync_step()
dm_sync_step()
async_step()