-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
109 lines (93 loc) · 4.55 KB
/
utils.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
import configparser
from sumolib import checkBinary
import os
import sys
def import_train_configuration(config_file):
"""
Read the config file regarding the training and import its content
"""
content = configparser.ConfigParser()
content.read(config_file)
config = {}
config['gui'] = content['simulation'].getboolean('gui')
config['total_episodes'] = content['simulation'].getint('total_episodes')
config['max_steps'] = content['simulation'].getint('max_steps')
config['n_cars_generated'] = content['simulation'].getint('n_cars_generated')
config['green_duration'] = content['simulation'].getint('green_duration')
config['yellow_duration'] = content['simulation'].getint('yellow_duration')
config['num_layers'] = content['model'].getint('num_layers')
config['width_layers'] = content['model'].getint('width_layers')
config['batch_size'] = content['model'].getint('batch_size')
config['learning_rate'] = content['model'].getfloat('learning_rate')
config['training_epochs'] = content['model'].getint('training_epochs')
config['memory_size_min'] = content['memory'].getint('memory_size_min')
config['memory_size_max'] = content['memory'].getint('memory_size_max')
config['num_states'] = content['agent'].getint('num_states')
config['num_actions'] = content['agent'].getint('num_actions')
config['gamma'] = content['agent'].getfloat('gamma')
config['models_path_name'] = content['dir']['models_path_name']
config['sumocfg_file_name'] = content['dir']['sumocfg_file_name']
return config
def import_test_configuration(config_file):
"""
Read the config file regarding the testing and import its content
"""
content = configparser.ConfigParser()
content.read(config_file)
config = {}
config['gui'] = content['simulation'].getboolean('gui')
config['max_steps'] = content['simulation'].getint('max_steps')
config['n_cars_generated'] = content['simulation'].getint('n_cars_generated')
config['episode_seed'] = content['simulation'].getint('episode_seed')
config['green_duration'] = content['simulation'].getint('green_duration')
config['yellow_duration'] = content['simulation'].getint('yellow_duration')
config['num_states'] = content['agent'].getint('num_states')
config['num_actions'] = content['agent'].getint('num_actions')
config['sumocfg_file_name'] = content['dir']['sumocfg_file_name']
config['models_path_name'] = content['dir']['models_path_name']
config['model_to_test'] = content['dir'].getint('model_to_test')
return config
def set_sumo(gui, sumocfg_file_name, max_steps):
"""
Configure various parameters of SUMO
"""
# sumo things - we need to import python modules from the $SUMO_HOME/tools directory
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
# setting the cmd mode or the visual mode
if gui == False:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
# setting the cmd command to run sumo at simulation time
sumo_cmd = [sumoBinary, "-c", os.path.join('intersection', sumocfg_file_name), "--no-step-log", "true", "--waiting-time-memory", str(max_steps)]
return sumo_cmd
def set_train_path(models_path_name):
"""
Create a new model path with an incremental integer, also considering previously created model paths
"""
models_path = os.path.join(os.getcwd(), models_path_name, '')
os.makedirs(os.path.dirname(models_path), exist_ok=True)
dir_content = os.listdir(models_path)
if dir_content:
previous_versions = [int(name.split("_")[1]) for name in dir_content]
new_version = str(max(previous_versions) + 1)
else:
new_version = '1'
data_path = os.path.join(models_path, 'model_'+new_version, '')
os.makedirs(os.path.dirname(data_path), exist_ok=True)
return data_path
def set_test_path(models_path_name, model_n):
"""
Returns a model path that identifies the model number provided as argument and a newly created 'test' path
"""
model_folder_path = os.path.join(os.getcwd(), models_path_name, 'model_'+str(model_n), '')
if os.path.isdir(model_folder_path):
plot_path = os.path.join(model_folder_path, 'test', '')
os.makedirs(os.path.dirname(plot_path), exist_ok=True)
return model_folder_path, plot_path
else:
sys.exit('The model number specified does not exist in the models folder')