-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsim_exp.py
More file actions
115 lines (93 loc) · 4.59 KB
/
sim_exp.py
File metadata and controls
115 lines (93 loc) · 4.59 KB
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
"""Runs consecutive experiments in simulation"""
import argparse
import os
import pickle
from copy import deepcopy
from datetime import datetime
from typing import List
import matplotlib
import matplotlib.pyplot as plt
from tqdm import tqdm
from ship_ice_planner import EXPERIMENT_ROOT_DIR, FULL_SCALE_SIM_EXP_CONFIG, FULL_SCALE_SIM_PARAM_CONFIG
from ship_ice_planner.experiments.generate_rand_exp import ice_field_plot
from ship_ice_planner.evaluation.evaluate_run_sim import process_experiment
from ship_ice_planner.utils.utils import DotDict
from ship_ice_planner.sim2d import sim
matplotlib.use('Agg') # non-interactive backend
# for every trial, save a plot of the initial ice field
ICE_FIELD_PLOT_FILE_NAME = 'ice_field.pdf'
def main(cfg_file: str,
run_name: str,
planner_names: List,
method_names: List,
exp_dict: dict,
anim_save=True):
base_cfg = DotDict.load_from_file(cfg_file)
base_cfg.output_dir = os.path.join(EXPERIMENT_ROOT_DIR, run_name)
base_cfg.anim.show = False
base_cfg.anim.save = anim_save
base_cfg.plot.show = False
base_cfg.save_paths = False
if base_cfg.output_dir:
# if dir already exists, make a new one with same name but with added date prefix
if os.path.isdir(base_cfg.output_dir):
base_cfg.output_dir += '-' + str(datetime.now().strftime('%m-%d_%H:%M:%S'))
# generate a separate config for each planner based on command line arguments
methods = []
for planner, method in zip(planner_names, method_names):
cfg = deepcopy(base_cfg)
cfg.planner = planner
methods.append((method, cfg))
num_trials = 0
for conc in tqdm(exp_dict['exp']):
print('\nConcentration:', conc)
for ice_field_idx in tqdm(exp_dict['exp'][conc]):
print('\nIce field index:', ice_field_idx)
exp = exp_dict['exp'][conc][ice_field_idx]
print('ship start', exp['ship_state'])
for (method, cfg) in methods:
num_trials += 1
print('\n\tMethod', method, '\n\tTrial', num_trials)
cfg.output_dir = os.path.join(base_cfg.output_dir, str(conc), str(ice_field_idx), method)
cfg.ship.start_pos = exp['ship_state']
cfg.ship.goal_pos = exp['goal']
sim(cfg=cfg,
debug=False,
logging=False,
log_level=10,
init_queue=deepcopy(exp))
# save a plot of the experiment configuration
ice_field_plot(conc, ice_field_idx,
exp['ship_state'],
exp['obstacles'],
exp_dict['meta_data']['map_shape'],
base_cfg.ship.vertices)
plt.gcf().savefig(
os.path.join(base_cfg.output_dir, str(conc), str(ice_field_idx), ICE_FIELD_PLOT_FILE_NAME), dpi=200
)
plt.close(plt.gcf())
print('All experiments completed!')
process_experiment(root_dir=base_cfg.output_dir,
exp_dict=exp_dict)
print('Done')
if __name__ == '__main__':
# setup arg parser
parser = argparse.ArgumentParser(description='Run consecutive simulation experiments')
parser.add_argument('exp_config_file', nargs='?', type=str, help='File path to experiment config pickle file '
'generated by generate_rand_exp.py',
default=FULL_SCALE_SIM_EXP_CONFIG)
parser.add_argument('planner_config_file', nargs='?', type=str, help='File path to planner and simulation '
'parameter config yaml file (see configs/)',
default=FULL_SCALE_SIM_PARAM_CONFIG)
parser.add_argument('--run_name', type=str, help='Name for the base directory to store all output files')
parser.add_argument('--planners', type=str, nargs='+', help='List of planners to run')
parser.add_argument('--method_names', type=str, nargs='+', help='List of method names')
parser.add_argument('--no_anim', action='store_true', help='Disable rendering (significantly speeds up sim!)')
args = parser.parse_args()
assert len(args.planners) == len(args.method_names), 'Number of planners and method names must be the same!'
main(cfg_file=args.planner_config_file,
run_name=args.run_name,
planner_names=args.planners,
method_names=args.method_names,
exp_dict=pickle.load(open(args.exp_config_file, 'rb')),
anim_save=not args.no_anim)