-
Notifications
You must be signed in to change notification settings - Fork 200
/
opencda.py
77 lines (66 loc) · 3.09 KB
/
opencda.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
# -*- coding: utf-8 -*-
"""
Script to run different scenarios.
"""
# Author: Runsheng Xu <rxx3386@ucla.edu>
# License: TDG-Attribution-NonCommercial-NoDistrib
import argparse
import importlib
import os
import sys
from omegaconf import OmegaConf
from opencda.version import __version__
def arg_parse():
# create an argument parser
parser = argparse.ArgumentParser(description="OpenCDA scenario runner.")
# add arguments to the parser
parser.add_argument('-t', "--test_scenario", required=True, type=str,
help='Define the name of the scenario you want to test. The given name must'
'match one of the testing scripts(e.g. single_2lanefree_carla) in '
'opencda/scenario_testing/ folder'
' as well as the corresponding yaml file in opencda/scenario_testing/config_yaml.')
parser.add_argument("--record", action='store_true',
help='whether to record and save the simulation process to .log file')
parser.add_argument("--apply_ml",
action='store_true',
help='whether ml/dl framework such as sklearn/pytorch is needed in the testing. '
'Set it to true only when you have installed the pytorch/sklearn package.')
parser.add_argument('-v', "--version", type=str, default='0.9.11',
help='Specify the CARLA simulator version, default'
'is 0.9.11, 0.9.12 is also supported.')
# parse the arguments and return the result
opt = parser.parse_args()
return opt
def main():
# parse the arguments
opt = arg_parse()
# print the version of OpenCDA
print("OpenCDA Version: %s" % __version__)
# set the default yaml file
default_yaml = config_yaml = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'opencda/scenario_testing/config_yaml/default.yaml')
# set the yaml file for the specific testing scenario
config_yaml = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'opencda/scenario_testing/config_yaml/%s.yaml' % opt.test_scenario)
# load the default yaml file and the scenario yaml file as dictionaries
default_dict = OmegaConf.load(default_yaml)
scene_dict = OmegaConf.load(config_yaml)
# merge the dictionaries
scene_dict = OmegaConf.merge(default_dict, scene_dict)
# import the testing script
testing_scenario = importlib.import_module(
"opencda.scenario_testing.%s" % opt.test_scenario)
# check if the yaml file for the specific testing scenario exists
if not os.path.isfile(config_yaml):
sys.exit(
"opencda/scenario_testing/config_yaml/%s.yaml not found!" % opt.test_cenario)
# get the function for running the scenario from the testing script
scenario_runner = getattr(testing_scenario, 'run_scenario')
# run the scenario testing
scenario_runner(opt, scene_dict)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print(' - Exited by user.')