-
Notifications
You must be signed in to change notification settings - Fork 4
/
training_env_factory.py
303 lines (257 loc) · 9.33 KB
/
training_env_factory.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import pathlib
from typing import Callable
import gymnasium as gym
import numpy as np
from gymnasium.wrappers import FlattenObservation, FrameStack
from gym_envs import cbf_factory
from gym_envs.multi_agent_env.common.track import Track
from gym_envs.multi_agent_env.planners.planner_factory import planner_factory
from gym_envs.wrappers import FrameSkip, FlattenAction, CBFSafetyLayer
from gym_envs.wrappers.action_wrappers import (
LocalPathActionWrapper,
WaypointActionWrapper,
)
from gym_envs.wrappers.observation_wrappers import VehicleTrackObservationWrapper
def make_f110_base_env(
env_id,
env_params,
cbf_params,
idx,
capture_video,
default_render_mode=None,
):
track_name = env_params["track_name"]
opp_planner = env_params["opp_planner"]
opp_params = env_params["opp_params"]
termination_types = env_params["termination_types"]
timeout = env_params["timeout"]
reward = env_params["reward"]
reset_mode = env_params["reset_mode"]
control_freq = env_params["control_freq"]
planning_freq = env_params["planning_freq"]
assert planning_freq % control_freq == 0, (
"planning_freq must be divisible by control_freq,"
f"got planning_freq={planning_freq} and control_freq={control_freq}"
)
if opp_planner is not None:
track = Track.from_track_name(track_name)
npc_planners = []
for i, (opp_plan, opp_par) in enumerate(zip(opp_planner, opp_params)):
opp = planner_factory(
planner=opp_plan, track=track, params=opp_par, agent_id=f"npc{i}"
)
npc_planners.append(opp)
else:
npc_planners = []
gym_env_params = {
"track_name": track_name,
"npc_planners": npc_planners,
"params": {
"name": idx,
"simulation": {
"control_frequency": control_freq,
},
"termination": {
"types": termination_types,
"timeout": timeout,
},
"reward": {
"reward_type": reward,
},
"reset": {
"types": [reset_mode],
"default_reset_mode": reset_mode,
},
},
}
if capture_video:
env = gym.make(env_id, **gym_env_params, render_mode="rgb_array")
else:
env = gym.make(env_id, **gym_env_params, render_mode=default_render_mode)
env = FlattenAction(env=env)
# cbf wrapper
if cbf_params["use_cbf"]:
make_cbf = cbf_factory(env_id=env_id, cbf_type=cbf_params["cbf_type"])
env = CBFSafetyLayer(
env,
safety_dim=cbf_params["safety_dim"],
gamma_range=cbf_params["cbf_gamma_range"],
make_cbf=make_cbf,
)
return env
def make_f110_env(
env_id: str,
env_params: dict,
cbf_params: dict,
idx: int,
evaluation: bool,
capture_video: bool,
log_dir: pathlib.Path,
default_render_mode: str = None,
):
def thunk():
env = make_f110_base_env(
env_id=env_id,
idx=idx,
env_params=env_params,
cbf_params=cbf_params,
capture_video=capture_video,
default_render_mode=default_render_mode,
)
# planning-action wrapper
control_freq = env_params["control_freq"]
planning_freq = env_params["planning_freq"]
frame_skip = planning_freq // control_freq
if (
env_params["local_path_generation"]
in LocalPathActionWrapper.planner_fns.keys()
):
env = LocalPathActionWrapper(
env=env, planner_type=env_params["local_path_generation"]
)
else:
env = WaypointActionWrapper(env=env)
env = FrameSkip(env, skip=frame_skip)
# observations
vehicle_features = env_params["vehicle_features"]
track_features = env_params["track_features"]
forward_curv_lookahead = env_params["forward_curv_lookahead"]
n_curvature_points = env_params["n_curvature_points"]
env = VehicleTrackObservationWrapper(
env,
vehicle_features=vehicle_features,
track_features=track_features,
forward_curv_lookahead=forward_curv_lookahead,
n_curvature_points=n_curvature_points,
)
env = FlattenObservation(env)
env = FrameStack(env, cbf_params["frame_stack"])
env = FlattenObservation(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
if evaluation and capture_video:
if idx == 0:
env = gym.wrappers.RecordVideo(
env, f"{log_dir}/videos", episode_trigger=lambda x: True
)
env = gym.wrappers.ClipAction(env)
if not evaluation:
env = gym.wrappers.NormalizeReward(env, gamma=0.99)
env = gym.wrappers.TransformReward(
env, lambda reward: np.clip(reward, -10, 10)
)
return env
return thunk
def load_f110_env_params(parser_args) -> dict:
params = {
"track_name": parser_args.track_name,
"opp_planner": parser_args.opp_planner,
"opp_params": [
{"vgain": mu, "vgain_std": std}
for mu, std in zip(parser_args.opp_vgain, parser_args.opp_vgain_std)
],
"termination_types": parser_args.termination_types,
"timeout": parser_args.time_limit,
"reset_mode": parser_args.reset_mode,
"reward": parser_args.reward,
"control_freq": parser_args.control_freq,
"planning_freq": parser_args.planning_freq,
"local_path_generation": parser_args.local_path_generation,
"vehicle_features": parser_args.vehicle_features,
"track_features": parser_args.track_features,
"forward_curv_lookahead": parser_args.forward_curv_lookahead,
"n_curvature_points": parser_args.n_curvature_points,
}
return params
def load_f110_cbf_params(parser_args):
cbf_type = parser_args.cbf_type
cbf_type = f"{cbf_type}decay" if parser_args.use_decay else cbf_type
params = {
"use_cbf": parser_args.use_cbf,
"cbf_type": cbf_type,
"safety_dim": parser_args.safety_dim,
"cbf_gamma_range": parser_args.cbf_gamma_range,
"frame_stack": parser_args.frame_stack,
}
return params
def make_particle_env(
env_id,
env_params,
cbf_params,
idx,
evaluation,
capture_video,
log_dir,
default_render_mode=None,
):
def thunk():
if capture_video:
env = gym.make(env_id, params=env_params, render_mode="rgb_array")
else:
env = gym.make(env_id, params=env_params, render_mode=default_render_mode)
env = FlattenObservation(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
env = FrameSkip(env, cbf_params["planning_freq"])
env = FrameStack(env, cbf_params["frame_stack"])
env = FlattenObservation(env)
env = gym.wrappers.ClipAction(env)
return env
return thunk
def load_particle_env_params(parser_args) -> dict:
params = {
"world_size": parser_args.world_size,
"time_limit": parser_args.time_limit,
"min_dist_goal": parser_args.min_dist_goal,
"min_agents": parser_args.min_agents,
"max_agents": parser_args.max_agents,
"obs_type": {"type": parser_args.obs_type},
"cbf_gamma_range": parser_args.cbf_gamma_range,
"ctrl_params": {
"use_clf": parser_args.use_clf,
"use_cbf": parser_args.use_cbf,
"robust": bool(parser_args.cbf_type == "robust"),
"safety_dim": parser_args.safety_dim,
},
}
return params
def load_particle_cbf_params(parser_args) -> dict:
return {
"planning_freq": parser_args.planning_freq,
"frame_stack": parser_args.frame_stack,
}
def get_env_id(env_id: str, use_cbf: bool, use_ctrl: bool, use_decay: bool = False):
if env_id == "particle-env-v0" or env_id == "particle-env-v1":
env_v = env_id.split("-")[-1]
ctrl = "auto" if use_ctrl else "rl"
cbf = "simple" if use_cbf else "none"
cbf = f"{cbf}decay" if use_cbf and use_decay else cbf
env_id = f"particle-env-{ctrl}-{cbf}-{env_v}"
elif env_id == "f110-multi-agent-v0" or env_id == "f110-multi-agent-v1":
env_v = env_id.split("-")[-1]
ctrl = "auto" if use_ctrl else "rl"
cbf = "cbf" if use_cbf else "none"
cbf = f"{cbf}decay" if use_cbf and use_decay else cbf
env_id = f"f110-multi-agent-{ctrl}-{cbf}-{env_v}"
else:
raise ValueError(f"env_id {env_id} is not supported")
return env_id
def load_env_param_factory(env_id: str) -> Callable:
if env_id == "particle-env-v0":
return load_particle_env_params
elif env_id == "f110-multi-agent-v0":
return load_f110_env_params
else:
raise NotImplementedError
def load_cbf_param_factory(env_id: str) -> Callable:
if env_id == "particle-env-v0":
return load_particle_cbf_params
elif env_id == "f110-multi-agent-v0":
return load_f110_cbf_params
else:
raise NotImplementedError
def make_env_factory(env_id: str) -> Callable:
if env_id == "particle-env-v0":
return make_particle_env
elif env_id == "f110-multi-agent-v0":
return make_f110_env
else:
raise NotImplementedError