2020-09-24 21:23:19 +03:00
|
|
|
import requests
|
|
|
|
import statistics
|
|
|
|
|
|
|
|
|
2020-09-25 03:26:46 +03:00
|
|
|
def get(args, cmd, **kwargs):
|
|
|
|
resp = requests.get(args.api + cmd, **kwargs)
|
|
|
|
if resp.status_code != requests.codes.ok:
|
|
|
|
raise Exception(resp.text)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
def post(args, cmd, **kwargs):
|
|
|
|
resp = requests.post(args.api + cmd, **kwargs)
|
|
|
|
if resp.status_code != requests.codes.ok:
|
|
|
|
raise Exception(resp.text)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2020-09-24 21:23:19 +03:00
|
|
|
# Returns Results
|
2020-09-24 22:08:46 +03:00
|
|
|
def run_sim(args, modifiers=[], edits=None):
|
2020-09-25 03:26:46 +03:00
|
|
|
post(args, '/sim/load', json={
|
2021-02-14 02:45:59 +03:00
|
|
|
'scenario': 'data/system/{}/{}/scenarios/{}/weekday.bin'.format(args.country_code, args.city_name, args.map_name),
|
2020-09-24 21:23:19 +03:00
|
|
|
'modifiers': modifiers,
|
2020-09-24 23:14:49 +03:00
|
|
|
'edits': edits,
|
2020-09-24 21:23:19 +03:00
|
|
|
})
|
2020-09-25 03:26:46 +03:00
|
|
|
post(args, '/sim/goto-time',
|
|
|
|
params={'t': '{}:00:00'.format(args.hours)})
|
2020-09-25 04:08:05 +03:00
|
|
|
raw_trips = get(args, '/data/get-finished-trips').json()
|
2020-09-24 21:23:19 +03:00
|
|
|
|
2020-10-03 20:41:53 +03:00
|
|
|
# Map trip ID to the duration (in seconds) of the trip. Filter out
|
|
|
|
# cancelled trips.
|
|
|
|
num_cancelled = 0
|
2020-09-24 21:23:19 +03:00
|
|
|
trip_times = {}
|
2020-09-25 04:08:05 +03:00
|
|
|
capped_trips = set()
|
|
|
|
for trip in raw_trips:
|
2020-11-22 22:21:44 +03:00
|
|
|
if trip['duration'] is None:
|
2020-10-03 20:41:53 +03:00
|
|
|
num_cancelled += 1
|
2020-09-24 21:23:19 +03:00
|
|
|
else:
|
2020-09-25 04:08:05 +03:00
|
|
|
trip_times[trip['id']] = trip['duration']
|
|
|
|
if trip['capped']:
|
|
|
|
capped_trips.add(trip['id'])
|
2020-09-24 21:23:19 +03:00
|
|
|
|
2020-10-03 20:41:53 +03:00
|
|
|
return Results(num_cancelled, trip_times, capped_trips)
|
2020-09-24 21:23:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Results:
|
2020-10-03 20:41:53 +03:00
|
|
|
def __init__(self, num_cancelled, trip_times, capped_trips):
|
|
|
|
self.num_cancelled = num_cancelled
|
2020-09-24 21:23:19 +03:00
|
|
|
# Maps trip ID to seconds
|
|
|
|
self.trip_times = trip_times
|
2020-09-25 04:08:05 +03:00
|
|
|
# A set of trip IDs
|
|
|
|
self.capped_trips = capped_trips
|
2020-09-24 21:23:19 +03:00
|
|
|
|
|
|
|
# self is the baseline, results2 is the experiment
|
|
|
|
def compare(self, results2):
|
|
|
|
faster = []
|
|
|
|
slower = []
|
|
|
|
|
|
|
|
for trip, after_dt in results2.trip_times.items():
|
|
|
|
before_dt = self.trip_times.get(trip)
|
|
|
|
if not before_dt:
|
|
|
|
# The trip didn't finish in time in the baseline run
|
|
|
|
continue
|
|
|
|
if before_dt:
|
|
|
|
if before_dt > after_dt:
|
|
|
|
faster.append(before_dt - after_dt)
|
|
|
|
elif after_dt > before_dt:
|
|
|
|
slower.append(after_dt - before_dt)
|
|
|
|
|
|
|
|
print('{:,} trips faster, average {:.1f}s savings'.format(
|
2020-09-24 22:08:46 +03:00
|
|
|
len(faster), avg(faster)))
|
2020-09-24 21:23:19 +03:00
|
|
|
print('{:,} trips slower, average {:.1f}s loss'.format(
|
2020-09-24 22:08:46 +03:00
|
|
|
len(slower), avg(slower)))
|
2020-10-03 20:41:53 +03:00
|
|
|
print('{:,} trips cancelled before, {:,} after'.format(
|
|
|
|
self.num_cancelled, results2.num_cancelled))
|
2020-09-24 22:08:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
def avg(data):
|
|
|
|
if data:
|
|
|
|
return statistics.mean(data)
|
|
|
|
else:
|
|
|
|
return 0.0
|