2019-04-07 04:58:14 +02:00
|
|
|
import gym
|
|
|
|
import numpy as np
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
2020-05-01 22:59:34 +02:00
|
|
|
from ray.rllib.env.external_multi_agent_env import ExternalMultiAgentEnv
|
2019-06-03 06:49:24 +08:00
|
|
|
from ray.rllib.evaluation.rollout_worker import RolloutWorker
|
2020-12-09 01:41:45 +01:00
|
|
|
from ray.rllib.evaluation.tests.test_rollout_worker import MockPolicy
|
2020-05-21 10:16:18 -07:00
|
|
|
from ray.rllib.examples.env.multi_agent import BasicMultiAgent
|
2021-08-21 17:05:48 +02:00
|
|
|
from ray.rllib.policy.sample_batch import SampleBatch
|
2019-04-07 04:58:14 +02:00
|
|
|
from ray.rllib.tests.test_external_env import make_simple_serving
|
|
|
|
|
|
|
|
SimpleMultiServing = make_simple_serving(True, ExternalMultiAgentEnv)
|
|
|
|
|
|
|
|
|
|
|
|
class TestExternalMultiAgentEnv(unittest.TestCase):
|
2020-05-27 16:19:13 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
2021-08-21 17:05:48 +02:00
|
|
|
ray.init()
|
2020-03-12 04:39:47 +01:00
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
2020-03-12 04:39:47 +01:00
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_external_multi_agent_env_complete_episodes(self):
|
2019-04-07 04:58:14 +02:00
|
|
|
agents = 4
|
2019-06-03 06:49:24 +08:00
|
|
|
ev = RolloutWorker(
|
2019-04-07 04:58:14 +02:00
|
|
|
env_creator=lambda _: SimpleMultiServing(BasicMultiAgent(agents)),
|
2020-10-15 18:21:30 +02:00
|
|
|
policy_spec=MockPolicy,
|
2020-03-14 12:05:04 -07:00
|
|
|
rollout_fragment_length=40,
|
2022-01-29 18:41:57 -08:00
|
|
|
batch_mode="complete_episodes",
|
|
|
|
)
|
2019-04-07 04:58:14 +02:00
|
|
|
for _ in range(3):
|
|
|
|
batch = ev.sample()
|
|
|
|
self.assertEqual(batch.count, 40)
|
2022-01-29 18:41:57 -08:00
|
|
|
self.assertEqual(len(np.unique(batch[SampleBatch.AGENT_INDEX])), agents)
|
2019-04-07 04:58:14 +02:00
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_external_multi_agent_env_truncate_episodes(self):
|
2019-04-07 04:58:14 +02:00
|
|
|
agents = 4
|
2019-06-03 06:49:24 +08:00
|
|
|
ev = RolloutWorker(
|
2019-04-07 04:58:14 +02:00
|
|
|
env_creator=lambda _: SimpleMultiServing(BasicMultiAgent(agents)),
|
2020-10-15 18:21:30 +02:00
|
|
|
policy_spec=MockPolicy,
|
2020-03-14 12:05:04 -07:00
|
|
|
rollout_fragment_length=40,
|
2022-01-29 18:41:57 -08:00
|
|
|
batch_mode="truncate_episodes",
|
|
|
|
)
|
2019-04-07 04:58:14 +02:00
|
|
|
for _ in range(3):
|
|
|
|
batch = ev.sample()
|
|
|
|
self.assertEqual(batch.count, 160)
|
2022-01-29 18:41:57 -08:00
|
|
|
self.assertEqual(len(np.unique(batch[SampleBatch.AGENT_INDEX])), agents)
|
2019-04-07 04:58:14 +02:00
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_external_multi_agent_env_sample(self):
|
2019-04-07 04:58:14 +02:00
|
|
|
agents = 2
|
|
|
|
act_space = gym.spaces.Discrete(2)
|
|
|
|
obs_space = gym.spaces.Discrete(2)
|
2019-06-03 06:49:24 +08:00
|
|
|
ev = RolloutWorker(
|
2019-04-07 04:58:14 +02:00
|
|
|
env_creator=lambda _: SimpleMultiServing(BasicMultiAgent(agents)),
|
2020-10-15 18:21:30 +02:00
|
|
|
policy_spec={
|
2019-05-20 16:46:05 -07:00
|
|
|
"p0": (MockPolicy, obs_space, act_space, {}),
|
|
|
|
"p1": (MockPolicy, obs_space, act_space, {}),
|
2019-04-07 04:58:14 +02:00
|
|
|
},
|
2021-06-21 13:46:01 +02:00
|
|
|
policy_mapping_fn=lambda aid, **kwargs: "p{}".format(aid % 2),
|
2022-01-29 18:41:57 -08:00
|
|
|
rollout_fragment_length=50,
|
|
|
|
)
|
2019-04-07 04:58:14 +02:00
|
|
|
batch = ev.sample()
|
|
|
|
self.assertEqual(batch.count, 50)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-03-12 04:39:47 +01:00
|
|
|
import pytest
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|