-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicTrafficLight.py
89 lines (57 loc) · 1.57 KB
/
DynamicTrafficLight.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
import time
class Stopwatch:
def __init__(self):
self.creationTime = time.time()
def elapsedTime(self):
return time.time() - self.creationTime
def setZero(self):
self.creationTime = time.time()
class Pole:
def __init__(self):
self.g = 0
self.time = Stopwatch()
def ForStart(carsNorth , carsWest):
if(carsNorth > carsWest):
poleNorth.g = 1
poleNorth.time.setZero()
return [1,0,0,1]
poleWest.g = 1
poleWest.time.setZero()
return [0,1,1,0]
def lights(north , west):
if north[0].g == 0:
toWork = west
noWork = north
else :
toWork = north
noWork = west
if (toWork[0].time.elapsedTime() <= 10):
return None
elif (toWork[0].time.elapsedTime() >=60 ):
##change Light
toWork[0].g = 0
noWork[0].g = 1
noWork[0].time.setZero()
else :
loadFactor = toWork[1] / (toWork[1] + noWork[1])
if(loadFactor < threshHold):
##change Light
toWork[0].g = 0
noWork[0].g = 1
noWork[0].time.setZero()
return [(north[0].g,(north[0].g + 1)%2),(west[0].g,(west[0].g + 1)%2)]
min = 10 # sec
max = 60 # sec
threshHold = 5/12
carsNorth = int(input())
carsWest = int(input())
poleNorth = Pole()
poleWest = Pole()
lightStatus = ForStart(carsNorth, carsWest)
print(lightStatus)
while(1):
lightStatus = lights((poleNorth, carsNorth),(poleWest,carsWest))
print(lightStatus)
time.sleep(5)
carsNorth = int(input())
carsWest = int(input())