2021-08-20 12:17:13 +02:00
|
|
|
import argparse
|
2021-10-26 10:11:39 -07:00
|
|
|
import os
|
2021-08-20 12:17:13 +02:00
|
|
|
|
2021-10-26 10:11:39 -07:00
|
|
|
import ray
|
2021-08-20 12:17:13 +02:00
|
|
|
from ray.rllib.agents.trainer_template import build_trainer
|
|
|
|
from ray.rllib.examples.policy.bare_metal_policy_with_custom_view_reqs \
|
|
|
|
import BareMetalPolicyWithCustomViewReqs
|
2021-10-26 10:11:39 -07:00
|
|
|
from ray import tune
|
2021-08-20 12:17:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_cli_args():
|
|
|
|
"""Create CLI parser and return parsed arguments"""
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
# general args
|
|
|
|
parser.add_argument(
|
|
|
|
"--run", default="PPO", help="The RLlib-registered algorithm to use.")
|
|
|
|
parser.add_argument("--num-cpus", type=int, default=3)
|
2021-10-26 10:11:39 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"--stop-iters",
|
|
|
|
type=int,
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
default=1,
|
2021-10-26 10:11:39 -07:00
|
|
|
help="Number of iterations to train.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-timesteps",
|
|
|
|
type=int,
|
|
|
|
default=100000,
|
|
|
|
help="Number of timesteps to train.")
|
2021-08-20 12:17:13 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--local-mode",
|
|
|
|
action="store_true",
|
|
|
|
help="Init Ray in local mode for easier debugging.")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
print(f"Running with following CLI args: {args}")
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = get_cli_args()
|
|
|
|
|
|
|
|
ray.init(num_cpus=args.num_cpus or None, local_mode=args.local_mode)
|
|
|
|
|
|
|
|
# Create q custom Trainer class using our custom Policy.
|
|
|
|
BareMetalPolicyTrainer = build_trainer(
|
|
|
|
name="MyPolicy", default_policy=BareMetalPolicyWithCustomViewReqs)
|
|
|
|
|
|
|
|
config = {
|
|
|
|
"env": "CartPole-v0",
|
2021-10-26 10:11:39 -07:00
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
2021-08-20 12:17:13 +02:00
|
|
|
"model": {
|
|
|
|
# Necessary to get the whole trajectory of 'state_in_0' in the
|
|
|
|
# sample batch.
|
|
|
|
"max_seq_len": 1,
|
|
|
|
},
|
|
|
|
"num_workers": 1,
|
|
|
|
# NOTE: Does this have consequences?
|
|
|
|
# I use it for not loading tensorflow/pytorch.
|
|
|
|
"framework": None,
|
|
|
|
"log_level": "DEBUG",
|
|
|
|
"create_env_on_driver": True,
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:11:39 -07:00
|
|
|
stop = {
|
|
|
|
"training_iteration": args.stop_iters,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:17:13 +02:00
|
|
|
# Train the Trainer with our policy.
|
2021-10-26 10:11:39 -07:00
|
|
|
results = tune.run(BareMetalPolicyTrainer, config=config, stop=stop)
|
2021-08-20 12:17:13 +02:00
|
|
|
print(results)
|