forked from pedropintoo/SmartTLS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
142 lines (111 loc) Β· 5.34 KB
/
Copy pathrunner.py
File metadata and controls
142 lines (111 loc) Β· 5.34 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
import os
import sys
import optparse
import random
from collections import defaultdict
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'")
from sumolib import checkBinary # noqa
import traci # noqa
def _get_acumulated_waiting_time():
""" Get the acumulated waiting time of the vehicles """
waiting_time = 0
for lane_id in traci.lanearea.getIDList():
vehicles = traci.lanearea.getLastStepVehicleIDs(lane_id)
for veh in vehicles:
waiting_time += traci.vehicle.getAccumulatedWaitingTime(veh)
return waiting_time
def run():
"""execute the TraCI control loop"""
## Tests
print(traci)
print(traci.trafficlight.getIDList())
list_tls = traci.trafficlight.getIDList()
filtered_tls = [tls for tls in list_tls if tls.startswith("TLS")]
print("FILTERED TLS", filtered_tls)
# Compacted version of the previous line
filtered_tls = [tls for tls in traci.trafficlight.getIDList() if tls.startswith("TLS")]
print("FILTERED TLS", filtered_tls)
filtered_detectors = defaultdict(list)
all_detectors = traci.lanearea.getIDList()
for detector in all_detectors:
tls = detector.split("_")[0]
filtered_detectors[tls].append(detector)
print("FILTERED DETECTORS", filtered_detectors)
# Compacted version of the previous line
filtered_detectors = defaultdict(list)
for detector in traci.lanearea.getIDList():
tls = detector.split("_")[0]
filtered_detectors[tls].append(detector)
print("Number of phases: ", len(traci.trafficlight.getAllProgramLogics("TLS1")[0].getPhases()))
## RUN
step = 0
# we start with phase 0 where NS has green
traci.trafficlight.setPhase("TLS1", 0)
accumulated_waiting_times = dict()
currently_waiting = dict()
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
if step % 4 == 0:
if traci.trafficlight.getPhase("TLS1") == 0:
traci.trafficlight.setPhase("TLS1", 2) # set phase to 2
else:
traci.trafficlight.setPhase("TLS1", 0) # set phase to 0
step += 1
queue_length = traci.lanearea.getJamLengthVehicle("TLS1_Det1") / 100
space_occupied = traci.lanearea.getLastStepOccupancy("TLS1_Det1") / 100
mean_speed = traci.lanearea.getIntervalMeanSpeed("TLS1_Det1")
observation = [queue_length, mean_speed, space_occupied]
total_accumulated_waiting = 0
for lane_id in traci.lanearea.getIDList():
for vehicle in traci.lanearea.getLastStepVehicleIDs(lane_id):
wt = traci.vehicle.getWaitingTime(vehicle)
if wt == 0: # vehicle is not waiting
# Safely delete from currently_waiting
if lane_id in currently_waiting and vehicle in currently_waiting[lane_id]:
del currently_waiting[lane_id][vehicle]
# Add the accumulated waiting time if exists
if vehicle in accumulated_waiting_times and lane_id in accumulated_waiting_times[vehicle]:
total_accumulated_waiting += accumulated_waiting_times[vehicle][lane_id]
continue
# Initialize or update the accumulated waiting time
if vehicle not in accumulated_waiting_times: # never seen before
accumulated_waiting_times[vehicle] = {lane_id: wt}
else:
if lane_id not in accumulated_waiting_times[vehicle]: # never seen before in this lane
accumulated_waiting_times[vehicle][lane_id] = wt
else:
accumulated_waiting_times[vehicle][lane_id] += wt - currently_waiting[lane_id].get(vehicle, 0)
# Update currently waiting dictionary
if lane_id not in currently_waiting:
currently_waiting[lane_id] = {}
currently_waiting[lane_id][vehicle] = wt
total_accumulated_waiting += accumulated_waiting_times[vehicle][lane_id]
print(total_accumulated_waiting)
# print(observation)
# print(_get_acumulated_waiting_time())
# print(traci.trafficlight.getDetectorVariable("TLS", "0", "vehicleCount"))
traci.close()
sys.stdout.flush()
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true",
default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
# this is the main entry point of this script
if __name__ == "__main__":
options = get_options()
# this script has been called from the command line. It will start sumo as a
# server, then connect and run
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
# this is the normal way of using traci. sumo is started as a
# subprocess and then the python script connects and runs
traci.start([sumoBinary, "-c", "sumo_config/cross/cross.sumocfg"])
run()