-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
527 lines (504 loc) · 22.4 KB
/
main.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"""Entry point.
"""
import argparse
import sys
import os
import time
import pickle
from collections import OrderedDict, defaultdict
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import approaches
import envs
import solvers
import utils
from settings import EnvConfig as ec
from settings import SolverConfig as sc
from settings import ApproachConfig as ac
from settings import GeneralConfig as gc
from settings import print_settings
np.set_printoptions(suppress=True, linewidth=np.inf)
APPROACH_TO_NAME = {
"ModelBased": "CAMP (ours)",
"ModelBasedNoDropping": "CAMP ablation",
"FullTestPlanner": "Pure planning",
"PlanTransfer": "Plan transfer",
"PolicyTransfer": "Policy learning",
"TaskConditionedPolicyTransfer": "Task-conditioned policy learning",
"Stilman": "Stilman's (NAMO-specific)",
}
ENV_TO_NAME = {
"enemiessmall": "Domain 1 (Gridworld), BFSReplan",
"enemiesbig": "Domain 1 (Gridworld), MCTS",
"tampnamo": "Domain 3 (Robotic NAMO)",
"tampbins": "Domain 4 (Robotic Manipulation)",
"dinner": "Domain 2 (Classical)",
}
class Runner:
"""Main runner class for multiple approaches and train/test splits.
"""
def __init__(self):
gc.rand_state = np.random.RandomState(seed=gc.start_seed)
if not os.path.exists("plots/"):
os.mkdir("plots/")
if not os.path.exists("results/"):
os.mkdir("results/")
if not os.path.exists("trained_models/"):
os.mkdir("trained_models/")
self._single_runners = OrderedDict()
train_env_names = ec.train_tests[ec.family_to_run]["train"]
test_env_names = ec.train_tests[ec.family_to_run]["test"]
solver = getattr(solvers, sc.solver_name)()
for approach_name in ac.approach_names:
runner_id = self._create_runner_id(approach_name)
self._single_runners[runner_id] = SingleRunner(
approach_name, solver, train_env_names, test_env_names)
# runner_id = "tampnamo_TAMPSolverStilman_FullTestPlanner_10"
# self._single_runners[runner_id] = SingleRunner(
# "FullTestPlanner", "TAMPSolver", train_env_names, test_env_names)
@staticmethod
def plot():
"""Only do plotting.
"""
all_results = defaultdict(lambda: defaultdict(list))
for i in range(gc.total_num_runs):
for approach_name in ac.approach_names:
runner_id = Runner._create_runner_id(approach_name)
filename = "results/{}_{}.pkl".format(runner_id, i+1)
with open(filename, "rb") as f:
result = pickle.load(f)
for env_name, res in result.items():
all_results[runner_id][env_name].append(res)
if ec.family_to_run == "tampnamo":
runner_id = "tampnamo_TAMPSolverStilman_FullTestPlanner_10"
filename = "results/{}_{}.pkl".format(runner_id, i+1)
with open(filename, "rb") as f:
result = pickle.load(f)
runner_id = "tampnamo_TAMPSolver_Stilman_10"
for env_name, res in result.items():
all_results[runner_id][env_name].append(res)
return_data = defaultdict(dict)
plancost_data = defaultdict(dict)
obj_value_data = defaultdict(dict)
for runner_id, env_name_to_res in all_results.items():
for env_name, res in env_name_to_res.items():
mean_res = np.mean(res, axis=0)
std_res = np.std(res, axis=0)
return_data[env_name][runner_id.split("_")[2]] = (
mean_res[0], std_res[0])
plancost_data[env_name][runner_id.split("_")[2]] = (
mean_res[1], std_res[1])
obj_value_data[env_name][runner_id.split("_")[2]] = (
mean_res[2], std_res[2])
# Runner._plot(return_data, "Returns", "returns")
# Runner._plot(plancost_data, "Solve Cost", "solvecosts")
# Runner._plot(obj_value_data, "Objective Value", "objvalues")
Runner._plot_2d(return_data, plancost_data)
def run(self):
"""Main method; train and test each approach.
"""
all_results = defaultdict(lambda: defaultdict(list))
parser = argparse.ArgumentParser()
parser.add_argument("--runindex", type=int, default=None)
args = parser.parse_args(sys.argv[1:])
for i in range(gc.total_num_runs):
print("RUN {} of {}".format(i+1, gc.total_num_runs), flush=True)
if args.runindex is not None and args.runindex != i:
print("Skipping due to --runindex\n")
continue
gc.seed = gc.start_seed+i
gc.rand_state = np.random.RandomState(seed=gc.seed)
print_settings()
for runner_id, runner in self._single_runners.items():
if gc.mode != "test":
runner.train()
if gc.mode != "train":
result = runner.test()
filename = "results/{}_{}.pkl".format(runner_id, i+1)
with open(filename, "wb") as f:
pickle.dump(result, f)
print("Dumped result to {}.".format(filename), flush=True)
for env_name, res in result.items():
all_results[runner_id][env_name].append(res)
# Comment out above, uncomment below to load results from cache.
# filename = "saved_results/{}_{}.pkl".format(runner_id, i+1)
# with open(filename, "rb") as f:
# result = pickle.load(f)
# # if "Stilman" in runner_id:
# # runner_id = "tampnamo_TAMPSolver_Stilman_10"
# for env_name, res in result.items():
# all_results[runner_id][env_name].append(res)
return_data = defaultdict(dict)
plancost_data = defaultdict(dict)
obj_value_data = defaultdict(dict)
for runner_id, env_name_to_res in all_results.items():
print("Runner ID: {}".format(runner_id))
for env_name, res in env_name_to_res.items():
print("\tTest environment: {}".format(env_name))
print("\t\tNum runs done: {}".format(len(res)))
mean_res = np.mean(res, axis=0)
std_res = np.std(res, axis=0)
print("\t\tAverage results: {}".format(mean_res))
print("\t\tSD results: {}".format(std_res))
return_data[env_name][runner_id.split("_")[2]] = (
mean_res[0], std_res[0])
plancost_data[env_name][runner_id.split("_")[2]] = (
mean_res[1], std_res[1])
obj_value_data[env_name][runner_id.split("_")[2]] = (
mean_res[2], std_res[2])
if args.runindex is None:
self._plot(return_data,
"Avg Returns ({} runs)".format(i+1),
"returns")
self._plot(plancost_data,
"Avg Plan Cost ({} runs)".format(i+1),
"solvecosts")
self._plot(obj_value_data,
"Avg Objective ({} runs)".format(i+1),
"objvalues")
@staticmethod
def _plot(data, title, namestr):
if "enemies" in ec.family_to_run:
# Group plots by easy/medium/hard.
Runner._plot_grouped_enemies(data, title, namestr)
return
if ec.family_to_run == "tamppickplace":
# Put all plots together.
Runner._plot_grouped_tamppickplace(data, title, namestr)
return
if ec.family_to_run == "tampnamo":
# Put all plots together.
Runner._plot_grouped_tampnamo(data, title, namestr)
return
if ec.family_to_run == "tampbins":
# Put all plots together.
Runner._plot_grouped_tampbins(data, title, namestr)
return
if ec.family_to_run == "dinner":
# Put all plots together.
Runner._plot_grouped_dinner(data, title, namestr)
return
for env_name, approach_name_to_res in data.items():
names = []
vals = []
errors = []
for approach_name, (mean, std) in approach_name_to_res.items():
names.append(APPROACH_TO_NAME[approach_name])
vals.append(mean)
errors.append(std)
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {} {}".format(title, env_name, sc.solver_name))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/{}_{}_{}.png".format(
env_name, sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _plot_2d(return_data, plancost_data):
ret_means = defaultdict(list)
ret_stds = defaultdict(list)
for env_name, approach_name_to_res in return_data.items():
if ec.family_to_run.startswith("enemies") and "Medium" not in env_name:
continue
for approach_name, (mean, std) in approach_name_to_res.items():
ret_means[approach_name].append(mean)
ret_stds[approach_name].append(std)
cost_means = defaultdict(list)
cost_stds = defaultdict(list)
for env_name, approach_name_to_res in plancost_data.items():
if ec.family_to_run.startswith("enemies") and "Medium" not in env_name:
continue
for approach_name, (mean, std) in approach_name_to_res.items():
if ec.family_to_run == "enemiesbig":
mean = 0.25 # timeout
std = 0
cost_means[approach_name].append(mean)
cost_stds[approach_name].append(std)
if ec.family_to_run == "enemiesbig":
plt.figure(figsize=[2.9, 4.8])
else:
plt.figure(figsize=[6.4, 4.8])
for approach_name in ret_means:
name = APPROACH_TO_NAME[approach_name]
ret_val = np.mean(ret_means[approach_name])
ret_error = np.mean(ret_stds[approach_name])
cost_val = np.mean(cost_means[approach_name])
cost_error = np.mean(cost_stds[approach_name])
plt.errorbar(cost_val, ret_val, xerr=cost_error, yerr=ret_error,
fmt="o", label=name, markersize=20)
# plt.errorbar(0,0,xerr=0,yerr=0,fmt="o",label="Stilman (NAMO only)")
plt.xlabel("Computational Cost (seconds)")
plt.ylabel("Returns")
plt.title(ENV_TO_NAME[ec.family_to_run])
fname = "plots/2d_{}_{}.png".format(ec.family_to_run, sc.solver_name)
if ec.family_to_run != "enemiesbig":
plt.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15),
ncol=2, fancybox=True, shadow=True)
# plt.legend(ncol=7)
plt.tight_layout()
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _plot_grouped_enemies(data, title, namestr):
easy_means = defaultdict(list)
medium_means = defaultdict(list)
hard_means = defaultdict(list)
easy_stds = defaultdict(list)
medium_stds = defaultdict(list)
hard_stds = defaultdict(list)
for env_name, approach_name_to_res in data.items():
for approach_name, (mean, std) in approach_name_to_res.items():
if "Easy" in env_name:
easy_means[approach_name].append(mean)
easy_stds[approach_name].append(std)
elif "Medium" in env_name:
medium_means[approach_name].append(mean)
medium_stds[approach_name].append(std)
elif "Hard" in env_name:
hard_means[approach_name].append(mean)
hard_stds[approach_name].append(std)
names = []
vals = []
errors = []
for approach_name in easy_means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(easy_means[approach_name]))
errors.append(np.mean(easy_stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {} {}, {}".format(title, ENV_TO_NAME[ec.family_to_run], "Easy", sc.solver_name))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/{}_{}_{}.png".format(
"easy", sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
names = []
vals = []
errors = []
for approach_name in medium_means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(medium_means[approach_name]))
errors.append(np.mean(medium_stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {} {}, {}".format(title, ENV_TO_NAME[ec.family_to_run], "Medium", sc.solver_name))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/{}_{}_{}.png".format(
"medium", sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
names = []
vals = []
errors = []
for approach_name in hard_means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(hard_means[approach_name]))
errors.append(np.mean(hard_stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {} {}, {}".format(title, ENV_TO_NAME[ec.family_to_run], "Hard", sc.solver_name))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/{}_{}_{}.png".format(
"hard", sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _plot_grouped_tamppickplace(data, title, namestr):
mode = "together"
# mode = "grouped"
if mode == "together":
means = defaultdict(list)
stds = defaultdict(list)
for env_name, approach_name_to_res in data.items():
for approach_name, (mean, std) in approach_name_to_res.items():
means[approach_name].append(mean)
stds[approach_name].append(std)
names = []
vals = []
errors = []
for approach_name in means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(means[approach_name]))
errors.append(np.mean(stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {}".format(title, ENV_TO_NAME[ec.family_to_run]))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/TAMPPickPlace_{}_{}.png".format(sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
elif mode == "grouped":
skinny_means = defaultdict(list)
skinny_stds = defaultdict(list)
fat_means = defaultdict(list)
fat_stds = defaultdict(list)
for env_name, approach_name_to_res in data.items():
for approach_name, (mean, std) in approach_name_to_res.items():
if "Fat" in env_name:
fat_means[approach_name].append(mean)
fat_stds[approach_name].append(std)
else:
skinny_means[approach_name].append(mean)
skinny_stds[approach_name].append(std)
names = []
vals = []
errors = []
for approach_name in fat_means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(fat_means[approach_name]))
errors.append(np.mean(fat_stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {} {}".format(title, "Fat", sc.solver_name))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/TAMPPickPlace_{}_{}_{}.png".format("fat", sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
names = []
vals = []
errors = []
for approach_name in skinny_means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(skinny_means[approach_name]))
errors.append(np.mean(skinny_stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {} {}".format(title, "Skinny", sc.solver_name))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/TAMPPickPlace_{}_{}_{}.png".format("skinny", sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _plot_grouped_tampnamo(data, title, namestr):
means = defaultdict(list)
stds = defaultdict(list)
for env_name, approach_name_to_res in data.items():
for approach_name, (mean, std) in approach_name_to_res.items():
means[approach_name].append(mean)
stds[approach_name].append(std)
names = []
vals = []
errors = []
for approach_name in means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(means[approach_name]))
errors.append(np.mean(stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {}".format(title, ENV_TO_NAME[ec.family_to_run]))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/TAMPNAMO_{}_{}.png".format(sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _plot_grouped_tampbins(data, title, namestr):
means = defaultdict(list)
stds = defaultdict(list)
for env_name, approach_name_to_res in data.items():
for approach_name, (mean, std) in approach_name_to_res.items():
means[approach_name].append(mean)
stds[approach_name].append(std)
names = []
vals = []
errors = []
for approach_name in means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(means[approach_name]))
errors.append(np.mean(stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {}".format(title, ENV_TO_NAME[ec.family_to_run]))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/TAMPBins_{}_{}.png".format(sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _plot_grouped_dinner(data, title, namestr):
means = defaultdict(list)
stds = defaultdict(list)
for env_name, approach_name_to_res in data.items():
for approach_name, (mean, std) in approach_name_to_res.items():
means[approach_name].append(mean)
stds[approach_name].append(std)
names = []
vals = []
errors = []
for approach_name in means:
names.append(APPROACH_TO_NAME[approach_name])
vals.append(np.mean(means[approach_name]))
errors.append(np.mean(stds[approach_name]))
plt.figure()
plt.errorbar(names, vals, errors, fmt="o")
plt.title("{}: {}".format(title, ENV_TO_NAME[ec.family_to_run]))
plt.xticks(rotation=45)
plt.tight_layout()
fname = "plots/Dinner_{}_{}.png".format(sc.solver_name, namestr)
plt.savefig(fname, dpi=300)
plt.close()
print("Plot written out to {}".format(fname))
@staticmethod
def _create_runner_id(approach_name):
return "{}_{}_{}_{}".format(ec.family_to_run, sc.solver_name,
approach_name, ac.lam)
class SingleRunner:
"""Main runner class for a single approach and train/test split.
"""
def __init__(self, approach_name, solver, train_env_names, test_env_names):
self._approach_name = approach_name
self._approach = getattr(approaches, approach_name)(solver)
if gc.mode != "test":
self._train_envs = [getattr(envs, env_name)()
for env_name in train_env_names]
if gc.mode != "train":
self._test_envs = [getattr(envs, env_name)()
for env_name in test_env_names]
def train(self):
"""Main method for training a single approach.
"""
print("\nTRAINING APPROACH {}...\n".format(self._approach_name))
start = time.time()
self._approach.train(self._train_envs)
print("Training approach {} took {:.3f} sec\n\n".format(
self._approach_name, time.time()-start))
def test(self):
"""Main method for testing a single approach.
"""
print("\nTESTING APPROACH {}...\n".format(self._approach_name))
all_results = {}
start = time.time()
for env in self._test_envs:
env_name = env.__class__.__name__
print("Testing env: {}".format(env_name))
results = utils.test_approach(
env, self._approach, render=gc.do_render, verbose=True)
all_results[env_name] = results
print("Testing approach {} took {:.3f} sec\n\n".format(
self._approach_name, time.time()-start))
return all_results
def _main():
Runner().run()
# Runner.plot()
if __name__ == "__main__":
_main()