import random
import math
n=0
overalltime=0
while n<=10000:
redlight = ("100",
"10",
"90",
"20",
"60",
"20",
"20",
"20",
"20",
"60",
"20",
"20",
"90",
"90",
"100",
"60",
"100",
"80",
"80",
"60",
"90",
"60",
"120",
"60",
"80",
"60",
"1",
"1")
greenlight = ("30",
"120",
"30",
"120",
"40",
"120",
"120",
"120",
"120",
"40",
"120",
"120",
"40",
"15",
"20",
"20",
"20",
"20",
"20",
"40",
"30",
"20",
"40",
"40",
"20",
"40",
"1",
"1")
distance = ("400",
"400",
"250",
"250",
"250",
"450",
"300",
"650",
"1000",
"450",
"500",
"900",
"450",
"400",
"1100",
"900",
"600",
"1000",
"450",
"450",
"400",
"450",
"200",
"500",
"350",
"400",
"500",
"500")
velocity = ("80",
"80",
"80",
"80",
"80",
"80",
"60",
"80",
"80",
"80",
"80",
"80",
"80",
"80",
"80",
"80",
"80",
"60",
"80",
"80",
"80",
"80",
"80",
"60",
"80",
"80",
"80",
"40")
r=0
g=0
d=0
v=0
vcurrent=float(0)
t=0
gtime=0
accel=float(100/(3.6*15))
decel = float(accel*2)
while r<=26:
red=float(redlight[r])
grn=float(greenlight[g])
dis=float(distance[d])
vel=float(float(velocity[v])/3.6)
vnext=float(float(velocity[v+1])/3.6)
saccel = float((vcurrent*(vel-vcurrent)/accel) + (vel-vcurrent)*(vel-vcurrent)/(2*accel))
scrit = float(dis-(vel/decel) - (vel*vel)/(2*decel))
startingred = random.randint(0, (int(grn)-1))
if vcurrent == vel:
if vnext>= vcurrent:
t = int (dis/vel)
if (t+startingred)%(red+grn)<=red:
t = int (scrit/vel + (vel/decel) + red-((t+startingred)%(red+grn))) ### 2
vcurrent = 0
else:
t = int (dis/vel)### 1
vcurrent = vel
else:
t = int ((scrit/vel) +
(vcurrent - (vnext)/((vcurrent*(vcurrent - (vnext/(dis-scrit))-
((vcurrent - vnext)*(vcurrent - vnext)/(2*(dis-scrit))))))))
if (t+startingred)%(red+grn)<=red:
t = int (scrit/vel + (vel/decel)+ red-((t+startingred)%(red+grn)))### 2
vcurrent = 0
else:
t = int (scrit/vel +
(vcurrent - vnext)/((vcurrent*(vcurrent - (vnext/(dis-scrit))-
((vcurrent - vnext)*(vcurrent - vnext)/(2*(dis-scrit)))))))### 3
vcurrent = float(vnext/3.6)
elif vcurrent < vel:
vcrit=math.sqrt(2*accel*scrit+vcurrent*vcurrent)
if saccel >= scrit:
if vnext >= vcrit:
t = int(((vcrit-vcurrent)/ accel) + (dis-scrit)/vcrit)
if (t+startingred)%(red+grn)<=red:
t = int(((vcrit-vcurrent)/ accel) + ((dis-scrit)*2/vcrit) + red-((t+startingred)%(red+grn)))### 8
vcurrent = 0
else:
t = int(((vcrit-vcurrent)/ accel) + (dis-scrit)/vcrit) ### 7
vcurrent = vcrit
else:
t = int(((vcrit-vcurrent)/ accel) + (vcrit - vnext)/((vcrit*(vcrit - vnext)/(dis-scrit))-
((vcrit - vnext)*(vcrit - vnext)/(2*(dis-scrit)))))
if (t+startingred)%(red+grn)<=red:
t = int(((vcrit-vcurrent)/ accel) + ((dis-scrit)*2/vcrit) + red-((t+startingred)%(red+grn)) ) ### 8
vcurrent = 0
else:
t = int(((vcrit-vcurrent)/ accel) + (vcrit - vnext)/((vcrit*(vcrit - vnext)/(dis-scrit))-
((vcrit - vnext)*(vcrit - vnext)/(2*(dis-scrit))))) ### 9
vcurrent = vnext
else:
if vnext>= vel:
t = int(((vel- vcurrent)/accel) + (dis-saccel)/vel)
if (t+startingred)%(red+grn)<=red:
t = int (((vel- vcurrent)/accel) + (scrit-saccel)/vel + (vel/decel)+ red-((t+startingred)%(red+grn)))### 5
vcurrent = 0
else:
t = int (((vel- vcurrent)/accel) + (dis-saccel)/vel)### 4
vcurrent = vel
else:
if (t+startingred)%(red+grn)<=red:
t = int (((vel- vcurrent)/accel) + (scrit-saccel)/vel + (vel/decel)+ red-((t+startingred)%(red+grn)))### 5
vcurrent = 0
else:
t = int (((vel- vcurrent)/accel) +(scrit-saccel)/vel + (vel - vnext)/((vel*(vel - vnext)/(dis-scrit))-((vel - vnext)*(vel - vnext)/(2*(dis-scrit))))) ### 6
vcurrent = vnext
else:
print ("ERROR: v current > v next")
#print (t)
r+=1
g+=1
d+=1
v+=1
gtime+=t
dev=(1476-gtime)*(1476-gtime)
#print (gtime)
n+=1
dev+=dev
overalltime+=gtime
print ("mean= ", overalltime/n)
print ("deviation= ", math.sqrt(dev/n))