-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·290 lines (237 loc) · 8.46 KB
/
main.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
import sys
import glob
import os
import random
import networkx as nx
from collections import defaultdict
from copy import deepcopy
from model import *
from ricsim import RicSim
overall_best_score = 0
def look_for_best_score():
global overall_best_score
infile = sys.argv[1]
directory = os.path.dirname(infile)
file_list = glob.glob(infile + "*_.out")
for file in file_list:
file_score = int(file.split("_")[-2])
if file_score > overall_best_score:
overall_best_score = file_score
# parsing
D, I, S, V, F = 0, 0, 0, 0, 0
streets = []
paths = []
intersections = {}
street_map = {}
cars = []
def parse(filename):
lines = open(filename).readlines()
global D,I,S,V,F
D,I,S,V,F = map(int,lines[0].split())
global streets, street_map
streets = []
street_map = {}
for line in lines[1:S+1]:
assert(len(line.split()) == 4)
B,E, name, L = line.split()
B,E,L=map(int,[B,E,L])
streets += [(B,E,name,L)]
street_map[name] = (B,E,name,L)
assert(len(streets) == S)
global intersections
for B, E, name, _ in streets:
if B not in intersections:
intersections[B] = [], []
intersections[B][1].append(name)
if E not in intersections:
intersections[E] = [], []
intersections[E][0].append(name)
global paths
global cars
for line in lines[S+1:]:
P = int(line.split()[0])
assert(len(line.split()) == P+1)
paths += [[(s, street_map[s][3]) for s in line.strip().split()[1:]]]
cars.append(Car(paths[-1]))
assert(len(paths) == V)
parse(sys.argv[1])
# smart things
class Solution:
def __init__(self):
self.score = 0
self.best_score = 0
self.cycles = {}
def get_score(self,verbose=False):
global intersections,street_map,cars,F,D
ricsim = RicSim(intersections,street_map,cars,F)
score = ricsim.simulate(self.cycles,D)
return score
def get_score_rayan(self,verbose=False):
# gather queue at each street extremity
street_lqueue = defaultdict(list)
lights = defaultdict()
cars_copy = deepcopy(cars)
# make all cars be at the extremity of their street to start sim
for car in cars_copy:
car.street = car.path[0][0]
car.time = car.path[0][1]
street_lqueue[car.street] = [car] + street_lqueue[car.street]
# set the light pattern
cycles = defaultdict()
cycles_lens = defaultdict()
for i in self.cycles:
time = 0
cycle_len = sum([d for s,d in self.cycles[i]])
for street, duration in self.cycles[i]:
cycles_lens[street] = cycle_len
cycles[street] = [False] * cycle_len
for _ in range(duration):
cycles[street][time] = True
time += 1
def is_light_on(street,T):
if street not in cycles_lens:
return False
cycle_len = cycles_lens[street]
cycle = cycles[street]
pos = T % cycle_len
return cycle[pos]
# now advance cars whenever possible
score = 0
for T in range(D):
#if verbose:
# print("T=",T)
has_passed = set() # only one car can pass a trafic light at that timepoint
for car in cars_copy:
if car.done: continue
street = car.street
#print(street)
# move the car
car.time += 1
# add car to street queue if its time has come
L = street_map[street][3]
if car.time == L:
street_lqueue[street] += [car]
if car.time >= L:
# see if it is waiting for the light
if len(street_lqueue[street]) > 0 and car == street_lqueue[street][0]:
# move car to next street, if possible, otherwise it waits
if street in has_passed:
# another car already passed, skip
continue
if is_light_on(street,T):
street_lqueue[street] = street_lqueue[street][1:]
has_passed.add(street)
if len(car.path) == 1:
# car is done! add to score
#if verbose:
# print("car is done!",score)
car.done = True
score += F + D - T
else:
new_street = car.path[1][0]
#print("moving car from",street,"to",new_street)
car.path = car.path[1:] # modifies the car path (but it's a copy)
car.street = car.path[0][0]
car.time = 0
return score
def __str__(self):
return f"{self.score} {self.best_score} - {self.cycles}"
def save(self):
global overall_best_score
current_score=self.get_score(verbose=True)
if current_score >= overall_best_score:
print("saved", current_score)
with open(sys.argv[1] + "_" + str(current_score) + "_.out", "w") as fp:
print(len(self.cycles), file=fp)
for i, cycle in self.cycles.items():
print(i, file=fp)
print(len(cycle), file=fp)
for street, time in cycle:
print(street, time, file=fp)
else:
print("not saved", current_score)
def naive_solution():
global intersections
sol = Solution()
for i, tup in intersections.items():
sol.cycles[i] = [(x, 1) for x in tup[0]]
# print(sol.cycles)
return sol
def greedy_cars():
global cars, street_map, intersections
# Compute hot points
busyness = {s:0 for s in street_map.keys()}
car_counts = {s:0 for s in street_map.keys()}
sorted_cars = sorted([c.min_time for c in cars])
# print("][".join([str(x) for x in sorted_cars]))
for car in cars:
for street, length in car.path[:-1]:
busyness[street] += 1 / length
car_counts[street] += 1
car_incluence = []
for car in cars:
influence = 0
for street, length in car.path[:-1]:
if car_counts[street] == 1 and len(intersections[street_map[street][1]][0]) > 1:
influence += 1
car_incluence.append(influence)
# print(car_incluence)
print(sum(car_incluence)/len(car_incluence))
threshold = sorted(car_incluence, reverse=True)[:3]
print(threshold)
threshold = threshold[-1]
problem_cars = [c for i,c in enumerate(cars) if car_incluence[i] < threshold]
# remove cars
for c in problem_cars:
for street, length in car.path[:-1]:
busyness[street] -= 1 / length
# For each intersection determine the cycle regarding the busyness
sol = Solution()
for i, tup in intersections.items():
inputs = tup[0]
local_busy = [busyness[street] for street in inputs if busyness[street] > 0]
# print(local_busy)
if len(local_busy) == 0:
continue
busy_min = min(local_busy)
sol.cycles[i] = [(street, round(min(1, busyness[street]/busy_min))) for street in inputs if busyness[street] > 0]
random.shuffle(sol.cycles[i])
# print(sol.cycles[i])
return sol
# Change the time of each traffic light with probability prob. Adding or subtracting is equally probable (coin flip). The value is chosen in [0,maxdelta] (uniform)
def fuzzer(solution, prob=25, maxdelta=1):
for intidx, streets in solution.cycles.items():
for i in range(len(streets)):
if random.randint(1,100) <= prob: # do the change
sub = random.randint(1,2) == 1
delta = random.randint(0,maxdelta)
streets[i] = (streets[i][0], max(1, streets[i][1] + (1-2*sub) * delta))
# Change a single traffic light time (given intersection idx and street name)
def edit_singletl_time(solution, intidx, streetname, value):
for i in range(len(solution.cycles[intidx])):
if solution.cycles[intidx][i][0] == streetname:
solution.cycles[intidx][i] = (solution.cycles[intidx][i][0], solution.cycles[intidx][i][1] + value)
def main():
sol = greedy_cars()
sol.save()
exit(0)
# Store cars
cars = []
for path in paths:
cars.append(Car(path))
# Store graph
G = nx.DiGraph()
# Nodes:
for inter_idx in set(street[0] for street in streets) | set(street[1] for street in streets):
G.add_node(inter_idx, data = Intersection(inter_idx))
# Edges:
for sinter, einter, name, length in streets:
G.add_edge(sinter, einter, data = Street(name, length))
sol = naive_solution()
#print(sol)
fuzzer(sol, prob=100, maxdelta=5)
#print(sol)
if __name__ == "__main__":
look_for_best_score()
main()