2020-07-06 22:32:26 -06:00
|
|
|
import unittest
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
import ray
|
|
|
|
from ray.tune.registry import register_env
|
|
|
|
from ray.rllib.env import PettingZooEnv
|
2021-02-08 12:05:16 +01:00
|
|
|
from ray.rllib.agents.registry import get_trainer_class
|
2020-07-06 22:32:26 -06:00
|
|
|
|
2020-11-09 19:09:49 -05:00
|
|
|
from pettingzoo.mpe import simple_spread_v2
|
2020-07-06 22:32:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
class TestPettingZooEnv(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_pettingzoo_env(self):
|
2020-11-09 19:09:49 -05:00
|
|
|
register_env("simple_spread",
|
|
|
|
lambda _: PettingZooEnv(simple_spread_v2.env()))
|
2020-07-06 22:32:26 -06:00
|
|
|
|
2021-02-08 12:05:16 +01:00
|
|
|
agent_class = get_trainer_class("PPO")
|
2020-07-06 22:32:26 -06:00
|
|
|
|
|
|
|
config = deepcopy(agent_class._default_config)
|
|
|
|
|
2021-07-13 09:57:15 -07:00
|
|
|
test_env = PettingZooEnv(simple_spread_v2.env())
|
|
|
|
obs_space = test_env.observation_space
|
|
|
|
act_space = test_env.action_space
|
|
|
|
test_env.close()
|
|
|
|
|
2020-07-06 22:32:26 -06:00
|
|
|
config["multiagent"] = {
|
2021-07-13 09:57:15 -07:00
|
|
|
"policies": {
|
|
|
|
# the first tuple value is None -> uses default policy
|
|
|
|
"av": (None, obs_space, act_space, {}),
|
|
|
|
},
|
2021-06-21 13:46:01 +02:00
|
|
|
"policy_mapping_fn": lambda agent_id, episode, **kwargs: "av"
|
2020-07-06 22:32:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
config["log_level"] = "DEBUG"
|
|
|
|
config["num_workers"] = 0
|
|
|
|
config["rollout_fragment_length"] = 30
|
|
|
|
config["train_batch_size"] = 200
|
|
|
|
config["horizon"] = 200 # After n steps, force reset simulation
|
|
|
|
config["no_done_at_end"] = False
|
|
|
|
|
2020-11-09 19:09:49 -05:00
|
|
|
agent = agent_class(env="simple_spread", config=config)
|
2020-07-06 22:32:26 -06:00
|
|
|
agent.train()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|