-
Notifications
You must be signed in to change notification settings - Fork 2
/
rllib.py
272 lines (247 loc) · 8.38 KB
/
rllib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import argparse
import logging
import multiprocessing
import random
from datetime import timedelta
from os import stat
from pathlib import Path
from typing import Dict
import numpy as np
from ray import tune
from ray.rllib.agents.callbacks import DefaultCallbacks
from ray.rllib.env.base_env import BaseEnv
from ray.rllib.evaluation.episode import MultiAgentEpisode
from ray.rllib.evaluation.rollout_worker import RolloutWorker
from ray.rllib.policy.policy import Policy
from ray.rllib.utils.typing import PolicyID
from ray.tune.schedulers import PopulationBasedTraining
import smarts
from examples.rllib_agent import TrainingModel, rllib_agent
from smarts.core.utils.file import copy_tree
from smarts.env.rllib_hiway_env import RLlibHiWayEnv
logging.basicConfig(level=logging.INFO)
# Add custom metrics to your tensorboard using these callbacks
# See: https://ray.readthedocs.io/en/latest/rllib-training.html#callbacks-and-custom-metrics
class Callbacks(DefaultCallbacks):
@staticmethod
def on_episode_start(
worker: RolloutWorker,
base_env: BaseEnv,
policies: Dict[PolicyID, Policy],
episode: MultiAgentEpisode,
env_index: int,
**kwargs,
):
episode.user_data["ego_speed"] = []
@staticmethod
def on_episode_step(
worker: RolloutWorker,
base_env: BaseEnv,
episode: MultiAgentEpisode,
env_index: int,
**kwargs,
):
single_agent_id = list(episode._agent_to_last_obs)[0]
obs = episode.last_raw_obs_for(single_agent_id)
episode.user_data["ego_speed"].append(obs["speed"])
@staticmethod
def on_episode_end(
worker: RolloutWorker,
base_env: BaseEnv,
policies: Dict[PolicyID, Policy],
episode: MultiAgentEpisode,
env_index: int,
**kwargs,
):
mean_ego_speed = np.mean(episode.user_data["ego_speed"])
print(
f"ep. {episode.episode_id:<12} ended;"
f" length={episode.length:<6}"
f" mean_ego_speed={mean_ego_speed:.2f}"
)
episode.custom_metrics["mean_ego_speed"] = mean_ego_speed
def explore(config):
# ensure we collect enough timesteps to do sgd
if config["train_batch_size"] < config["rollout_fragment_length"] * 2:
config["train_batch_size"] = config["rollout_fragment_length"] * 2
return config
def main(
scenario,
headless,
time_total_s,
rollout_fragment_length,
train_batch_size,
seed,
num_samples,
num_agents,
num_workers,
resume_training,
result_dir,
checkpoint_num,
save_model_path,
):
assert train_batch_size > 0, f"{train_batch_size.__name__} cannot be less than 1."
if rollout_fragment_length > train_batch_size:
rollout_fragment_length = train_batch_size
pbt = PopulationBasedTraining(
time_attr="time_total_s",
metric="episode_reward_mean",
mode="max",
perturbation_interval=300,
resample_probability=0.25,
# Specifies the mutations of these hyperparams
# See: `ray.rllib.agents.trainer.COMMON_CONFIG` for common hyperparams
hyperparam_mutations={
"lr": [1e-3, 5e-4, 1e-4, 5e-5, 1e-5],
"rollout_fragment_length": lambda: rollout_fragment_length,
"train_batch_size": lambda: train_batch_size,
},
# Specifies additional mutations after hyperparam_mutations is applied
custom_explore_fn=explore,
)
# XXX: There is a bug in Ray where we can only export a trained model if
# the policy it's attached to is named 'default_policy'.
# See: https://github.com/ray-project/ray/issues/5339
rllib_policies = {
"default_policy": (
None,
rllib_agent["observation_space"],
rllib_agent["action_space"],
{"model": {"custom_model": TrainingModel.NAME}},
)
}
smarts.core.seed(seed)
tune_config = {
"env": RLlibHiWayEnv,
"log_level": "WARN",
"num_workers": num_workers,
"env_config": {
"seed": tune.sample_from(lambda spec: random.randint(0, 300)),
"scenarios": [str(Path(scenario).expanduser().resolve().absolute())],
"headless": headless,
"agent_specs": {
f"AGENT-{i}": rllib_agent["agent_spec"] for i in range(num_agents)
},
},
"multiagent": {"policies": rllib_policies},
"callbacks": Callbacks,
}
experiment_name = "rllib_example_multi"
result_dir = Path(result_dir).expanduser().resolve().absolute()
if checkpoint_num:
checkpoint = str(
result_dir / f"checkpoint_{checkpoint_num}" / f"checkpoint-{checkpoint_num}"
)
else:
checkpoint = None
print(f"Checkpointing at {str(result_dir)}")
analysis = tune.run(
"PG",
name=experiment_name,
stop={"time_total_s": time_total_s},
checkpoint_freq=1,
checkpoint_at_end=True,
local_dir=str(result_dir),
resume=resume_training,
restore=checkpoint,
max_failures=3,
num_samples=num_samples,
export_formats=["model", "checkpoint"],
config=tune_config,
scheduler=pbt,
)
print(analysis.dataframe().head())
best_logdir = Path(analysis.get_best_logdir("episode_reward_max", mode="max"))
model_path = best_logdir / "model"
copy_tree(str(model_path), save_model_path, overwrite=True)
print(f"Wrote model to: {save_model_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser("rllib-example")
parser.add_argument(
"scenario",
help="Scenario to run (see scenarios/ for some samples you can use)",
type=str,
)
parser.add_argument(
"--headless",
action="store_true",
default=False,
help="Run simulation in headless mode",
)
parser.add_argument(
"--num_samples",
type=int,
default=1,
help="Number of times to sample from hyperparameter space",
)
parser.add_argument(
"--rollout_fragment_length",
type=int,
default=200,
help="Episodes are divided into fragments of this many steps for each rollout. In this example this will be ensured to be `1=<rollout_fragment_length<=train_batch_size`",
)
parser.add_argument(
"--train_batch_size",
type=int,
default=2000,
help="The training batch size. This value must be > 0.",
)
parser.add_argument(
"--time_total_s",
type=int,
default=1 * 60 * 60, # 1 hour
help="Total time in seconds to run the simulation for. This is a rough end time as it will be checked per training batch.",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="The base random seed to use, intended to be mixed with --num_samples",
)
parser.add_argument(
"--num_agents", type=int, default=2, help="Number of agents (one per policy)"
)
parser.add_argument(
"--num_workers",
type=int,
default=(multiprocessing.cpu_count() // 2 + 1),
help="Number of workers (defaults to use all system cores)",
)
parser.add_argument(
"--resume_training",
default=False,
action="store_true",
help="Resume the last trained example",
)
parser.add_argument(
"--result_dir",
type=str,
default="~/ray_results",
help="Directory containing results",
)
parser.add_argument(
"--checkpoint_num", type=int, default=None, help="Checkpoint number"
)
save_model_path = str(Path(__file__).expanduser().resolve().parent / "model")
parser.add_argument(
"--save_model_path",
type=str,
default=save_model_path,
help="Destination path of where to copy the model when training is over",
)
args = parser.parse_args()
main(
scenario=args.scenario,
headless=args.headless,
time_total_s=args.time_total_s,
rollout_fragment_length=args.rollout_fragment_length,
train_batch_size=args.train_batch_size,
seed=args.seed,
num_samples=args.num_samples,
num_agents=args.num_agents,
num_workers=args.num_workers,
resume_training=args.resume_training,
result_dir=args.result_dir,
checkpoint_num=args.checkpoint_num,
save_model_path=args.save_model_path,
)