-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
260 lines (221 loc) · 8.92 KB
/
simulation.py
File metadata and controls
260 lines (221 loc) · 8.92 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
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
import os
import sys
import timeit
import time
# import gc
# import objgraph
# import guppy
# from pympler import tracker, muppy, summary
from memory_profiler import profile
import random
import torch
import numpy as np
import traci
import traci.constants as tc
from sumolib import checkBinary
# tr = tracker.SummaryTracker()
# hp = guppy.hpy()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cell_length = 7
detection_length = 350
lane_num = 16
grid_size = int(detection_length / cell_length)
training_epochs = 200
edges = {
'east_edge': (0, '-E2'),
'south_edge': (4, '-E3'),
'west_edge': (8, 'E0'),
'north_edge': (12, 'E1')
}
incoming_edges = ['-E2', '-E3', 'E0', 'E1']
action_state_map = {
0: 'grrrgrrGGgrrrgrrGG',
1: 'grrrgrrrrgrrrgGGGG',
2: 'grrrgGGrrgrrrgGGrr',
3: 'grrrgGGGGgrrrgrrrr',
4: 'grrGgrrrrgrrGgrrrr',
5: 'grrrgrrrrgGGGgrrrr',
6: 'gGGrgrrrrgGGrgrrrr',
7: 'gGGGgrrrrgrrrgrrrr'
}
def set_sumo(gui=False, sumocfg_path='data/Eastway-Central.sumocfg', random=True, log_path=None):
# 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'")
# cmd mode or visual mode
if gui:
sumoBinary = checkBinary('sumo-gui')
else:
sumoBinary = checkBinary('sumo')
# setting the cmd to run sumo
if random and not log_path:
sumo_cmd = [sumoBinary, '-c', sumocfg_path, '--random', '--no-warnings', '--no-step-log']
elif random and log_path:
sumo_cmd = [sumoBinary, '-c', sumocfg_path, '--random', '--no-warnings', '--no-step-log',
'--tripinfo-output', log_path + '_tripinfo.xml']
elif not random and log_path:
sumo_cmd = [sumoBinary, '-c', sumocfg_path, '--no-warnings', '--no-step-log',
'--tripinfo-output', log_path + '_tripinfo.xml']
else:
sumo_cmd = [sumoBinary, '-c', sumocfg_path, '--no-warnings', '--no-step-log']
return sumo_cmd
class Simulation:
def __init__(self, agent, sumo_cmd, is_training=True):
self.agent = agent
self.sumo_cmd = sumo_cmd
self.is_training = is_training
self.step = 0
# self.max_step = 4000
self.yellow_time = 3
self.red_time = 2
self.min_green_time = 10
self.training_epochs = training_epochs
self.channels = 2
self.height = grid_size
self.width = lane_num
# store data from every epoch
self.total_waiting_time = 0
self.total_waiting_times = []
self.total_neg_reward = 0
self.total_rewards = []
# Run an episode of simulation, then start a training session
# @profile
def run(self, epsilon):
start_time = timeit.default_timer()
traci.start(self.sumo_cmd)
print('Simulating...')
self.step = 0
self.total_neg_reward = 0
self.total_waiting_time = 0
# Warm up 10 minutes
while self.step < 600:
traci.simulationStep()
self.step += 1
while self.step == 600:
traci.simulationStep()
self.step += 1
last_state = self.get_state()
last_waiting_time = self.get_waiting_times()
last_action = torch.tensor([[random.randint(0, 7)]], dtype=torch.long, device=device)
# Start to call agent
while 600 < self.step <= 4400:
# Get the current state
current_state = self.get_state()
# Calculate the total waiting time of current state
current_waiting_time = self.get_waiting_times()
reward = last_waiting_time - current_waiting_time
reward_tensor = torch.tensor([reward], device=device)
# self.total_waiting_time += current_waiting_time
# print('-----current reward:', reward)
# Save the data into memory
if self.is_training:
self.agent.store(last_state, last_action, reward_tensor, current_state)
# Signal control
action = self.agent.get_action(current_state, epsilon)
action_int = int(action)
last_action_int = int(last_action)
# print(self.step, action)
if last_action_int != action_int:
self.set_yellow_red(action_int, last_action_int)
self.set_green(action_int)
else:
traci.simulationStep()
self.step += 1
self.total_waiting_time += self.get_waiting_times()
last_state = current_state
last_action = action
last_waiting_time = current_waiting_time
if reward < 0:
self.total_neg_reward += reward
print(f'Total reward: {self.total_neg_reward} --Epsilon: {epsilon:.3f} --Total steps: {self.step}')
self.save_episode_stats()
traci.close()
simulation_time = round(timeit.default_timer() - start_time, 1)
print(f'------ Simulation time: {simulation_time} ------')
if self.is_training:
print('Training...')
start_time = timeit.default_timer()
for _ in range(self.training_epochs):
self.agent.train()
training_time = round(timeit.default_timer() - start_time, 1)
print(f'------ Training time: {training_time} ------')
# Execute the designated simulation step
def simulate(self, steps_todo):
while steps_todo > 0:
traci.simulationStep()
self.step += 1
steps_todo -= 1
self.total_waiting_time += self.get_waiting_times()
# Get the state
def get_state(self):
state = torch.zeros((1, self.channels, self.height, self.width), device=device)
for veh_id in traci.vehicle.getIDList():
traci.vehicle.subscribe(veh_id, (tc.VAR_NEXT_TLS, tc.VAR_LANE_ID, tc.VAR_SPEED))
p = traci.vehicle.getAllSubscriptionResults()
for x in p:
if p[x][tc.VAR_NEXT_TLS]:
ps_tls = p[x][tc.VAR_NEXT_TLS][0][2] # get the distance to the traffic light
else:
ps_tls = -1 # vehicle has crossed the stop line and set to a negative value
if p[x][tc.VAR_LANE_ID]:
ln_id, ln_idx = p[x][tc.VAR_LANE_ID].split('_') # get the lane id and index
spd = p[x][tc.VAR_SPEED] # get the speed
# get the position in state array
if 0 < ps_tls < detection_length:
height_index = int(ps_tls / cell_length)
for edge in edges.values():
if edge[1] in ln_id:
width_index = int(ln_idx) + edge[0]
state[:, :, height_index, width_index] = torch.tensor([1, spd])
return state
def set_green(self, action):
"""
phase-movement mapping
{0: (WL, EL), 1: (W, WL), 2: (W, E), 3: (E, EL), 4: (SL, NL), 5: (S, SL), 6: (S, N), 7: (N, NL)}
"""
green_state = action_state_map[action]
traci.trafficlight.setRedYellowGreenState('J1', green_state)
self.simulate(self.min_green_time)
# print('------Set green------')
# Activate the corresponding yellow and red phase
def set_yellow_red(self, action, last_action):
action_state = action_state_map[action]
old_action_state = action_state_map[last_action]
yellow_state = []
red_state = []
for i in range(18):
# print(action_state[i], old_action_state[i])
if old_action_state[i] == 'G' and old_action_state[i] != action_state[i]:
yellow_state.append('Y')
else:
yellow_state.append(old_action_state[i])
yellow_state = ''.join(yellow_state)
traci.trafficlight.setRedYellowGreenState('J1', yellow_state)
self.simulate(self.yellow_time)
for i in range(18):
if yellow_state[i] == 'Y':
red_state.append('r')
else:
red_state.append(yellow_state[i])
red_state = ''.join(red_state)
traci.trafficlight.setRedYellowGreenState('J1', red_state)
self.simulate(self.red_time)
# Get the waiting times from the sumo
def get_waiting_times(self):
waiting_times = 0
for edge in incoming_edges:
waiting_times += traci.edge.getWaitingTime(edge)
return waiting_times
def save_episode_stats(self):
self.total_rewards.append(self.total_neg_reward)
self.total_waiting_times.append(self.total_waiting_time)
def get_stats(self):
return {
'Reward': self.total_rewards,
'Mean Waiting Time (s)': np.divide(self.total_waiting_times, self.step)
}
def save_stats(self, save_time):
np.savetxt(f'result\\training_stats_{save_time}.csv', self.total_rewards, delimiter=',')