-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_code1.py
More file actions
168 lines (134 loc) · 4.52 KB
/
main_code1.py
File metadata and controls
168 lines (134 loc) · 4.52 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 6 01:38:58 2021
@author: Sristi
"""
import filehandle
import dailycomm
streets,cars,stime=filehandle.ret()
# #print(streets)
# #print(cars)
# route_to_inter=[]
# for i in cars:
# stop=[]
# for j in i[1:]:
# for k in streets:
# if j==k[2]:
# stop.append(k[1])
# route_to_inter.append(stop)
#print(route_to_inter,"route")
import inout
begs,ends=inout.di()
#print(inter_freq)
# priority=[]
# for key in ends:
# maxi=len(begs[key])+len(ends[key])
# if maxi==0:
# pass
# else:
# priority.append([maxi,key])
# priority.sort(reverse=True)
# #print(priority)
# inter_freq=[]
# for ele in priority:
# count=0
# for i in route_to_inter:
# if ele[1] in i:
# count+=1
# inter_freq.append([ele[1],count])
#Op = [str(len(begs.keys()))+'\n']
#print(ends)
mapp2,accmap=dailycomm.retlist()
# #print(mapp2)
# traffic=[]
# for key in ends:
# #print(key)
# minil=[]
# for road in ends[key]:
# #print(road)
# for j in range (len(mapp2)):
# if road == mapp2[j][1]:
# minil.append(mapp2[j][0])
# traffic.append([key,minil])
#Quick question : minil is a list containing the cars lined up for that road
# traffic is a list of lists containing intersection and cars in that intersection in the order of most frequent roads
# Now the most frequented road cannot have less number of cars than the latter roads so it is safe to assume that the minilist for each intersection would be sorted in the reverse order.
# Another interesting thing is sum(minil)<=simulation time? || sum(minil)<=total cars
# print(traffic)
OP=[str(len(begs.keys()))+'\n']
def greenlight(OP):
for mkey in ends:
OP.append(mkey+'\n')
#OP.append(str(len(ends[mkey]))+'\n')
outl=[]
#count=0
summ=[]
eachline=[]
for key in mapp2:
# print("key",key)
if key in accmap:
if key in ends[mkey]:
if accmap[key]!=0:
summ.append(accmap[key])
eachline.append(key)
# if count>0:
# OP.append(str(count)+'\n')
# count=0
# print("OP",OP)
# print('outl',outl)
for line in eachline:
#if accmap[line]/sum(summ)<1:
# print("Summ= ",summ)
#print("accmap[line]=",accmap[line])
outl+=[line +' '+str(round((accmap[line]/sum(summ))*stime)) +'\n']
outl.insert(0,str(len(eachline))+'\n')
OP=OP+outl
return OP
#OP gives roads and the time for green light cycle for each intersection
# for key1 in ends:
# for key2 in accmap:
# if key2 in ends[key1]:
# print("yes")
# for i in len(ends[key1]):
# print(accmap[key1][i])
# if accmap[key1][i]!=0:
# out=ends[key1][i]+' '+accmap[key1][i]+'\n'
# print("out",out)
# else:
# pass
# OP.append(out)
# return OP
stuff=greenlight(OP)
f=open("SubbD.txt",'w')
f.writelines(stuff)
f.close()
# for i in priority:
# Op.append(i[1]+'\n')
# Op.append(str(len(ends[i[1]]))+'\n')
# for k in inter_freq:
# if i[1]==k[0]:
# time=k[1]//len(ends[i[1]]) #proportianal to roads--freq
# if time>stime:
# time=stime
# elif time==0:
# time=1
# else:
# pass
# for j in ends[i[1]]:
# Op.append(j+' '+str(time)+'\n')
# #print(Op)
'''f=open("SubbD.txt",'w')
f.writelines(Op)
f.close()'''
'''
mapps=[[freq,road_name]...]
#The road with max frequency of cars comes first.
adding the incoming cars from mapps to minil for each intersection
minil=[cars_in_2,cars_in_1,cars_in_3]
# Hence the car frequency would be in decreasing order given that
#we traversed mapps to compute this
traffic=[[key1,[cars_in_2,cars_in_3,cars_in_1..]],[key2,..]]
# Here cars_in_1,2,3 is sorted--reverse order
ends={key1:[road_1,road_2,road_3..]..}
#Here road_1,2,3 is not in the same order as the cars_in_1,2,3!
'''