-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.py
291 lines (251 loc) · 10.7 KB
/
simulator.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
291
from config import *
import source
import control
import trafficlight
from car import Car
from control import *
from sets import Set
def get_blocks_before_turn(new_car):
if new_car.exit_road:
return new_car.exit_road - int(new_car.position/100)
return None
class Simulator(object):
def __init__(self, lanes, lines):
self.current_time = 0
self.warmup_time = 0
self.delta_t = 0.125
self.last_300 = []
self.last_300_private = []
self.people_source = source.Source(0.2)
self.car_source = source.Source(1)
self.warmup = True
self.init_target_functions(lines)
self.init_lanes(lanes)
self.init_queues()
self.init_elements(lines)
self.listener = None
self.warmup_buses = Set([])
def add_listener(self, listener):
self.listener = listener
def init_elements(self, lines):
self.lights = []
self.cars = []
self.buses = []
self.lines = lines
for i in range(1, STREETS):
l = trafficlight.TrafficLight(i * 100, 30)
l.acc = - i * 5
self.lights.append(l)
def init_lanes(self, lanes):
self.lanes = lanes
self.bus_start_lane = lanes[0]
exclusives = False
prev = None
for lane in lanes[0:]:
exclusives = exclusives or lane.exclusive
if prev and lane.exclusive == prev.exclusive:
lane.prev = prev
prev.next = lane
prev = lane
if lane.exclusive or not exclusives:
self.bus_start_lane = lane
lanes.reverse()
for index, lane in enumerate(filter(lambda x: x.exclusive, lanes)):
lane.index = index
for index, lane in enumerate(filter(lambda x: not x.exclusive, lanes)):
lane.index = index
lanes.reverse()
def init_target_functions(self, lines):
self.meters_covered_public = 0
self.meters_covered_private = 0
self.time_spent_public = 0
self.time_spent_private = 0
self.people_finished_public = 0
self.people_finished_private = 0
self.people_in_the_system_public = 0
self.people_in_the_system_private = 0
self.last_bus_arrival_time = {}
self.arrival_histograms = {}
for line in lines:
self.last_bus_arrival_time[line] = 0
self.arrival_histograms[line] = self.build_histogram()
def build_histogram(self):
histogram = []
for i in range(9):
histogram.append(0)
return histogram
def _add_value_to_histogram(self, line, val):
histogram = self.arrival_histograms[line]
frequency = line.frequency
histogram_width = frequency * 1.0 / 9
histogram_start = frequency - histogram_width / 2 - 4 * histogram_width
histogram_end = frequency + histogram_width / 2 + 4 * histogram_width
histogram_x_axis = []
i = histogram_start
while i <= histogram_end:
histogram_x_axis.append(i)
i += histogram_width
if val < histogram_start:
histogram[0] = histogram[0] + 1
elif val > histogram_end:
histogram[8] = histogram[8] + 1
else:
for i in range(8):
if histogram_x_axis[i] <= val < histogram_x_axis[i+1]:
histogram[i] = histogram[i] + 1
def _bus_arrived(self, bus):
if bus in self.warmup_buses:
self.warmup_buses.remove(bus)
else:
self._add_value_to_histogram(bus.line,
self.current_time - self.last_bus_arrival_time[bus.line])
self.last_bus_arrival_time[bus.line] = self.current_time
def init_queues(self):
self.car_queues_init = [
([], lane) for lane in self.lanes if not lane.exclusive
]
self.car_queues_corners = [
([], p) for p in range(100, ROAD_LENGTH, 100)
]
self.bus_queue = []
def advance_cars(self):
for lane in self.lanes:
for car in lane.cars:
control.do_time_step(
car,
lane,
self.lanes,
self.lights,
self.current_time,
self.delta_t
)
self.current_time += self.delta_t
def loop(self):
if self.listener:
self.listener.pre_loop(self)
self.advance_cars()
# Ascenso y descenso de personas en buses
for bus2 in self.buses:
if bus2.just_stopped:
number_left = bus2.people_leave()
if not self.warmup:
self.people_finished_public += number_left
self.people_in_the_system_public -= number_left
number = bus2.closest_stop.bus_arrived(bus2)
bus2.delay_time = DELAY_PER_PERSON * number
bus2.people_carried += number
bus2.just_stopped = False
if self.bus_queue:
new_car = self.bus_queue[0]
if (not get_next_car(new_car, self.bus_start_lane)) \
or rear(get_next_car(new_car, self.bus_start_lane), 0) > DISTANCE_MARGIN:
self.bus_start_lane.add_car(self.bus_queue.pop(0))
if self.warmup:
self.warmup_buses.add(new_car)
for queue in self.car_queues_init:
if len(queue[0]):
lane = queue[1]
new_car = queue[0][0]
if (not get_next_car(new_car, lane)) \
or rear(get_next_car(new_car, lane), 0) > DISTANCE_MARGIN:
new_car.position += new_car.length + DISTANCE_MARGIN
lane.add_car(queue[0].pop(0))
for queue in self.car_queues_corners:
if len(queue[0]) and not self.lights[0].is_green(self.current_time):
new_car = queue[0][0]
target_lanes = filter(lambda x:
not x.exclusive and
(not get_next_car(new_car, x)
or rear(get_next_car(new_car, x), 0)
> new_car.position + DISTANCE_MARGIN),
self.lanes
)
if target_lanes:
new_car.position += new_car.length + DISTANCE_MARGIN
random.choice(target_lanes).add_car(queue[0].pop(0))
if self.people_source.chances_to_appear(self.delta_t):
line = random.choice(self.lines)
stop = random.choice(line.bus_stops)
stop.person_arrived()
self.people_in_the_system_public += 1
self.people_source.reset()
# TEA para autos ingresando al sistema
if self.car_source.chances_to_appear(self.delta_t):
new_car = Car(0, 0, 0, 0, get_random_people_for_private_car())
if random.random() < CHANCES_TO_ADD_AT_BEGINNING:
random.choice(self.car_queues_init)[0].append(new_car)
else:
where = random.choice(self.car_queues_corners)
where[0].append(new_car)
new_car.position = where[1]
self.cars.append(new_car)
self.people_in_the_system_private += new_car.people_carried
new_car.exit_road = get_exit_road(new_car.position)
new_car.blocks_before_turn = get_blocks_before_turn(new_car)
new_car.started = self.current_time
self.car_source.reset()
for line in self.lines:
if self.current_time - line.last_time_appeared > line.frequency:
new_bus = line.new_bus()
self.people_in_the_system_public += new_bus.people_carried
line.last_time_appeared = self.current_time
self.bus_queue.append(new_bus)
self.buses.append(new_bus)
if not self.warmup:
self.time_spent_public += self.delta_t * self.people_in_the_system_public
self.time_spent_private += self.delta_t * self.people_in_the_system_private
for car in self.cars:
self.meters_covered_private += car.last_delta * car.people_carried
for bus in self.buses:
self.meters_covered_public += bus.last_delta * bus.people_carried
else:
self.warmup = not control.has_warmup_finished(self.lanes, MIN_LANES_OCCUPIED, MIN_CARS_PER_LANE)
if not self.warmup:
self.warmup_time = self.current_time
for car in control.remove_old_cars(self.lanes, 0, ROAD_LENGTH):
self.people_in_the_system_private -= car.people_carried
self.cars.remove(car)
if not self.warmup:
self.people_finished_private += car.people_carried
for bus2 in control.remove_old_buses(self.lanes, 0, ROAD_LENGTH):
self.people_in_the_system_public -= bus2.people_carried
self.buses.remove(bus2)
if not self.warmup:
self.people_finished_public += bus2.people_carried
self._bus_arrived(bus2)
self.current_time += self.delta_t
if not self.warmup and self.current_time - math.ceil(self.current_time) == 0:
if self.time_spent_private > 0 and self.time_spent_public > 0:
self.last_300.append(self.public_speed())
self.last_300_private.append(self.private_speed())
if len(self.last_300) > 300:
self.last_300.pop(0)
self.last_300_private.pop(0)
if self.listener:
self.listener.after_loop(self)
def private_speed(self):
return self.meters_covered_private / self.time_spent_private
def public_speed(self):
return self.meters_covered_public / self.time_spent_public
def private_transported(self):
return self.people_finished_private
def public_transported(self):
return self.people_finished_public
def has_finished(self):
# 1 hora
return (self.current_time - self.warmup_time) > 60 * 60
def has_finished_long(self):
if self.warmup or len(self.last_300) != 300:
return False
len_public = len(self.last_300)
mean_public = sum(self.last_300) / len_public
std_public = math.sqrt(sum((x-mean_public)**2 for x in self.last_300) / len_public)
len_private = len(self.last_300_private)
mean_private = sum(self.last_300_private) / len_private
std_private = math.sqrt(sum((x-mean_private)**2 for x in self.last_300_private) / len_private)
return (
not self.warmup
and len(self.last_300) == 300
and std_public < 0.01 * mean_public
and std_private < 0.01 * mean_private
)