2020-03-25 14:08:28 +08:00
|
|
|
import numpy as np
|
2021-09-03 05:05:04 +08:00
|
|
|
import pytest
|
|
|
|
import tqdm
|
2020-04-10 18:02:05 +08:00
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
2021-02-19 10:33:49 +08:00
|
|
|
from tianshou.data import (
|
2021-09-03 05:05:04 +08:00
|
|
|
AsyncCollector,
|
|
|
|
Batch,
|
|
|
|
CachedReplayBuffer,
|
|
|
|
Collector,
|
2021-02-19 10:33:49 +08:00
|
|
|
PrioritizedReplayBuffer,
|
2021-09-03 05:05:04 +08:00
|
|
|
ReplayBuffer,
|
2021-02-19 10:33:49 +08:00
|
|
|
VectorReplayBuffer,
|
|
|
|
)
|
2021-09-03 05:05:04 +08:00
|
|
|
from tianshou.env import DummyVectorEnv, SubprocVectorEnv
|
|
|
|
from tianshou.policy import BasePolicy
|
2020-03-25 14:08:28 +08:00
|
|
|
|
2022-06-27 18:52:21 -04:00
|
|
|
try:
|
|
|
|
import envpool
|
|
|
|
except ImportError:
|
|
|
|
envpool = None
|
|
|
|
|
2023-08-25 23:40:56 +02:00
|
|
|
if __name__ == "__main__":
|
2021-04-25 15:23:46 +08:00
|
|
|
from env import MyTestEnv, NXEnv
|
2020-03-25 14:08:28 +08:00
|
|
|
else: # pytest
|
2021-04-25 15:23:46 +08:00
|
|
|
from test.base.env import MyTestEnv, NXEnv
|
2020-03-25 14:08:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MyPolicy(BasePolicy):
|
2022-06-27 18:52:21 -04:00
|
|
|
def __init__(self, dict_state=False, need_state=True, action_shape=None):
|
2023-08-25 23:40:56 +02:00
|
|
|
"""Mock policy for testing.
|
|
|
|
|
2020-08-19 15:00:24 +08:00
|
|
|
:param bool dict_state: if the observation of the environment is a dict
|
|
|
|
:param bool need_state: if the policy needs the hidden state (for RNN)
|
|
|
|
"""
|
2020-03-25 14:08:28 +08:00
|
|
|
super().__init__()
|
2020-04-28 20:56:02 +08:00
|
|
|
self.dict_state = dict_state
|
2020-08-19 15:00:24 +08:00
|
|
|
self.need_state = need_state
|
2022-06-27 18:52:21 -04:00
|
|
|
self.action_shape = action_shape
|
2020-03-25 14:08:28 +08:00
|
|
|
|
2020-04-10 10:47:16 +08:00
|
|
|
def forward(self, batch, state=None):
|
2020-08-19 15:00:24 +08:00
|
|
|
if self.need_state:
|
|
|
|
if state is None:
|
|
|
|
state = np.zeros((len(batch.obs), 2))
|
|
|
|
else:
|
|
|
|
state += 1
|
2020-04-28 20:56:02 +08:00
|
|
|
if self.dict_state:
|
2023-08-25 23:40:56 +02:00
|
|
|
action_shape = self.action_shape if self.action_shape else len(batch.obs["index"])
|
2022-06-27 18:52:21 -04:00
|
|
|
return Batch(act=np.ones(action_shape), state=state)
|
|
|
|
action_shape = self.action_shape if self.action_shape else len(batch.obs)
|
|
|
|
return Batch(act=np.ones(action_shape), state=state)
|
2020-03-25 14:08:28 +08:00
|
|
|
|
|
|
|
def learn(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-07-23 16:40:53 +08:00
|
|
|
class Logger:
|
2020-04-10 18:02:05 +08:00
|
|
|
def __init__(self, writer):
|
|
|
|
self.cnt = 0
|
|
|
|
self.writer = writer
|
|
|
|
|
2020-07-23 16:40:53 +08:00
|
|
|
def preprocess_fn(self, **kwargs):
|
|
|
|
# modify info before adding into the buffer, and recorded into tfb
|
2021-07-05 09:50:39 +08:00
|
|
|
# if obs && env_id exist -> reset
|
|
|
|
# if obs_next/rew/done/info/env_id exist -> normal step
|
2023-08-25 23:40:56 +02:00
|
|
|
if "rew" in kwargs:
|
|
|
|
info = kwargs["info"]
|
|
|
|
info.rew = kwargs["rew"]
|
|
|
|
if "key" in info:
|
|
|
|
self.writer.add_scalar("key", np.mean(info.key), global_step=self.cnt)
|
2020-07-23 16:40:53 +08:00
|
|
|
self.cnt += 1
|
|
|
|
return Batch(info=info)
|
2023-08-25 23:40:56 +02:00
|
|
|
return Batch()
|
2020-07-23 16:40:53 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def single_preprocess_fn(**kwargs):
|
|
|
|
# same as above, without tfb
|
2023-08-25 23:40:56 +02:00
|
|
|
if "rew" in kwargs:
|
|
|
|
info = kwargs["info"]
|
|
|
|
info.rew = kwargs["rew"]
|
2020-07-23 16:40:53 +08:00
|
|
|
return Batch(info=info)
|
2023-08-25 23:40:56 +02:00
|
|
|
return Batch()
|
2020-04-10 18:02:05 +08:00
|
|
|
|
|
|
|
|
2023-08-25 23:40:56 +02:00
|
|
|
@pytest.mark.parametrize("gym_reset_kwargs", [None, {}])
|
2022-06-27 18:52:21 -04:00
|
|
|
def test_collector(gym_reset_kwargs):
|
2023-08-25 23:40:56 +02:00
|
|
|
writer = SummaryWriter("log/collector")
|
2020-04-10 18:02:05 +08:00
|
|
|
logger = Logger(writer)
|
2020-06-09 18:46:14 +08:00
|
|
|
env_fns = [lambda x=i: MyTestEnv(size=x, sleep=0) for i in [2, 3, 4, 5]]
|
2020-04-10 18:02:05 +08:00
|
|
|
|
2020-03-25 14:08:28 +08:00
|
|
|
venv = SubprocVectorEnv(env_fns)
|
2020-08-19 15:00:24 +08:00
|
|
|
dum = DummyVectorEnv(env_fns)
|
2020-03-25 14:08:28 +08:00
|
|
|
policy = MyPolicy()
|
|
|
|
env = env_fns[0]()
|
2022-06-27 18:52:21 -04:00
|
|
|
c0 = Collector(
|
|
|
|
policy,
|
|
|
|
env,
|
|
|
|
ReplayBuffer(size=100),
|
|
|
|
logger.preprocess_fn,
|
|
|
|
)
|
|
|
|
c0.collect(n_step=3, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(c0.buffer) == 3
|
|
|
|
assert np.allclose(c0.buffer.obs[:4, 0], [0, 1, 0, 0])
|
|
|
|
assert np.allclose(c0.buffer[:].obs_next[..., 0], [1, 2, 1])
|
2022-06-27 18:52:21 -04:00
|
|
|
keys = np.zeros(100)
|
|
|
|
keys[:3] = 1
|
|
|
|
assert np.allclose(c0.buffer.info["key"], keys)
|
|
|
|
for e in c0.buffer.info["env"][:3]:
|
|
|
|
assert isinstance(e, MyTestEnv)
|
|
|
|
assert np.allclose(c0.buffer.info["env_id"], 0)
|
|
|
|
rews = np.zeros(100)
|
|
|
|
rews[:3] = [0, 1, 0]
|
|
|
|
assert np.allclose(c0.buffer.info["rew"], rews)
|
|
|
|
c0.collect(n_episode=3, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(c0.buffer) == 8
|
|
|
|
assert np.allclose(c0.buffer.obs[:10, 0], [0, 1, 0, 1, 0, 1, 0, 1, 0, 0])
|
2021-09-03 05:05:04 +08:00
|
|
|
assert np.allclose(c0.buffer[:].obs_next[..., 0], [1, 2, 1, 2, 1, 2, 1, 2])
|
2022-06-27 18:52:21 -04:00
|
|
|
assert np.allclose(c0.buffer.info["key"][:8], 1)
|
|
|
|
for e in c0.buffer.info["env"][:8]:
|
|
|
|
assert isinstance(e, MyTestEnv)
|
|
|
|
assert np.allclose(c0.buffer.info["env_id"][:8], 0)
|
|
|
|
assert np.allclose(c0.buffer.info["rew"][:8], [0, 1, 0, 1, 0, 1, 0, 1])
|
|
|
|
c0.collect(n_step=3, random=True, gym_reset_kwargs=gym_reset_kwargs)
|
|
|
|
|
2021-02-19 10:33:49 +08:00
|
|
|
c1 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
venv,
|
|
|
|
VectorReplayBuffer(total_size=100, buffer_num=4),
|
|
|
|
logger.preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2022-06-27 18:52:21 -04:00
|
|
|
c1.collect(n_step=8, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
obs = np.zeros(100)
|
2022-06-27 18:52:21 -04:00
|
|
|
valid_indices = [0, 1, 25, 26, 50, 51, 75, 76]
|
|
|
|
obs[valid_indices] = [0, 1, 0, 1, 0, 1, 0, 1]
|
2021-02-19 10:33:49 +08:00
|
|
|
assert np.allclose(c1.buffer.obs[:, 0], obs)
|
|
|
|
assert np.allclose(c1.buffer[:].obs_next[..., 0], [1, 2, 1, 2, 1, 2, 1, 2])
|
2022-06-27 18:52:21 -04:00
|
|
|
keys = np.zeros(100)
|
|
|
|
keys[valid_indices] = [1, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
assert np.allclose(c1.buffer.info["key"], keys)
|
|
|
|
for e in c1.buffer.info["env"][valid_indices]:
|
|
|
|
assert isinstance(e, MyTestEnv)
|
|
|
|
env_ids = np.zeros(100)
|
|
|
|
env_ids[valid_indices] = [0, 0, 1, 1, 2, 2, 3, 3]
|
|
|
|
assert np.allclose(c1.buffer.info["env_id"], env_ids)
|
|
|
|
rews = np.zeros(100)
|
|
|
|
rews[valid_indices] = [0, 1, 0, 0, 0, 0, 0, 0]
|
|
|
|
assert np.allclose(c1.buffer.info["rew"], rews)
|
|
|
|
c1.collect(n_episode=4, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(c1.buffer) == 16
|
2022-06-27 18:52:21 -04:00
|
|
|
valid_indices = [2, 3, 27, 52, 53, 77, 78, 79]
|
2021-02-19 10:33:49 +08:00
|
|
|
obs[[2, 3, 27, 52, 53, 77, 78, 79]] = [0, 1, 2, 2, 3, 2, 3, 4]
|
|
|
|
assert np.allclose(c1.buffer.obs[:, 0], obs)
|
2021-09-03 05:05:04 +08:00
|
|
|
assert np.allclose(
|
2023-08-25 23:40:56 +02:00
|
|
|
c1.buffer[:].obs_next[..., 0],
|
|
|
|
[1, 2, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5],
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2022-06-27 18:52:21 -04:00
|
|
|
keys[valid_indices] = [1, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
assert np.allclose(c1.buffer.info["key"], keys)
|
|
|
|
for e in c1.buffer.info["env"][valid_indices]:
|
|
|
|
assert isinstance(e, MyTestEnv)
|
|
|
|
env_ids[valid_indices] = [0, 0, 1, 2, 2, 3, 3, 3]
|
|
|
|
assert np.allclose(c1.buffer.info["env_id"], env_ids)
|
|
|
|
rews[valid_indices] = [0, 1, 1, 0, 1, 0, 0, 1]
|
|
|
|
assert np.allclose(c1.buffer.info["rew"], rews)
|
|
|
|
c1.collect(n_episode=4, random=True, gym_reset_kwargs=gym_reset_kwargs)
|
|
|
|
|
2021-02-19 10:33:49 +08:00
|
|
|
c2 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
dum,
|
|
|
|
VectorReplayBuffer(total_size=100, buffer_num=4),
|
|
|
|
logger.preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2022-06-27 18:52:21 -04:00
|
|
|
c2.collect(n_episode=7, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
obs1 = obs.copy()
|
|
|
|
obs1[[4, 5, 28, 29, 30]] = [0, 1, 0, 1, 2]
|
|
|
|
obs2 = obs.copy()
|
|
|
|
obs2[[28, 29, 30, 54, 55, 56, 57]] = [0, 1, 2, 0, 1, 2, 3]
|
|
|
|
c2obs = c2.buffer.obs[:, 0]
|
|
|
|
assert np.all(c2obs == obs1) or np.all(c2obs == obs2)
|
2022-06-27 18:52:21 -04:00
|
|
|
c2.reset_env(gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
c2.reset_buffer()
|
2023-08-25 23:40:56 +02:00
|
|
|
assert c2.collect(n_episode=8, gym_reset_kwargs=gym_reset_kwargs)["n/ep"] == 8
|
2022-06-27 18:52:21 -04:00
|
|
|
valid_indices = [4, 5, 28, 29, 30, 54, 55, 56, 57]
|
|
|
|
obs[valid_indices] = [0, 1, 0, 1, 2, 0, 1, 2, 3]
|
2021-02-19 10:33:49 +08:00
|
|
|
assert np.all(c2.buffer.obs[:, 0] == obs)
|
2022-06-27 18:52:21 -04:00
|
|
|
keys[valid_indices] = [1, 1, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
assert np.allclose(c2.buffer.info["key"], keys)
|
|
|
|
for e in c2.buffer.info["env"][valid_indices]:
|
|
|
|
assert isinstance(e, MyTestEnv)
|
|
|
|
env_ids[valid_indices] = [0, 0, 1, 1, 1, 2, 2, 2, 2]
|
|
|
|
assert np.allclose(c2.buffer.info["env_id"], env_ids)
|
|
|
|
rews[valid_indices] = [0, 1, 0, 0, 1, 0, 0, 0, 1]
|
|
|
|
assert np.allclose(c2.buffer.info["rew"], rews)
|
|
|
|
c2.collect(n_episode=4, random=True, gym_reset_kwargs=gym_reset_kwargs)
|
2020-08-27 12:15:18 +08:00
|
|
|
|
2021-02-19 10:33:49 +08:00
|
|
|
# test corner case
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
Collector(policy, dum, ReplayBuffer(10))
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
Collector(policy, dum, PrioritizedReplayBuffer(10, 0.5, 0.5))
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
c2.collect()
|
2020-08-27 12:15:18 +08:00
|
|
|
|
2021-04-25 15:23:46 +08:00
|
|
|
# test NXEnv
|
|
|
|
for obs_type in ["array", "object"]:
|
2023-08-25 23:40:56 +02:00
|
|
|
envs = SubprocVectorEnv([lambda i=x, t=obs_type: NXEnv(i, t) for x in [5, 10, 15, 20]])
|
2021-09-03 05:05:04 +08:00
|
|
|
c3 = Collector(policy, envs, VectorReplayBuffer(total_size=100, buffer_num=4))
|
2022-06-27 18:52:21 -04:00
|
|
|
c3.collect(n_step=6, gym_reset_kwargs=gym_reset_kwargs)
|
2021-04-25 15:23:46 +08:00
|
|
|
assert c3.buffer.obs.dtype == object
|
|
|
|
|
2020-08-27 12:15:18 +08:00
|
|
|
|
2023-08-25 23:40:56 +02:00
|
|
|
@pytest.mark.parametrize("gym_reset_kwargs", [None, {}])
|
2022-06-27 18:52:21 -04:00
|
|
|
def test_collector_with_async(gym_reset_kwargs):
|
2020-07-26 12:01:21 +02:00
|
|
|
env_lens = [2, 3, 4, 5]
|
2023-08-25 23:40:56 +02:00
|
|
|
writer = SummaryWriter("log/async_collector")
|
2020-07-26 12:01:21 +02:00
|
|
|
logger = Logger(writer)
|
2023-08-25 23:40:56 +02:00
|
|
|
env_fns = [lambda x=i: MyTestEnv(size=x, sleep=0.001, random_sleep=True) for i in env_lens]
|
2020-07-26 12:01:21 +02:00
|
|
|
|
2020-08-19 15:00:24 +08:00
|
|
|
venv = SubprocVectorEnv(env_fns, wait_num=len(env_fns) - 1)
|
2020-07-26 12:01:21 +02:00
|
|
|
policy = MyPolicy()
|
2021-02-19 10:33:49 +08:00
|
|
|
bufsize = 60
|
|
|
|
c1 = AsyncCollector(
|
2022-06-27 18:52:21 -04:00
|
|
|
policy,
|
|
|
|
venv,
|
|
|
|
VectorReplayBuffer(total_size=bufsize * 4, buffer_num=4),
|
|
|
|
logger.preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
ptr = [0, 0, 0, 0]
|
|
|
|
for n_episode in tqdm.trange(1, 30, desc="test async n_episode"):
|
2022-06-27 18:52:21 -04:00
|
|
|
result = c1.collect(n_episode=n_episode, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert result["n/ep"] >= n_episode
|
|
|
|
# check buffer data, obs and obs_next, env_id
|
2021-09-03 05:05:04 +08:00
|
|
|
for i, count in enumerate(np.bincount(result["lens"], minlength=6)[2:]):
|
2021-02-19 10:33:49 +08:00
|
|
|
env_len = i + 2
|
|
|
|
total = env_len * count
|
|
|
|
indices = np.arange(ptr[i], ptr[i] + total) % bufsize
|
|
|
|
ptr[i] = (ptr[i] + total) % bufsize
|
|
|
|
seq = np.arange(env_len)
|
|
|
|
buf = c1.buffer.buffers[i]
|
|
|
|
assert np.all(buf.info.env_id[indices] == i)
|
|
|
|
assert np.all(buf.obs[indices].reshape(count, env_len) == seq)
|
2021-09-03 05:05:04 +08:00
|
|
|
assert np.all(buf.obs_next[indices].reshape(count, env_len) == seq + 1)
|
2021-02-19 10:33:49 +08:00
|
|
|
# test async n_step, for now the buffer should be full of data
|
|
|
|
for n_step in tqdm.trange(1, 15, desc="test async n_step"):
|
2022-06-27 18:52:21 -04:00
|
|
|
result = c1.collect(n_step=n_step, gym_reset_kwargs=gym_reset_kwargs)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert result["n/st"] >= n_step
|
|
|
|
for i in range(4):
|
|
|
|
env_len = i + 2
|
|
|
|
seq = np.arange(env_len)
|
|
|
|
buf = c1.buffer.buffers[i]
|
|
|
|
assert np.all(buf.info.env_id == i)
|
|
|
|
assert np.all(buf.obs.reshape(-1, env_len) == seq)
|
|
|
|
assert np.all(buf.obs_next.reshape(-1, env_len) == seq + 1)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
c1.collect()
|
2020-07-26 12:01:21 +02:00
|
|
|
|
|
|
|
|
2020-04-28 20:56:02 +08:00
|
|
|
def test_collector_with_dict_state():
|
|
|
|
env = MyTestEnv(size=5, sleep=0, dict_state=True)
|
|
|
|
policy = MyPolicy(dict_state=True)
|
2021-09-03 05:05:04 +08:00
|
|
|
c0 = Collector(policy, env, ReplayBuffer(size=100), Logger.single_preprocess_fn)
|
2020-04-28 20:56:02 +08:00
|
|
|
c0.collect(n_step=3)
|
2020-07-23 16:40:53 +08:00
|
|
|
c0.collect(n_episode=2)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(c0.buffer) == 10
|
2023-08-25 23:40:56 +02:00
|
|
|
env_fns = [lambda x=i: MyTestEnv(size=x, sleep=0, dict_state=True) for i in [2, 3, 4, 5]]
|
2020-08-19 15:00:24 +08:00
|
|
|
envs = DummyVectorEnv(env_fns)
|
2020-07-24 17:38:12 +08:00
|
|
|
envs.seed(666)
|
2022-09-26 18:31:23 +02:00
|
|
|
obs, info = envs.reset()
|
2023-08-25 23:40:56 +02:00
|
|
|
assert not np.isclose(obs[0]["rand"], obs[1]["rand"])
|
2021-02-19 10:33:49 +08:00
|
|
|
c1 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
envs,
|
|
|
|
VectorReplayBuffer(total_size=100, buffer_num=4),
|
|
|
|
Logger.single_preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
c1.collect(n_step=12)
|
|
|
|
result = c1.collect(n_episode=8)
|
2023-08-25 23:40:56 +02:00
|
|
|
assert result["n/ep"] == 8
|
|
|
|
lens = np.bincount(result["lens"])
|
|
|
|
assert (
|
|
|
|
result["n/st"] == 21
|
|
|
|
and np.all(lens == [0, 0, 2, 2, 2, 2])
|
|
|
|
or result["n/st"] == 20
|
|
|
|
and np.all(lens == [0, 0, 3, 1, 2, 2])
|
|
|
|
)
|
2020-08-15 16:10:42 +08:00
|
|
|
batch, _ = c1.buffer.sample(10)
|
2020-04-29 12:14:53 +08:00
|
|
|
c0.buffer.update(c1.buffer)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(c0.buffer) in [42, 43]
|
|
|
|
if len(c0.buffer) == 42:
|
2021-09-03 05:05:04 +08:00
|
|
|
assert np.all(
|
2023-08-25 23:40:56 +02:00
|
|
|
c0.buffer[:].obs.index[..., 0]
|
|
|
|
== [
|
2021-09-03 05:05:04 +08:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
2023-08-25 23:40:56 +02:00
|
|
|
],
|
2021-09-03 05:05:04 +08:00
|
|
|
), c0.buffer[:].obs.index[..., 0]
|
2021-02-19 10:33:49 +08:00
|
|
|
else:
|
2021-09-03 05:05:04 +08:00
|
|
|
assert np.all(
|
2023-08-25 23:40:56 +02:00
|
|
|
c0.buffer[:].obs.index[..., 0]
|
|
|
|
== [
|
2021-09-03 05:05:04 +08:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
2023-08-25 23:40:56 +02:00
|
|
|
],
|
2021-09-03 05:05:04 +08:00
|
|
|
), c0.buffer[:].obs.index[..., 0]
|
2021-02-19 10:33:49 +08:00
|
|
|
c2 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
envs,
|
|
|
|
VectorReplayBuffer(total_size=100, buffer_num=4, stack_num=4),
|
|
|
|
Logger.single_preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
c2.collect(n_episode=10)
|
2020-08-15 16:10:42 +08:00
|
|
|
batch, _ = c2.buffer.sample(10)
|
2020-04-28 20:56:02 +08:00
|
|
|
|
|
|
|
|
2020-07-13 00:24:31 +08:00
|
|
|
def test_collector_with_ma():
|
|
|
|
env = MyTestEnv(size=5, sleep=0, ma_rew=4)
|
|
|
|
policy = MyPolicy()
|
2021-09-03 05:05:04 +08:00
|
|
|
c0 = Collector(policy, env, ReplayBuffer(size=100), Logger.single_preprocess_fn)
|
2020-07-23 16:40:53 +08:00
|
|
|
# n_step=3 will collect a full episode
|
2023-08-25 23:40:56 +02:00
|
|
|
rew = c0.collect(n_step=3)["rews"]
|
2022-01-30 00:53:56 +08:00
|
|
|
assert len(rew) == 0
|
2023-08-25 23:40:56 +02:00
|
|
|
rew = c0.collect(n_episode=2)["rews"]
|
|
|
|
assert rew.shape == (2, 4)
|
|
|
|
assert np.all(rew == 1)
|
2021-09-03 05:05:04 +08:00
|
|
|
env_fns = [lambda x=i: MyTestEnv(size=x, sleep=0, ma_rew=4) for i in [2, 3, 4, 5]]
|
2020-08-19 15:00:24 +08:00
|
|
|
envs = DummyVectorEnv(env_fns)
|
2021-02-19 10:33:49 +08:00
|
|
|
c1 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
envs,
|
|
|
|
VectorReplayBuffer(total_size=100, buffer_num=4),
|
|
|
|
Logger.single_preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2023-08-25 23:40:56 +02:00
|
|
|
rew = c1.collect(n_step=12)["rews"]
|
2022-01-30 00:53:56 +08:00
|
|
|
assert rew.shape == (2, 4) and np.all(rew == 1), rew
|
2023-08-25 23:40:56 +02:00
|
|
|
rew = c1.collect(n_episode=8)["rews"]
|
|
|
|
assert rew.shape == (8, 4)
|
|
|
|
assert np.all(rew == 1)
|
2020-08-15 16:10:42 +08:00
|
|
|
batch, _ = c1.buffer.sample(10)
|
2020-07-13 00:24:31 +08:00
|
|
|
print(batch)
|
|
|
|
c0.buffer.update(c1.buffer)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(c0.buffer) in [42, 43]
|
|
|
|
if len(c0.buffer) == 42:
|
|
|
|
rew = [
|
2021-09-03 05:05:04 +08:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
2021-02-19 10:33:49 +08:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
rew = [
|
2021-09-03 05:05:04 +08:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
2021-02-19 10:33:49 +08:00
|
|
|
]
|
|
|
|
assert np.all(c0.buffer[:].rew == [[x] * 4 for x in rew])
|
|
|
|
assert np.all(c0.buffer[:].done == rew)
|
|
|
|
c2 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
envs,
|
|
|
|
VectorReplayBuffer(total_size=100, buffer_num=4, stack_num=4),
|
|
|
|
Logger.single_preprocess_fn,
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2023-08-25 23:40:56 +02:00
|
|
|
rew = c2.collect(n_episode=10)["rews"]
|
|
|
|
assert rew.shape == (10, 4)
|
|
|
|
assert np.all(rew == 1)
|
2020-08-15 16:10:42 +08:00
|
|
|
batch, _ = c2.buffer.sample(10)
|
2020-07-13 00:24:31 +08:00
|
|
|
|
|
|
|
|
2021-02-19 10:33:49 +08:00
|
|
|
def test_collector_with_atari_setting():
|
|
|
|
reference_obs = np.zeros([6, 4, 84, 84])
|
|
|
|
for i in range(6):
|
|
|
|
reference_obs[i, 3, np.arange(84), np.arange(84)] = i
|
|
|
|
reference_obs[i, 2, np.arange(84)] = i
|
|
|
|
reference_obs[i, 1, :, np.arange(84)] = i
|
|
|
|
reference_obs[i, 0] = i
|
|
|
|
|
|
|
|
# atari single buffer
|
|
|
|
env = MyTestEnv(size=5, sleep=0, array_state=True)
|
|
|
|
policy = MyPolicy()
|
|
|
|
c0 = Collector(policy, env, ReplayBuffer(size=100))
|
|
|
|
c0.collect(n_step=6)
|
|
|
|
c0.collect(n_episode=2)
|
|
|
|
assert c0.buffer.obs.shape == (100, 4, 84, 84)
|
|
|
|
assert c0.buffer.obs_next.shape == (100, 4, 84, 84)
|
|
|
|
assert len(c0.buffer) == 15
|
|
|
|
obs = np.zeros_like(c0.buffer.obs)
|
|
|
|
obs[np.arange(15)] = reference_obs[np.arange(15) % 5]
|
|
|
|
assert np.all(obs == c0.buffer.obs)
|
|
|
|
|
|
|
|
c1 = Collector(policy, env, ReplayBuffer(size=100, ignore_obs_next=True))
|
|
|
|
c1.collect(n_episode=3)
|
|
|
|
assert np.allclose(c0.buffer.obs, c1.buffer.obs)
|
|
|
|
with pytest.raises(AttributeError):
|
2023-08-25 23:40:56 +02:00
|
|
|
c1.buffer.obs_next # noqa: B018
|
2021-02-19 10:33:49 +08:00
|
|
|
assert np.all(reference_obs[[1, 2, 3, 4, 4] * 3] == c1.buffer[:].obs_next)
|
|
|
|
|
|
|
|
c2 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
env,
|
|
|
|
ReplayBuffer(size=100, ignore_obs_next=True, save_only_last_obs=True),
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
c2.collect(n_step=8)
|
|
|
|
assert c2.buffer.obs.shape == (100, 84, 84)
|
|
|
|
obs = np.zeros_like(c2.buffer.obs)
|
|
|
|
obs[np.arange(8)] = reference_obs[[0, 1, 2, 3, 4, 0, 1, 2], -1]
|
|
|
|
assert np.all(c2.buffer.obs == obs)
|
2023-08-25 23:40:56 +02:00
|
|
|
assert np.allclose(c2.buffer[:].obs_next, reference_obs[[1, 2, 3, 4, 4, 1, 2, 2], -1])
|
2021-02-19 10:33:49 +08:00
|
|
|
|
|
|
|
# atari multi buffer
|
2023-08-25 23:40:56 +02:00
|
|
|
env_fns = [lambda x=i: MyTestEnv(size=x, sleep=0, array_state=True) for i in [2, 3, 4, 5]]
|
2021-02-19 10:33:49 +08:00
|
|
|
envs = DummyVectorEnv(env_fns)
|
2021-09-03 05:05:04 +08:00
|
|
|
c3 = Collector(policy, envs, VectorReplayBuffer(total_size=100, buffer_num=4))
|
2021-02-19 10:33:49 +08:00
|
|
|
c3.collect(n_step=12)
|
|
|
|
result = c3.collect(n_episode=9)
|
2023-08-25 23:40:56 +02:00
|
|
|
assert result["n/ep"] == 9
|
|
|
|
assert result["n/st"] == 23
|
2021-02-19 10:33:49 +08:00
|
|
|
assert c3.buffer.obs.shape == (100, 4, 84, 84)
|
|
|
|
obs = np.zeros_like(c3.buffer.obs)
|
|
|
|
obs[np.arange(8)] = reference_obs[[0, 1, 0, 1, 0, 1, 0, 1]]
|
|
|
|
obs[np.arange(25, 34)] = reference_obs[[0, 1, 2, 0, 1, 2, 0, 1, 2]]
|
|
|
|
obs[np.arange(50, 58)] = reference_obs[[0, 1, 2, 3, 0, 1, 2, 3]]
|
|
|
|
obs[np.arange(75, 85)] = reference_obs[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]]
|
|
|
|
assert np.all(obs == c3.buffer.obs)
|
|
|
|
obs_next = np.zeros_like(c3.buffer.obs_next)
|
|
|
|
obs_next[np.arange(8)] = reference_obs[[1, 2, 1, 2, 1, 2, 1, 2]]
|
|
|
|
obs_next[np.arange(25, 34)] = reference_obs[[1, 2, 3, 1, 2, 3, 1, 2, 3]]
|
|
|
|
obs_next[np.arange(50, 58)] = reference_obs[[1, 2, 3, 4, 1, 2, 3, 4]]
|
|
|
|
obs_next[np.arange(75, 85)] = reference_obs[[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]]
|
|
|
|
assert np.all(obs_next == c3.buffer.obs_next)
|
|
|
|
c4 = Collector(
|
2023-08-25 23:40:56 +02:00
|
|
|
policy,
|
|
|
|
envs,
|
2021-09-03 05:05:04 +08:00
|
|
|
VectorReplayBuffer(
|
|
|
|
total_size=100,
|
|
|
|
buffer_num=4,
|
|
|
|
stack_num=4,
|
|
|
|
ignore_obs_next=True,
|
2023-08-25 23:40:56 +02:00
|
|
|
save_only_last_obs=True,
|
|
|
|
),
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
c4.collect(n_step=12)
|
|
|
|
result = c4.collect(n_episode=9)
|
2023-08-25 23:40:56 +02:00
|
|
|
assert result["n/ep"] == 9
|
|
|
|
assert result["n/st"] == 23
|
2021-02-19 10:33:49 +08:00
|
|
|
assert c4.buffer.obs.shape == (100, 84, 84)
|
|
|
|
obs = np.zeros_like(c4.buffer.obs)
|
|
|
|
slice_obs = reference_obs[:, -1]
|
|
|
|
obs[np.arange(8)] = slice_obs[[0, 1, 0, 1, 0, 1, 0, 1]]
|
|
|
|
obs[np.arange(25, 34)] = slice_obs[[0, 1, 2, 0, 1, 2, 0, 1, 2]]
|
|
|
|
obs[np.arange(50, 58)] = slice_obs[[0, 1, 2, 3, 0, 1, 2, 3]]
|
|
|
|
obs[np.arange(75, 85)] = slice_obs[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]]
|
|
|
|
assert np.all(c4.buffer.obs == obs)
|
|
|
|
obs_next = np.zeros([len(c4.buffer), 4, 84, 84])
|
2021-09-03 05:05:04 +08:00
|
|
|
ref_index = np.array(
|
|
|
|
[
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
4,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
4,
|
2023-08-25 23:40:56 +02:00
|
|
|
],
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
obs_next[:, -1] = slice_obs[ref_index]
|
|
|
|
ref_index -= 1
|
|
|
|
ref_index[ref_index < 0] = 0
|
|
|
|
obs_next[:, -2] = slice_obs[ref_index]
|
|
|
|
ref_index -= 1
|
|
|
|
ref_index[ref_index < 0] = 0
|
|
|
|
obs_next[:, -3] = slice_obs[ref_index]
|
|
|
|
ref_index -= 1
|
|
|
|
ref_index[ref_index < 0] = 0
|
|
|
|
obs_next[:, -4] = slice_obs[ref_index]
|
|
|
|
assert np.all(obs_next == c4.buffer[:].obs_next)
|
|
|
|
|
2021-09-03 05:05:04 +08:00
|
|
|
buf = ReplayBuffer(100, stack_num=4, ignore_obs_next=True, save_only_last_obs=True)
|
2021-02-19 10:33:49 +08:00
|
|
|
c5 = Collector(policy, envs, CachedReplayBuffer(buf, 4, 10))
|
|
|
|
result_ = c5.collect(n_step=12)
|
2023-08-25 23:40:56 +02:00
|
|
|
assert len(buf) == 5
|
|
|
|
assert len(c5.buffer) == 12
|
2021-02-19 10:33:49 +08:00
|
|
|
result = c5.collect(n_episode=9)
|
2023-08-25 23:40:56 +02:00
|
|
|
assert result["n/ep"] == 9
|
|
|
|
assert result["n/st"] == 23
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(buf) == 35
|
2021-09-03 05:05:04 +08:00
|
|
|
assert np.all(
|
2023-08-25 23:40:56 +02:00
|
|
|
buf.obs[: len(buf)]
|
|
|
|
== slice_obs[
|
|
|
|
[
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
]
|
|
|
|
],
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
|
|
|
assert np.all(
|
2023-08-25 23:40:56 +02:00
|
|
|
buf[:].obs_next[:, -1]
|
|
|
|
== slice_obs[
|
|
|
|
[
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
4,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
4,
|
|
|
|
]
|
|
|
|
],
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-02-19 10:33:49 +08:00
|
|
|
assert len(buf) == len(c5.buffer)
|
|
|
|
|
|
|
|
# test buffer=None
|
|
|
|
c6 = Collector(policy, envs)
|
|
|
|
result1 = c6.collect(n_step=12)
|
|
|
|
for key in ["n/ep", "n/st", "rews", "lens"]:
|
|
|
|
assert np.allclose(result1[key], result_[key])
|
|
|
|
result2 = c6.collect(n_episode=9)
|
|
|
|
for key in ["n/ep", "n/st", "rews", "lens"]:
|
|
|
|
assert np.allclose(result2[key], result[key])
|
|
|
|
|
|
|
|
|
2022-06-27 18:52:21 -04:00
|
|
|
@pytest.mark.skipif(envpool is None, reason="EnvPool doesn't support this platform")
|
|
|
|
def test_collector_envpool_gym_reset_return_info():
|
Improved typing and reduced duplication (#912)
# Goals of the PR
The PR introduces **no changes to functionality**, apart from improved
input validation here and there. The main goals are to reduce some
complexity of the code, to improve types and IDE completions, and to
extend documentation and block comments where appropriate. Because of
the change to the trainer interfaces, many files are affected (more
details below), but still the overall changes are "small" in a certain
sense.
## Major Change 1 - BatchProtocol
**TL;DR:** One can now annotate which fields the batch is expected to
have on input params and which fields a returned batch has. Should be
useful for reading the code. getting meaningful IDE support, and
catching bugs with mypy. This annotation strategy will continue to work
if Batch is replaced by TensorDict or by something else.
**In more detail:** Batch itself has no fields and using it for
annotations is of limited informational power. Batches with fields are
not separate classes but instead instances of Batch directly, so there
is no type that could be used for annotation. Fortunately, python
`Protocol` is here for the rescue. With these changes we can now do
things like
```python
class ActionBatchProtocol(BatchProtocol):
logits: Sequence[Union[tuple, torch.Tensor]]
dist: torch.distributions.Distribution
act: torch.Tensor
state: Optional[torch.Tensor]
class RolloutBatchProtocol(BatchProtocol):
obs: torch.Tensor
obs_next: torch.Tensor
info: Dict[str, Any]
rew: torch.Tensor
terminated: torch.Tensor
truncated: torch.Tensor
class PGPolicy(BasePolicy):
...
def forward(
self,
batch: RolloutBatchProtocol,
state: Optional[Union[dict, Batch, np.ndarray]] = None,
**kwargs: Any,
) -> ActionBatchProtocol:
```
The IDE and mypy are now very helpful in finding errors and in
auto-completion, whereas before the tools couldn't assist in that at
all.
## Major Change 2 - remove duplication in trainer package
**TL;DR:** There was a lot of duplication between `BaseTrainer` and its
subclasses. Even worse, it was almost-duplication. There was also
interface fragmentation through things like `onpolicy_trainer`. Now this
duplication is gone and all downstream code was adjusted.
**In more detail:** Since this change affects a lot of code, I would
like to explain why I thought it to be necessary.
1. The subclasses of `BaseTrainer` just duplicated docstrings and
constructors. What's worse, they changed the order of args there, even
turning some kwargs of BaseTrainer into args. They also had the arg
`learning_type` which was passed as kwarg to the base class and was
unused there. This made things difficult to maintain, and in fact some
errors were already present in the duplicated docstrings.
2. The "functions" a la `onpolicy_trainer`, which just called the
`OnpolicyTrainer.run`, not only introduced interface fragmentation but
also completely obfuscated the docstring and interfaces. They themselves
had no dosctring and the interface was just `*args, **kwargs`, which
makes it impossible to understand what they do and which things can be
passed without reading their implementation, then reading the docstring
of the associated class, etc. Needless to say, mypy and IDEs provide no
support with such functions. Nevertheless, they were used everywhere in
the code-base. I didn't find the sacrifices in clarity and complexity
justified just for the sake of not having to write `.run()` after
instantiating a trainer.
3. The trainers are all very similar to each other. As for my
application I needed a new trainer, I wanted to understand their
structure. The similarity, however, was hard to discover since they were
all in separate modules and there was so much duplication. I kept
staring at the constructors for a while until I figured out that
essentially no changes to the superclass were introduced. Now they are
all in the same module and the similarities/differences between them are
much easier to grasp (in my opinion)
4. Because of (1), I had to manually change and check a lot of code,
which was very tedious and boring. This kind of work won't be necessary
in the future, since now IDEs can be used for changing signatures,
renaming args and kwargs, changing class names and so on.
I have some more reasons, but maybe the above ones are convincing
enough.
## Minor changes: improved input validation and types
I added input validation for things like `state` and `action_scaling`
(which only makes sense for continuous envs). After adding this, some
tests failed to pass this validation. There I added
`action_scaling=isinstance(env.action_space, Box)`, after which tests
were green. I don't know why the tests were green before, since action
scaling doesn't make sense for discrete actions. I guess some aspect was
not tested and didn't crash.
I also added Literal in some places, in particular for
`action_bound_method`. Now it is no longer allowed to pass an empty
string, instead one should pass `None`. Also here there is input
validation with clear error messages.
@Trinkle23897 The functional tests are green. I didn't want to fix the
formatting, since it will change in the next PR that will solve #914
anyway. I also found a whole bunch of code in `docs/_static`, which I
just deleted (shouldn't it be copied from the sources during docs build
instead of committed?). I also haven't adjusted the documentation yet,
which atm still mentions the trainers of the type
`onpolicy_trainer(...)` instead of `OnpolicyTrainer(...).run()`
## Breaking Changes
The adjustments to the trainer package introduce breaking changes as
duplicated interfaces are deleted. However, it should be very easy for
users to adjust to them
---------
Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-08-22 18:54:46 +02:00
|
|
|
envs = envpool.make_gymnasium("Pendulum-v1", num_envs=4, gym_reset_return_info=True)
|
2022-06-27 18:52:21 -04:00
|
|
|
policy = MyPolicy(action_shape=(len(envs), 1))
|
|
|
|
|
|
|
|
c0 = Collector(
|
|
|
|
policy,
|
|
|
|
envs,
|
|
|
|
VectorReplayBuffer(len(envs) * 10, len(envs)),
|
2023-08-25 23:40:56 +02:00
|
|
|
exploration_noise=True,
|
2022-06-27 18:52:21 -04:00
|
|
|
)
|
|
|
|
c0.collect(n_step=8)
|
|
|
|
env_ids = np.zeros(len(envs) * 10)
|
|
|
|
env_ids[[0, 1, 10, 11, 20, 21, 30, 31]] = [0, 0, 1, 1, 2, 2, 3, 3]
|
|
|
|
assert np.allclose(c0.buffer.info["env_id"], env_ids)
|
|
|
|
|
|
|
|
|
2023-08-25 23:40:56 +02:00
|
|
|
if __name__ == "__main__":
|
2022-06-27 18:52:21 -04:00
|
|
|
test_collector(gym_reset_kwargs=None)
|
2023-08-25 23:40:56 +02:00
|
|
|
test_collector(gym_reset_kwargs={})
|
2020-04-28 20:56:02 +08:00
|
|
|
test_collector_with_dict_state()
|
2020-07-13 00:24:31 +08:00
|
|
|
test_collector_with_ma()
|
2021-02-19 10:33:49 +08:00
|
|
|
test_collector_with_atari_setting()
|
2022-06-27 18:52:21 -04:00
|
|
|
test_collector_with_async(gym_reset_kwargs=None)
|
2023-08-25 23:40:56 +02:00
|
|
|
test_collector_with_async(gym_reset_kwargs={"return_info": True})
|
2022-06-27 18:52:21 -04:00
|
|
|
test_collector_envpool_gym_reset_return_info()
|