2021-07-28 10:40:04 -04:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
|
|
|
|
import ray
|
|
|
|
from ray import tune
|
|
|
|
from ray.rllib.examples.env.mock_env import MockVectorEnv
|
|
|
|
from ray.rllib.utils.framework import try_import_tf, try_import_torch
|
|
|
|
from ray.rllib.utils.test_utils import check_learning_achieved
|
|
|
|
|
|
|
|
tf1, tf, tfv = try_import_tf()
|
|
|
|
torch, nn = try_import_torch()
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--run", type=str, default="PPO", help="The RLlib-registered algorithm to use."
|
|
|
|
)
|
2021-07-28 10:40:04 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--framework",
|
|
|
|
choices=["tf", "tf2", "tfe", "torch"],
|
|
|
|
default="tf",
|
2022-01-29 18:41:57 -08:00
|
|
|
help="The DL framework specifier.",
|
|
|
|
)
|
2021-07-28 10:40:04 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--as-test",
|
|
|
|
action="store_true",
|
|
|
|
help="Whether this script should be run as a test: --stop-reward must "
|
2022-01-29 18:41:57 -08:00
|
|
|
"be achieved within --stop-timesteps AND --stop-iters.",
|
|
|
|
)
|
2021-07-28 10:40:04 -04:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--stop-iters", type=int, default=50, help="Number of iterations to train."
|
|
|
|
)
|
2021-07-28 10:40:04 -04:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--stop-timesteps", type=int, default=100000, help="Number of timesteps to train."
|
|
|
|
)
|
2021-07-28 10:40:04 -04:00
|
|
|
parser.add_argument(
|
2022-01-29 18:41:57 -08:00
|
|
|
"--stop-reward", type=float, default=35.0, help="Reward at which we stop training."
|
|
|
|
)
|
2021-07-28 10:40:04 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
|
|
|
ray.init()
|
|
|
|
|
|
|
|
# episode-len=100
|
|
|
|
# num-envs=4 (note that these are fake-envs as the MockVectorEnv only
|
|
|
|
# carries a single CartPole sub-env in it).
|
|
|
|
tune.register_env("custom_vec_env", lambda env_ctx: MockVectorEnv(100, 4))
|
|
|
|
|
|
|
|
config = {
|
|
|
|
"env": "custom_vec_env",
|
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
|
|
|
"num_workers": 2, # parallelism
|
|
|
|
"framework": args.framework,
|
|
|
|
}
|
|
|
|
|
|
|
|
stop = {
|
|
|
|
"training_iteration": args.stop_iters,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
|
|
|
"episode_reward_mean": args.stop_reward,
|
|
|
|
}
|
|
|
|
|
|
|
|
results = tune.run(args.run, config=config, stop=stop, verbose=1)
|
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
ray.shutdown()
|