-
Notifications
You must be signed in to change notification settings - Fork 1
/
maininganother.py
78 lines (61 loc) · 1.91 KB
/
maininganother.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
import pickle
import time
import os
import sys
import traci
from done2 import *
obj=redlight()
trafficlights = []
current_states = []
try:
tools=os.path.join(os.environ['SUMO_HOME'],'tools')
sys.path.append(tools)
except ImportError:
sys.exit("please declare environment variable 'SUMO_HOME' as the root directory of your sumo installation (it should contain folders 'bin', 'tools' and 'docs')")
sumoBinary = "C:\\Program Files (x86)\\DLR\\Sumo\\bin\\sumo-gui"
sumoCmd = [sumoBinary, "-c", "C:\\Users\\anike\\Desktop\\SMARTathon\\intersection1\\intersection1\\intersection1.sumocfg"]
traci.start(sumoCmd)
def throughput():
traffic_lights = traci.trafficlights.getIDList()
for i in trafficlights:
current_states.append(traci.trafficlights.getRedYellowGreenState(i))
throughput = []
l = []
l.append(traci.inductionloop.getLastStepVehicleNumber("0"))
l.append(traci.inductionloop.getLastStepVehicleNumber("1"))
l.append(traci.inductionloop.getLastStepVehicleNumber("2"))
l.append(traci.inductionloop.getLastStepVehicleNumber("3"))
throughput.extend(l)
return(throughput)
step = 0
listOftrafficlights=traci.trafficlights.getIDList()
visited=[]
red = 0
green = 0
countofsteps = 0
#next = 'GrGr'
while traci.simulation.getMinExpectedNumber()>0:
current=traci.trafficlights.getRedYellowGreenState('0')
traci.simulationStep()
val_list=throughput()
print(val_list)
obj.talk(val_list)
tempy = current
next=obj.getlight()
if tempy != next:
countofsteps += 1
#print(next)
if next == 'GrGr':
green += 1
elif next == 'rGrG':
red += 1
traci.trafficlights.setRedYellowGreenState('0',next)
#time.sleep(0.2)
step += 1
if traci.simulation.getMinExpectedNumber() == 0:
print(traci.simulation.getCurrentTime())
traci.close(False)
timeavg = [float(red)/float(countofsteps), float(green)/float(countofsteps)]
with open("timing2.txt" ,'a') as filey:
for item in timeavg:
filey.write("{}\n".format(item))