-
Notifications
You must be signed in to change notification settings - Fork 0
/
1light.py
129 lines (119 loc) · 3.71 KB
/
1light.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
import numpy as np
import matplotlib.pyplot as plt
#traffic light
#apply cellular automaton algorithm
#road=road setup array, sites=numbers of cells, pnew=probability of new car (traffic density)
#pred=probability of speed reduction, limit=speed limit (in cells/timestep), position=position of traffic light
#stop=True or False
def alg2(road, sites, pnew, pred, limit, position, stop):
#acceleration
for i in range(sites+1):
if road[i]>-1:
if road[i]!=limit:
road[i]+=1
#deceleration depending on spacing and random deleceration
for i in range(sites+1):
if road[i]>-1:
rand=np.random.uniform()
j=i+1
if j<sites:
d=0
while road[j]==-1 or road[j]==-3:
if j==sites:
d='end'
break
d+=1
j+=1
if d!='end':
if d<road[i]:
road[i]=d
if road[i]>0:
if pred>rand:
road[i]-=1
elif d>=road[i]:
if road[i]>0:
if pred>rand:
road[i]-=1
#movement of cars
road2=np.array([])
if stop==True:
light=-2
elif stop==False:
light=-3
for i in range(position):
road2=np.append(road2, -1)
road2=np.append(road2, light)
for i in range(sites-position):
road2=np.append(road2, -1)
count=0
for i in range(sites+1):
if road[i]>-1:
j=i+int(road[i])
if j<sites+1:
road2[j]=road[i]
elif j>sites+1:
count+=1
#randomly, new car introduced
if road2[0]==-1:
if pnew>np.random.uniform():
road2[0]=np.random.randint(0,limit+1)
return road2, count
def func2(sites, pnew, pred, limit, loops, position, stop, on, off):
#loops=number of light changes, on=stop duration, off=go duration
road=np.array([])
if stop==True:
light=-2
elif stop==False:
light=-3
for i in range(position):
rand=np.random.uniform()
if pnew>=rand:
road=np.append(road, np.random.randint(0,limit+1))
elif pnew<rand:
road=np.append(road, -1)
road=np.append(road, light)
for i in range(sites-position):
rand=np.random.uniform()
if pnew>=rand:
road=np.append(road, np.random.randint(0,limit+1))
elif pnew<rand:
road=np.append(road, -1)
roads=[road]
total=1
count=0
if stop==True:
dura=on
elif stop==False:
dura=off
for i in range(loops):
for j in range(dura):
[a,b]=alg2(road, sites, pnew, pred, limit, position, stop)
road=a
roads.append(road)
total+=1
count+=b
if stop==True:
stop=False
dura=off
road[position]=-3
elif stop==False:
stop=True
dura=on
road[position]=-2
roads=np.asarray(roads)
roads=roads.astype(int)
roads=np.ndarray.tolist(roads)
for i in range(total):
for j in range(sites+1):
roads[i][j]=str(roads[i][j])
if roads[i][j]=='-1':
roads[i][j]='.'
elif roads[i][j]=='-2':
roads[i][j]='|'
elif roads[i][j]=='-3':
roads[i][j]='.'
for i in range(total):
str1=''.join(roads[i])
print(str1)
return print('Number of cars passed = ', count)
func2(100,0.2,0.3,5,10,50,False,40,30)