2019-08-08 14:03:28 -07:00
|
|
|
"""The two-step game from QMIX: https://arxiv.org/pdf/1803.11485.pdf
|
|
|
|
|
|
|
|
Configurations you can try:
|
|
|
|
- normal policy gradients (PG)
|
|
|
|
- contrib/MADDPG
|
|
|
|
- QMIX
|
|
|
|
|
|
|
|
See also: centralized_critic.py for centralized critic PPO on this game.
|
|
|
|
"""
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
import argparse
|
2019-10-08 13:18:07 -07:00
|
|
|
from gym.spaces import Tuple, MultiDiscrete, Dict, Discrete
|
2020-10-02 23:07:44 +02:00
|
|
|
import os
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
import ray
|
2019-03-30 14:07:50 -07:00
|
|
|
from ray import tune
|
|
|
|
from ray.tune import register_env, grid_search
|
2020-05-01 22:59:34 +02:00
|
|
|
from ray.rllib.env.multi_agent_env import ENV_STATE
|
|
|
|
from ray.rllib.examples.env.two_step_game import TwoStepGame
|
2020-05-12 08:23:10 +02:00
|
|
|
from ray.rllib.utils.test_utils import check_learning_achieved
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--run",
|
|
|
|
type=str,
|
|
|
|
default="PG",
|
|
|
|
help="The RLlib-registered algorithm to use.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--framework",
|
|
|
|
choices=["tf", "tf2", "tfe", "torch"],
|
|
|
|
default="tf",
|
|
|
|
help="The DL framework specifier.")
|
2020-02-15 23:50:44 +01:00
|
|
|
parser.add_argument("--num-cpus", type=int, default=0)
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--as-test",
|
|
|
|
action="store_true",
|
|
|
|
help="Whether this script should be run as a test: --stop-reward must "
|
|
|
|
"be achieved within --stop-timesteps AND --stop-iters.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-iters",
|
|
|
|
type=int,
|
|
|
|
default=200,
|
|
|
|
help="Number of iterations to train.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-timesteps",
|
|
|
|
type=int,
|
|
|
|
default=50000,
|
|
|
|
help="Number of timesteps to train.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-reward",
|
|
|
|
type=float,
|
|
|
|
default=7.0,
|
|
|
|
help="Reward at which we stop training.")
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
grouping = {
|
2019-08-06 19:22:06 -04:00
|
|
|
"group_1": [0, 1],
|
2018-12-18 10:40:01 -08:00
|
|
|
}
|
|
|
|
obs_space = Tuple([
|
2019-10-08 13:18:07 -07:00
|
|
|
Dict({
|
|
|
|
"obs": MultiDiscrete([2, 2, 2, 3]),
|
|
|
|
ENV_STATE: MultiDiscrete([2, 2, 2])
|
|
|
|
}),
|
|
|
|
Dict({
|
|
|
|
"obs": MultiDiscrete([2, 2, 2, 3]),
|
|
|
|
ENV_STATE: MultiDiscrete([2, 2, 2])
|
|
|
|
}),
|
2018-12-18 10:40:01 -08:00
|
|
|
])
|
|
|
|
act_space = Tuple([
|
|
|
|
TwoStepGame.action_space,
|
|
|
|
TwoStepGame.action_space,
|
|
|
|
])
|
|
|
|
register_env(
|
|
|
|
"grouped_twostep",
|
|
|
|
lambda config: TwoStepGame(config).with_agent_groups(
|
|
|
|
grouping, obs_space=obs_space, act_space=act_space))
|
|
|
|
|
2019-08-06 19:22:06 -04:00
|
|
|
if args.run == "contrib/MADDPG":
|
|
|
|
obs_space_dict = {
|
2019-10-08 13:18:07 -07:00
|
|
|
"agent_1": Discrete(6),
|
|
|
|
"agent_2": Discrete(6),
|
2019-08-06 19:22:06 -04:00
|
|
|
}
|
|
|
|
act_space_dict = {
|
|
|
|
"agent_1": TwoStepGame.action_space,
|
|
|
|
"agent_2": TwoStepGame.action_space,
|
|
|
|
}
|
|
|
|
config = {
|
|
|
|
"learning_starts": 100,
|
|
|
|
"env_config": {
|
|
|
|
"actions_are_logits": True,
|
|
|
|
},
|
|
|
|
"multiagent": {
|
|
|
|
"policies": {
|
2019-10-08 13:18:07 -07:00
|
|
|
"pol1": (None, Discrete(6), TwoStepGame.action_space, {
|
|
|
|
"agent_id": 0,
|
|
|
|
}),
|
|
|
|
"pol2": (None, Discrete(6), TwoStepGame.action_space, {
|
|
|
|
"agent_id": 1,
|
|
|
|
}),
|
2019-08-06 19:22:06 -04:00
|
|
|
},
|
2021-06-18 12:21:49 -07:00
|
|
|
"policy_mapping_fn": lambda x: "pol1" if x == 0 else "pol2",
|
2019-08-06 19:22:06 -04:00
|
|
|
},
|
2021-05-18 13:18:12 +02:00
|
|
|
"framework": args.framework,
|
2020-10-02 23:07:44 +02:00
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
2019-08-06 19:22:06 -04:00
|
|
|
}
|
|
|
|
group = False
|
|
|
|
elif args.run == "QMIX":
|
2018-12-18 10:40:01 -08:00
|
|
|
config = {
|
2020-03-14 12:05:04 -07:00
|
|
|
"rollout_fragment_length": 4,
|
2018-12-18 10:40:01 -08:00
|
|
|
"train_batch_size": 32,
|
2020-07-17 12:14:34 +02:00
|
|
|
"exploration_config": {
|
|
|
|
"epsilon_timesteps": 5000,
|
|
|
|
"final_epsilon": 0.05,
|
|
|
|
},
|
2018-12-18 10:40:01 -08:00
|
|
|
"num_workers": 0,
|
|
|
|
"mixer": grid_search([None, "qmix", "vdn"]),
|
2019-10-08 13:18:07 -07:00
|
|
|
"env_config": {
|
|
|
|
"separate_state_space": True,
|
|
|
|
"one_hot_state_encoding": True
|
|
|
|
},
|
2020-10-02 23:07:44 +02:00
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
2021-05-18 13:18:12 +02:00
|
|
|
"framework": args.framework,
|
2018-12-18 10:40:01 -08:00
|
|
|
}
|
2019-02-23 21:23:40 -08:00
|
|
|
group = True
|
2018-12-18 10:40:01 -08:00
|
|
|
else:
|
2020-10-02 23:07:44 +02:00
|
|
|
config = {
|
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
2021-05-18 13:18:12 +02:00
|
|
|
"framework": args.framework,
|
2020-10-02 23:07:44 +02:00
|
|
|
}
|
2019-02-23 21:23:40 -08:00
|
|
|
group = False
|
2018-12-18 10:40:01 -08:00
|
|
|
|
2020-02-15 23:50:44 +01:00
|
|
|
ray.init(num_cpus=args.num_cpus or None)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
stop = {
|
|
|
|
"episode_reward_mean": args.stop_reward,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
2021-05-18 13:18:12 +02:00
|
|
|
"training_iteration": args.stop_iters,
|
2020-05-12 08:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config = dict(config, **{
|
|
|
|
"env": "grouped_twostep" if group else TwoStepGame,
|
|
|
|
})
|
|
|
|
|
2020-07-17 12:14:34 +02:00
|
|
|
results = tune.run(args.run, stop=stop, config=config, verbose=1)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
|
|
|
|
ray.shutdown()
|