-
Notifications
You must be signed in to change notification settings - Fork 1
/
SUMOHelper.py
206 lines (168 loc) · 5.92 KB
/
SUMOHelper.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
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
import os
import sys
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'")
import traci
import numpy as np
class SUMOHelper:
"""Class to deal with sumo apis"""
def __init__(self,binary_path,sumo_cfg_path):
super(SUMOHelper, self).__init__()
self.traci = traci
self.sumoBinary = binary_path
self.sumoCmd = [self.sumoBinary, "-c", sumo_cfg_path, "--start","--quit-on-end"]
def start(self):
self.traci.start(self.sumoCmd)
def close(self):
self.traci.close(wait=True)
def __str__(self):
return "This is custom SUMO module\n"
def getTrafficLights(self):
return self.traci.trafficlight
def getSimulation(self):
return self.traci.simulation
def step(self):
self.traci.simulationStep()
def getWaitingTime(self):
routes = ['1si','2si','3si','4si']
wt = 0
for r in routes:
wt+=self.traci.edge.getLastStepHaltingNumber(r)
return wt
def getr1(self):
return self.traci.edge.getLastStepVehicleNumber(
'1si') + self.traci.edge.getLastStepVehicleNumber('2si')
def getr2(self):
return self.traci.edge.getLastStepVehicleNumber(
'3si') + self.traci.edge.getLastStepVehicleNumber('4si')
def simulate(self,waiting_time,action,light,tls,step):
if(action == 0 and light[0][0][0] == 0):
for i in range(6):
step += 1
tls.setPhase('0', 1)
waiting_time += self.getWaitingTime()
self.step()
for i in range(10):
step += 1
tls.setPhase('0', 2)
waiting_time += self.getWaitingTime()
self.step()
for i in range(6):
step += 1
tls.setPhase('0', 3)
waiting_time += self.getWaitingTime()
self.step()
# Action Execution
r1 = self.getr1()
r2 = self.getr2()
for i in range(10):
step += 1
tls.setPhase('0', 4)
r1 += self.getr1()
r2 += self.getr2()
waiting_time += self.getWaitingTime()
self.step()
if(action == 0 and light[0][0][0] == 1):
r1 = self.getr1()
r2 = self.getr2()
for i in range(10):
step += 1
tls.setPhase('0', 4)
r1 += self.getr1()
r2 += self.getr2()
waiting_time += self.getWaitingTime()
self.step()
if(action == 1 and light[0][0][0] == 0):
r2 = self.getr1()
r1 = self.getr2()
for i in range(10):
step += 1
r2 += self.getr1()
r1 += self.getr2()
tls.setPhase('0', 0)
waiting_time += self.getWaitingTime()
self.step()
if(action == 1 and light[0][0][0] == 1):
for i in range(6):
step += 1
tls.setPhase('0', 5)
waiting_time += self.getWaitingTime()
self.step()
for i in range(10):
step += 1
tls.setPhase('0', 6)
waiting_time += self.getWaitingTime()
self.step()
for i in range(6):
step += 1
tls.setPhase('0', 7)
waiting_time += self.getWaitingTime()
self.step()
r2 = self.getr1()
r1 = self.getr2()
for i in range(10):
step += 1
tls.setPhase('0', 0)
r2 += self.getr1()
r1 += self.getr2()
waiting_time += self.getWaitingTime()
self.step()
state = self.getState()
reward = r1-r2
return step,state,reward,waiting_time
def getState(self):#get velocity and position normalize the data
cellLength = 7
offset = 11
speedLimit = 14
junctionPosition = traci.junction.getPosition('0')[0]
vehicles_road1 = traci.edge.getLastStepVehicleIDs('1si')
vehicles_road2 = traci.edge.getLastStepVehicleIDs('2si')
vehicles_road3 = traci.edge.getLastStepVehicleIDs('3si')
vehicles_road4 = traci.edge.getLastStepVehicleIDs('4si')
positionMatrix = [[0]*12 for y in range(12)]
velocityMatrix = [[0]*12 for y in range(12)]
for v in vehicles_road1:
ind = int(
abs((junctionPosition - traci.vehicle.getPosition(v)[0] - offset)) / cellLength)
if(ind < 12):
positionMatrix[2 - traci.vehicle.getLaneIndex(v)][11 - ind] = 1
velocityMatrix[2 - traci.vehicle.getLaneIndex(
v)][11 - ind] = traci.vehicle.getSpeed(v) / speedLimit
for v in vehicles_road2:
ind = int(
abs((junctionPosition - traci.vehicle.getPosition(v)[0] + offset)) / cellLength)
if(ind < 12):
positionMatrix[3 + traci.vehicle.getLaneIndex(v)][ind] = 1
velocityMatrix[3 + traci.vehicle.getLaneIndex(
v)][ind] = traci.vehicle.getSpeed(v) / speedLimit
junctionPosition = traci.junction.getPosition('0')[1]
for v in vehicles_road3:
ind = int(
abs((junctionPosition - traci.vehicle.getPosition(v)[1] - offset)) / cellLength)
if(ind < 12):
positionMatrix[6 + 2 -
traci.vehicle.getLaneIndex(v)][11 - ind] = 1
velocityMatrix[6 + 2 - traci.vehicle.getLaneIndex(
v)][11 - ind] = traci.vehicle.getSpeed(v) / speedLimit
for v in vehicles_road4:
ind = int(
abs((junctionPosition - traci.vehicle.getPosition(v)[1] + offset)) / cellLength)
if(ind < 12):
positionMatrix[9 + traci.vehicle.getLaneIndex(v)][ind] = 1
velocityMatrix[9 + traci.vehicle.getLaneIndex(
v)][ind] = traci.vehicle.getSpeed(v) / speedLimit
light = []
if(traci.trafficlight.getPhase('0') == 4):
light = [1, 0]
else:
light = [0, 1]
position = np.array(positionMatrix)
position = position.reshape(1, 12, 12, 1)
velocity = np.array(velocityMatrix)
velocity = velocity.reshape(1, 12, 12, 1)
lgts = np.array(light)
lgts = lgts.reshape(1, 2, 1)
return [position, velocity, lgts]