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)
|
2022-05-06 12:35:21 +02:00
|
|
|
- MADDPG
|
2019-08-08 14:03:28 -07:00
|
|
|
- QMIX
|
|
|
|
|
|
|
|
See also: centralized_critic.py for centralized critic PPO on this game.
|
|
|
|
"""
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
import argparse
|
2021-07-15 05:51:24 -04:00
|
|
|
from gym.spaces import Dict, Discrete, Tuple, MultiDiscrete
|
2022-05-12 16:12:42 +02:00
|
|
|
import logging
|
2020-10-02 23:07:44 +02:00
|
|
|
import os
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
import ray
|
2022-07-27 04:12:59 -07:00
|
|
|
from ray import air, tune
|
2022-01-04 08:54:41 +01:00
|
|
|
from ray.tune import register_env
|
2022-05-19 09:30:42 -07:00
|
|
|
from ray.rllib.algorithms.qmix import QMixConfig
|
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
|
2021-07-15 05:51:24 -04:00
|
|
|
from ray.rllib.policy.policy import PolicySpec
|
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
|
|
|
|
2022-05-12 16:12:42 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
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)
|
2022-01-04 08:54:41 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--mixer",
|
|
|
|
type=str,
|
|
|
|
default="qmix",
|
|
|
|
choices=["qmix", "vdn", "none"],
|
|
|
|
help="The mixer model to use.",
|
|
|
|
)
|
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=70000, help="Number of timesteps to train."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-reward", type=float, default=8.0, help="Reward at which we stop training."
|
|
|
|
)
|
2021-09-21 22:00:14 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--local-mode",
|
|
|
|
action="store_true",
|
|
|
|
help="Init Ray in local mode for easier debugging.",
|
|
|
|
)
|
2018-12-18 10:40:01 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-09-21 22:00:14 +02:00
|
|
|
ray.init(num_cpus=args.num_cpus or None, local_mode=args.local_mode)
|
2021-06-19 22:42:00 +02:00
|
|
|
|
2022-05-12 16:12:42 +02:00
|
|
|
if args.run == "contrib/MADDPG":
|
|
|
|
logger.warning(
|
|
|
|
"`contrib/MADDPG` is not longer a valid algorithm descriptor! "
|
|
|
|
"Use `MADDPG` instead."
|
|
|
|
)
|
|
|
|
args.run = "MADDPG"
|
|
|
|
|
2018-12-18 10:40:01 -08:00
|
|
|
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
|
2022-01-29 18:41:57 -08:00
|
|
|
),
|
2018-12-18 10:40:01 -08:00
|
|
|
)
|
|
|
|
|
2022-05-06 12:35:21 +02:00
|
|
|
if args.run == "MADDPG":
|
2021-07-15 05:51:24 -04:00
|
|
|
obs_space = Discrete(6)
|
|
|
|
act_space = TwoStepGame.action_space
|
2019-08-06 19:22:06 -04:00
|
|
|
config = {
|
2022-05-16 00:45:32 -07:00
|
|
|
"env": TwoStepGame,
|
2019-08-06 19:22:06 -04:00
|
|
|
"env_config": {
|
|
|
|
"actions_are_logits": True,
|
|
|
|
},
|
2022-08-11 13:07:30 +02:00
|
|
|
"num_steps_sampled_before_learning_starts": 100,
|
2019-08-06 19:22:06 -04:00
|
|
|
"multiagent": {
|
|
|
|
"policies": {
|
2021-07-15 05:51:24 -04:00
|
|
|
"pol1": PolicySpec(
|
|
|
|
observation_space=obs_space,
|
|
|
|
action_space=act_space,
|
|
|
|
config={"agent_id": 0},
|
|
|
|
),
|
|
|
|
"pol2": PolicySpec(
|
|
|
|
observation_space=obs_space,
|
|
|
|
action_space=act_space,
|
|
|
|
config={"agent_id": 1},
|
|
|
|
),
|
2019-08-06 19:22:06 -04:00
|
|
|
},
|
2021-06-21 13:46:01 +02:00
|
|
|
"policy_mapping_fn": (lambda aid, **kwargs: "pol2" if aid else "pol1"),
|
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
|
|
|
}
|
|
|
|
elif args.run == "QMIX":
|
2022-05-16 00:45:32 -07:00
|
|
|
config = (
|
|
|
|
QMixConfig()
|
|
|
|
.training(mixer=args.mixer, train_batch_size=32)
|
|
|
|
.rollouts(num_rollout_workers=0, rollout_fragment_length=4)
|
|
|
|
.exploration(
|
|
|
|
exploration_config={
|
|
|
|
"final_epsilon": 0.0,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.environment(
|
|
|
|
env="grouped_twostep",
|
|
|
|
env_config={
|
|
|
|
"separate_state_space": True,
|
|
|
|
"one_hot_state_encoding": True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.resources(num_gpus=int(os.environ.get("RLLIB_NUM_GPUS", "0")))
|
|
|
|
)
|
|
|
|
config = config.to_dict()
|
2018-12-18 10:40:01 -08:00
|
|
|
else:
|
2020-10-02 23:07:44 +02:00
|
|
|
config = {
|
2022-05-16 00:45:32 -07:00
|
|
|
"env": TwoStepGame,
|
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,
|
2020-10-02 23:07:44 +02:00
|
|
|
}
|
2018-12-18 10:40:01 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-27 04:12:59 -07:00
|
|
|
results = tune.Tuner(
|
|
|
|
args.run,
|
|
|
|
run_config=air.RunConfig(stop=stop, verbose=2),
|
|
|
|
param_space=config,
|
|
|
|
).fit()
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
|
|
|
|
ray.shutdown()
|