-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
161 lines (144 loc) · 4.96 KB
/
environment.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
from pathlib import Path
from pandas import DataFrame
from sumo_rl import SumoEnvironment
class SumoEnvironmentWrapper(SumoEnvironment):
""" Wrapper for a SumoEnvironment, overrides save_csv to change the filename standard. """
def __init__(
self,
net_file: str,
route_file: str,
out_csv_name: str,
use_gui: bool,
num_seconds: int,
delta_time: int,
yellow_time: int,
min_green: int,
max_green: int,
fixed_ts: bool
) -> None:
"""
Wrapper for a SumoEnvironment, overrides save_csv to change the filename standard.
:param net_file: Path to the net_file.
:type net_file: str
:param route_file: Path to the route_file.
:type route_file: str
:param out_csv_name: Path for the csv file to save.
:type out_csv_name: str
:param use_gui: Whether to use SUMO GUI when running.
:type use_gui: bool
:param num_seconds: Number of simulation seconds.
:type num_seconds: int
:param delta_time: Simulation seconds between actions. Must be at least greater than yellow_time.
:type delta_time: int
:param yellow_time: Fixed yellow time between actions.
:type yellow_time: int
:param min_green: Minimum green time in a phase.
:type min_green: int
:param max_green: Max green time in a phase.
:type max_green: int
:param fixed_ts: If true, it will follow the phase configuration in the route_file and ignore the actions.
:type fixed_ts: bool
"""
super().__init__(
net_file = net_file,
route_file = route_file,
out_csv_name = out_csv_name,
use_gui = use_gui,
num_seconds = num_seconds,
max_depart_delay = 10000,
delta_time = delta_time,
yellow_time = yellow_time,
min_green = min_green,
max_green = max_green,
fixed_ts = fixed_ts,
single_agent = True,
add_per_agent_info = False
)
def save_csv(self, out_csv_name: str, run: int) -> None:
"""
Saves the csv data to the given filepath.
:param out_csv_name: Filepath to save to.
:type out_csv_name: str
:param run: Number of the run.
:type run: int
"""
Path(Path(out_csv_name).parent).mkdir(parents = True, exist_ok = True)
DataFrame(self.metrics).to_csv(out_csv_name + '.csv', index = False)
class TrafficEnvironment():
""" Wrapper to get new SumoEnvironments. """
def __init__(self, net: str, rou: str, seconds: int, delta_time: int, yellow_time: int, min_green: int, max_green: int) -> None:
"""
Wrapper to get new SumoEnvironments.
:param net: Path to the .net.xml file.
:type net: str
:param rou: Path to the .rou.xml file.
:type rou: str
:param seconds: Number of simulation seconds.
:type seconds: int
:param delta_time: Simulation seconds between actions. Must be at least greater than yellow_time.
:type delta_time: int
:param yellow_time: Fixed yellow time between actions.
:type yellow_time: int
:param min_green: Minimum green time in a phase.
:type min_green: int
:param max_green: Max green time in a phase.
:type max_green: int
"""
self._net: str = f'{net}.net.xml'
self._rou: str = f'{rou}.rou.xml'
self._seconds: int = seconds + (delta_time - seconds % delta_time) % delta_time
self._delta_time: int = delta_time
self._yellow_time: int = yellow_time
self._min_green: int = min_green
self._max_green: int = max_green
@property
def seconds(self) -> int:
""" Number of simulation seconds. """
return self._seconds
@property
def delta_time(self) -> int:
""" Simulation seconds between actions. """
return self._delta_time
@property
def yellow_time(self) -> int:
""" Fixed yellow time between actions. """
return self._yellow_time
@property
def min_green(self) -> int:
""" Minimum green time in a phase. """
return self._min_green
@property
def max_green(self) -> int:
""" Max green time in a phase. """
return self._max_green
def set_seconds(self, seconds: int) -> None:
"""
Sets the simulation seconds.
:param seconds: New amount of simulation seconds.
:type seconds: int
"""
self._seconds = seconds
def get_sumo_env(self, fixed: bool, out_csv_name: str, use_gui: bool) -> SumoEnvironment:
"""
Returns a new SumoEnvironment.
:param fixed: Whether a fixed cycle or a reinforcement learning schema is used.
:type fixed: bool
:param out_csv_name: Filepath to save csv data.
:type out_csv_name: str
:param use_gui: Whether to show SUMO GUI while running (if True, will slow down the run).
:type use_gui: bool
:return: A new SumoEnvironment.
:rtype: SumoEnvironment.
"""
return SumoEnvironmentWrapper(
net_file = self._net,
route_file = self._rou,
out_csv_name = out_csv_name,
use_gui = use_gui,
num_seconds = self.seconds,
delta_time = self.delta_time,
yellow_time = self.yellow_time,
min_green = self.min_green,
max_green = self.max_green,
fixed_ts = fixed
)