-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
329 lines (290 loc) · 13.1 KB
/
test.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import argparse
import datetime
import functools as ft
import os
import pathlib
import ipdb
import jax
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
import numpy as np
import yaml
import pickle
from gcbfplus.algo import GCBF, GCBFPlus, make_algo, CentralizedCBF, DecShareCBF
from gcbfplus.env import make_env
from gcbfplus.env.base import RolloutResult
from gcbfplus.env.utils import get_leader_id_dir, TrajLog
from gcbfplus.trainer.utils import get_bb_cbf
from gcbfplus.utils.graph import GraphsTuple
from gcbfplus.utils.utils import jax_jit_np, tree_index, chunk_vmap, merge01, jax_vmap
from gcbfplus.utils.typing import Array
def nominal_leader_dir_fn():
return jnp.array(0), jnp.array([1, 0])
def test(args):
print(f"> Running test.py {args}")
stamp_str = datetime.datetime.now().strftime("%m%d-%H%M")
# set up environment variables and seed
os.environ["XLA_PYTHON_CLIENT_PREALLOCATE"] = "false"
if args.cpu:
os.environ["JAX_PLATFORM_NAME"] = "cpu"
if args.debug:
jax.config.update("jax_disable_jit", True)
np.random.seed(args.seed)
# load config
if not args.u_ref and args.path is not None:
with open(os.path.join(args.path, "config.yaml"), "r") as f:
config = yaml.load(f, Loader=yaml.UnsafeLoader)
# create environments
num_agents = config.num_agents if args.num_agents is None else args.num_agents
if args.preset_scene == 'box' and args.preset_reset:
num_agents = 5
args.area_size = 4.5
if args.preset_scene == 'corners' and args.preset_reset:
num_agents = 24
args.area_size = 6
env = make_env(
env_id=config.env if args.env is None else args.env,
num_agents=num_agents,
num_obs=args.obs,
area_size=args.area_size,
max_step=args.max_step,
max_travel=args.max_travel,
use_connect=config.use_connect if not args.u_ref else False,
reconfig_connect=args.reconfig_connect if not args.u_ref else False,
use_leader=args.use_leader if not args.u_ref else False,
preset_reset=args.preset_reset,
preset_scene=args.preset_scene,
)
if not args.u_ref:
if args.path is not None:
path = args.path
model_path = os.path.join(path, "models")
if args.step is None:
models = os.listdir(model_path)
step = max([int(model) for model in models if model.isdigit()])
else:
step = args.step
print("step: ", step)
# check if config as dim_factor
if "dim_factor" not in config:
dim_factor = 2
else:
dim_factor = config.dim_factor
algo = make_algo(
algo=config.algo,
env=env,
node_dim=env.node_dim,
edge_dim=env.edge_dim,
state_dim=env.state_dim,
action_dim=env.action_dim,
n_agents=env.num_agents,
gnn_layers=config.gnn_layers,
batch_size=config.batch_size,
buffer_size=config.buffer_size,
horizon=config.horizon,
lr_actor=config.lr_actor,
lr_cbf=config.lr_cbf,
alpha=config.alpha,
eps=0.02,
inner_epoch=8,
loss_action_coef=config.loss_action_coef,
loss_unsafe_coef=config.loss_unsafe_coef,
loss_safe_coef=config.loss_safe_coef,
loss_h_dot_coef=config.loss_h_dot_coef,
max_grad_norm=2.0,
seed=config.seed,
use_connect=config.use_connect,
dim_factor=dim_factor,
)
algo.load(model_path, step)
act_fn = jax.jit(algo.act)
else:
algo = make_algo(
algo=args.algo,
env=env,
node_dim=env.node_dim,
edge_dim=env.edge_dim,
state_dim=env.state_dim,
action_dim=env.action_dim,
n_agents=env.num_agents,
alpha=args.alpha,
)
act_fn = jax.jit(algo.act)
path = os.path.join(f"./logs/{args.env}/{args.algo}")
if not os.path.exists(path):
os.makedirs(path)
step = None
else:
assert args.env is not None
path = os.path.join(f"./logs/{args.env}/nominal")
if not os.path.exists("./logs"):
os.mkdir("./logs")
if not os.path.exists(os.path.join("./logs", args.env)):
os.mkdir(os.path.join("./logs", args.env))
if not os.path.exists(path):
os.mkdir(path)
algo = None
act_fn = jax.jit(env.u_ref)
step = 0
test_key = jr.PRNGKey(args.seed)
test_keys = jr.split(test_key, 1_000)[: args.epi]
test_keys = test_keys[args.offset:]
algo_is_cbf = isinstance(algo, (CentralizedCBF, DecShareCBF))
if args.cbf is not None:
assert isinstance(algo, GCBF) or isinstance(algo, GCBFPlus) or isinstance(algo, CentralizedCBF)
get_bb_cbf_fn_ = ft.partial(get_bb_cbf, algo.get_cbf, env, agent_id=args.cbf, x_dim=0, y_dim=1)
get_bb_cbf_fn_ = jax_jit_np(get_bb_cbf_fn_)
def get_bb_cbf_fn(T_graph: GraphsTuple):
T = len(T_graph.states)
outs = [get_bb_cbf_fn_(tree_index(T_graph, kk)) for kk in range(T)]
Tb_x, Tb_y, Tbb_h = jtu.tree_map(lambda *x: jnp.stack(list(x), axis=0), *outs)
return Tb_x, Tb_y, Tbb_h
else:
get_bb_cbf_fn = None
cbf_fn = None
if args.nojit_rollout:
print("Only jit step, no jit rollout!")
rollout_fn = env.rollout_fn_jitstep(act_fn, args.max_step, noedge=True, nograph=args.no_video,
keep_mode=args.keep_mode, leader_dir_fn=nominal_leader_dir_fn if args.use_llm_leader else None)
# is_unsafe_fn = jax_jit_np(jax_vmap(env.collision_mask))
# is_finish_fn = jax_jit_np(jax_vmap(env.finish_mask))
# is_disconnect_fn = jax_jit_np(jax.vmap(env.disconnect_mask))
is_unsafe_fn = None
is_finish_fn = None
is_disconnect_fn = None
else:
print("jit rollout!")
rollout_fn = jax_jit_np(env.rollout_fn(act_fn, args.max_step, keep_mode=args.keep_mode))
is_unsafe_fn = jax_jit_np(jax_vmap(env.collision_mask))
is_finish_fn = jax_jit_np(jax_vmap(env.finish_mask))
is_disconnect_fn = jax_jit_np(jax.vmap(env.disconnect_mask))
rewards = []
costs = []
rollouts = []
is_unsafes = []
is_finishes = []
is_disconnects = []
rates = []
cbfs = []
for i_epi in range(args.epi):
key_x0, _ = jr.split(test_keys[i_epi], 2)
if args.nojit_rollout:
rollout: RolloutResult
rollout, is_unsafe, is_finish, is_disconnect = rollout_fn(key_x0)
# if not jnp.isnan(rollout.T_reward).any():
is_unsafes.append(is_unsafe)
is_finishes.append(is_finish)
is_disconnects.append(is_disconnect)
else:
rollout: RolloutResult = rollout_fn(key_x0)
# if not jnp.isnan(rollout.T_reward).any():
is_unsafes.append(is_unsafe_fn(rollout.Tp1_graph))
is_finishes.append(is_finish_fn(rollout.Tp1_graph))
is_disconnects.append(is_disconnect_fn(rollout.Tp1_graph))
epi_reward = rollout.T_reward.sum()
epi_cost = rollout.T_cost.sum()
rewards.append(epi_reward)
costs.append(epi_cost)
rollouts.append(rollout)
if args.cbf is not None:
cbfs.append(get_bb_cbf_fn(rollout.Tp1_graph))
else:
cbfs.append(None)
if len(is_unsafes) == 0:
continue
safe_rate = 1 - is_unsafes[-1].max(axis=0).mean()
finish_rate = is_finishes[-1].max(axis=0).mean()
disconnect_rate = is_disconnects[-1].max(axis=0).mean()
success_rate = ((1 - is_unsafes[-1].max(axis=0)) * is_finishes[-1].max(axis=0)).mean()
print(f"epi: {i_epi}, reward: {epi_reward:.3f}, cost: {epi_cost:.3f}, "
f"safe rate: {safe_rate * 100:.3f}%,"
f"finish rate: {finish_rate * 100:.3f}%, "
f"success rate: {success_rate * 100:.3f}%, "
f"disconnect rate: {disconnect_rate * 100:.3f}%")
rates.append(np.array([safe_rate, finish_rate, success_rate]))
is_unsafe = np.max(np.stack(is_unsafes), axis=1)
is_finish = np.max(np.stack(is_finishes), axis=1)
is_disconnects = np.max(np.stack(is_disconnects), axis=1)
disconnect_mean, disconnect_std = is_disconnects.mean(), is_disconnects.std()
safe_mean, safe_std = (1 - is_unsafe).mean(), (1 - is_unsafe).std()
finish_mean, finish_std = is_finish.mean(), is_finish.std()
success_mean, success_std = ((1 - is_unsafe) * is_finish).mean(), ((1 - is_unsafe) * is_finish).std()
print(
f"reward: {np.mean(rewards):.3f}, min/max reward: {np.min(rewards):.3f}/{np.max(rewards):.3f}, "
f"cost: {np.mean(costs):.3f}, min/max cost: {np.min(costs):.3f}/{np.max(costs):.3f}, "
f"safe_rate: {safe_mean * 100:.3f}%, "
f"finish_rate: {finish_mean * 100:.3f}%, "
f"success_rate: {success_mean * 100:.3f}%, "
f"disconnect_rate: {disconnect_mean * 100:.3f}%"
)
# save results
if args.log:
with open(os.path.join(path, "test_log.csv"), "a") as f:
f.write(f"{env.num_agents},{args.epi},{env.max_episode_steps},"
f"{env.area_size},{env.params['n_obs']},"
f"{safe_mean * 100:.3f},{safe_std * 100:.3f},"
f"{finish_mean * 100:.3f},{finish_std * 100:.3f},"
f"{disconnect_mean * 100:.3f},{disconnect_std * 100:.3f},"
f"{success_mean * 100:.3f},{success_std * 100:.3f}\n")
# save trajectories
if args.save_traj:
traj_path = os.path.join(path, "trajs")
for i in range(args.epi):
leader_id, leader_dir = jax_vmap(get_leader_id_dir)(rollouts[i].Tp1_graph)
traj_log = TrajLog(rollouts[i].Tp1_graph.to_tuple(), rollouts[i].T_action, leader_id, leader_dir)
with open(os.path.join(traj_path, f"traj_log_{i}.pkl"), "wb") as f:
pickle.dump(traj_log, f)
# make video
if args.no_video:
return
videos_dir = pathlib.Path(path) / "videos"
videos_dir.mkdir(exist_ok=True, parents=True)
for ii, (rollout, Ta_is_unsafe, cbf) in enumerate(zip(rollouts, is_unsafes, cbfs)):
if algo_is_cbf:
safe_rate, finish_rate, success_rate = rates[ii] * 100
video_name = f"n{num_agents}_epi{ii:02}_sr{safe_rate:.0f}_fr{finish_rate:.0f}_sr{success_rate:.0f}"
else:
video_name = f"n{num_agents}_step{step}_epi{ii:02}_reward{rewards[ii]:.3f}_cost{costs[ii]:.3f}"
viz_opts = {}
if args.cbf is not None:
video_name += f"_cbf{args.cbf}"
viz_opts["cbf"] = [*cbf, args.cbf]
video_path = videos_dir / f"{stamp_str}_{video_name}.mp4"
env.render_video(rollout, video_path, Ta_is_unsafe, viz_opts, dpi=args.dpi)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--num-agents", type=int, default=None)
parser.add_argument("--obs", type=int, default=0)
parser.add_argument("--area-size", type=float, required=True)
parser.add_argument("--max-step", type=int, default=None)
parser.add_argument("--path", type=str, default=None)
parser.add_argument("--n-rays", type=int, default=32)
parser.add_argument("--alpha", type=float, default=1.0)
parser.add_argument("--max-travel", type=float, default=None)
parser.add_argument("--cbf", type=int, default=None)
parser.add_argument("--seed", type=int, default=1234)
parser.add_argument("--debug", action="store_true", default=False)
parser.add_argument("--cpu", action="store_true", default=False)
parser.add_argument("--u-ref", action="store_true", default=False)
parser.add_argument("--env", type=str, default=None)
parser.add_argument("--algo", type=str, default=None)
parser.add_argument("--step", type=int, default=None)
parser.add_argument("--epi", type=int, default=5)
parser.add_argument("--offset", type=int, default=0)
parser.add_argument("--no-video", action="store_true", default=False)
parser.add_argument("--nojit-rollout", action="store_true", default=False)
parser.add_argument("--log", action="store_true", default=False)
parser.add_argument("--dpi", type=int, default=100)
parser.add_argument("--reconfig_connect", action="store_true", default=False)
parser.add_argument("--use_leader", action="store_true", default=False)
parser.add_argument("--preset_reset", action="store_true", default=False)
parser.add_argument("--preset_scene", type=str, default=None)
parser.add_argument("--save-traj", action="store_true", default=False)
parser.add_argument("--keep-mode", type=int, default=1)
parser.add_argument("--use-llm-leader", action="store_true", default=False)
args = parser.parse_args()
test(args)
if __name__ == "__main__":
with ipdb.launch_ipdb_on_exception():
main()